diff --git a/composer.json b/composer.json index 1038509..f335804 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,10 @@ "squizlabs/php_codesniffer": "^3.5", "mikey179/vfsstream": "^1.6", "vimeo/psalm": "^4.3", - "phpro/grumphp": "^0.19 || ^1.2.0" + "phpro/grumphp": "^0.19 || ^1.2.0", + "codeception/codeception": "^4.1" + }, + "suggest": { + "codeception/codeception": "Use helper to handle clock with your codeception tests" } } diff --git a/phpunit.xml b/phpunit.xml index 51caa44..2591178 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,6 +5,9 @@ src + + src/CodeceptionHelper + diff --git a/src/CodeceptionHelper/Clock.php b/src/CodeceptionHelper/Clock.php new file mode 100644 index 0000000..f188655 --- /dev/null +++ b/src/CodeceptionHelper/Clock.php @@ -0,0 +1,43 @@ + + * Created at: 25/03/2021 + */ + +declare(strict_types=1); + +namespace JeckelLab\Clock\CodeceptionHelper; + +use Codeception\Configuration; +use Codeception\Module; +use RuntimeException; + +/** + * Class Clock + * @package JeckelLab\Clock\CodeceptionHelper + */ +class Clock extends Module +{ + /** + * @var array + */ + protected $requiredFields = ['fake_time_path']; + + /** + * Expected format 'Y-m-d H:i:s' + * @Given current date and time is :currentDate + * @param string $currentDate + */ + public function currentDateAndTimeIs(string $currentDate): void + { + /** @psalm-suppress MixedArrayAccess */ + $fullPath = Configuration::projectDir() . ((string) $this->config['fake_time_path']); + + $dir = dirname($fullPath); + if (!is_dir($dir) && !mkdir($dir) && !is_dir($dir)) { + throw new RuntimeException(sprintf('Directory "%s" was not created', $dir)); + } + file_put_contents($fullPath, $currentDate); + } +}