Skip to content
This repository was archived by the owner on Apr 23, 2024. It is now read-only.

Commit 2599040

Browse files
authored
Merge pull request #7 from jeckel/add-clock-module
Add clock module
2 parents 55ff997 + 5658cd8 commit 2599040

File tree

4 files changed

+155
-5
lines changed

4 files changed

+155
-5
lines changed

composer.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121
}
2222
},
2323
"require": {
24-
"php": ">=7.0",
24+
"php": ">=7.1",
2525
"codeception/codeception": "^2.5",
2626
"behat/behat": "^3.5"
2727
},
2828
"require-dev": {
2929
"phpstan/phpstan": "^0.11.5",
3030
"squizlabs/php_codesniffer": "^3.4",
31-
"phpmd/phpmd": "^2.6"
31+
"phpmd/phpmd": "^2.6",
32+
"mikey179/vfsStream": "^1.6"
3233
},
3334
"suggest": {
3435
"edno/codeception-gherkin-param": "The Codeception extension for supporting parameter notation in Gherkin scenario.",
35-
"flow/jsonpath" : "Json path parser"
36+
"flow/jsonpath": "Json path parser",
37+
"jeckel/clock": "Clock service with mock capability"
3638
}
3739
}

composer.lock

+50-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Clock.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
namespace Jeckel\Gherkin;
3+
4+
use Codeception\Lib\Interfaces\RequiresPackage;
5+
use Codeception\Configuration;
6+
7+
/**
8+
* Class Clock
9+
* @package Jeckel\Gherkin
10+
* @SuppressWarnings(PHPMD.LongVariable)
11+
*/
12+
class Clock extends ContextAbstract
13+
{
14+
/** @var array */
15+
protected $requiredFields = ['clock_path'];
16+
17+
// disable all inherited actions
18+
public static $includeInheritedActions = false;
19+
20+
/** @var string */
21+
protected $projectDir = null;
22+
23+
/**
24+
* @return string
25+
* @SuppressWarnings(PHPMD.StaticAccess)
26+
*/
27+
public function getProjectDir(): string
28+
{
29+
if (empty($this->projectDir)) {
30+
return Configuration::projectDir();
31+
}
32+
return $this->projectDir;
33+
}
34+
35+
/**
36+
* @param string $projectDir
37+
* @return Clock
38+
*/
39+
public function setProjectDir(string $projectDir): Clock
40+
{
41+
$this->projectDir = $projectDir;
42+
return $this;
43+
}
44+
45+
/**
46+
* @Given I set fake clock to :clock
47+
* @param string $clock
48+
*/
49+
public function setFakeClockTo(string $clock)
50+
{
51+
file_put_contents($this->projectDir . $this->config['clock_path'], $clock);
52+
}
53+
}

tests/unit/ClockTest.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* User: jeckel
4+
* Date: 21/04/19
5+
* Time: 15:45
6+
*/
7+
8+
namespace Test\Jeckel\Gherkin;
9+
10+
use Codeception\Lib\ModuleContainer;
11+
use Jeckel\Gherkin\Clock;
12+
use org\bovigo\vfs\vfsStream;
13+
use org\bovigo\vfs\vfsStreamDirectory;
14+
use org\bovigo\vfs\vfsStreamWrapper;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
17+
class ClockTest extends \Codeception\Test\Unit
18+
{
19+
/** @var Clock */
20+
protected $helper;
21+
22+
public function setUp()
23+
{
24+
/** @var MockObject | ModuleContainer $moduleContainer */
25+
$moduleContainer = $this->createMock(ModuleContainer::class);
26+
27+
$this->helper = new Clock($moduleContainer);
28+
29+
vfsStreamWrapper::register();
30+
vfsStreamWrapper::setRoot(new vfsStreamDirectory('clockDir'));
31+
$this->helper->setProjectDir(vfsStream::url('clockDir'));
32+
33+
return parent::setUp(); // TODO: Change the autogenerated stub
34+
}
35+
36+
public function testSetFakeClockTo()
37+
{
38+
$clock = '2018-01-01 10:10:00';
39+
$clockFile = '/clock';
40+
41+
$this->helper->_setConfig(['clock_path' => $clockFile]);
42+
43+
$this->helper->setFakeClockTo($clock);
44+
$this->assertTrue(vfsStreamWrapper::getRoot()->hasChild('clock'));
45+
$this->assertEquals($clock, file_get_contents(vfsStream::url('clockDir') . $clockFile));
46+
}
47+
}

0 commit comments

Comments
 (0)