diff --git a/README.md b/README.md index 39c9abbaf..ecc037190 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,23 @@ Step-up Middleware ## Installation Clone the repository or download the archive to a directory. Install the dependencies by running `composer install` and fill out the database credentials et cetera. + +## Notes + +### Mocking Broadway DateTime::now() + +To help with mocking time, the helper `BroadwayFixedDateTimeNow` was created. Call `::enable(DateTime)` to set a fixed +date/time, and call `::disable()` to… disable it. It is recommended to run a tests in a separate process (see +`IdentityCommandHandlerTest::testAYubikeyPossessionCanBeProven()`) when using this helper so the mock doesn't persist +between tests. + +```php +/** @runTestInSeparateProcess */ +public function testItWorks() +{ + # Trick `DateTime::now()` into thinking it is 1970. + BroadwayFixedDateTimeNow::enable(new \DateTime('@0')); + + $this->assertEquals('1970-01-01T00:00:00.000000+00:00', \Broadway\Domain\DateTime::now()->toString()); +} +``` diff --git a/composer.json b/composer.json index c1931ae24..b3851abfb 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ } }, "autoload-dev": { - "classmap": ["src/Surfnet/StepupMiddleware/ApiBundle/Tests/Request/commands.php"] + "classmap": ["src/Surfnet/StepupMiddleware/ApiBundle/Tests/Request/commands.php"], + "files": ["src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/microtime.php"] }, "minimum-stability": "stable", "repositories": [{"type":"vcs","url":"https://github.com/rjkip/symfony"}], diff --git a/composer.lock b/composer.lock index 18a4a2989..d30e63d56 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "7d98cf9fdc16ef2615866095daaa8ff3", + "hash": "34a25fb58407cbf57d8912d9ed62196f", "packages": [ { "name": "beberlei/assert", diff --git a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/BroadwayFixedDateTimeNow.php b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/BroadwayFixedDateTimeNow.php new file mode 100644 index 000000000..779b547b8 --- /dev/null +++ b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/BroadwayFixedDateTimeNow.php @@ -0,0 +1,53 @@ +format('U.u'); + } else { + return self::$fixedReturnDateTime->format('0.u00 U'); + } + } else { + return \microtime($getAsFloat); + } + } +} diff --git a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/BroadwayFixedDateTimeNowTest.php b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/BroadwayFixedDateTimeNowTest.php new file mode 100644 index 000000000..f879e956f --- /dev/null +++ b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/BroadwayFixedDateTimeNowTest.php @@ -0,0 +1,59 @@ +assertEquals(12345.0, microtime(true)); + $this->assertEquals('0.00000000 12345', microtime()); + } + + public function testItWorksWithSeparateProcesses() + { + $this->assertInternalType('float', microtime(true)); + $this->assertInternalType('string', microtime()); + } + + public function testItCanBeDisabledInTheSameProcess() + { + BroadwayFixedDateTimeNow::enable(new \DateTime('@12345')); + $this->assertEquals(12345.0, microtime(true)); + + BroadwayFixedDateTimeNow::disable(); + $this->assertNotEquals(12345.0, microtime(true)); + $this->assertInternalType('float', microtime(true)); + $this->assertInternalType('string', microtime()); + } + + public function testWeAreVisiting1970() + { + BroadwayFixedDateTimeNow::enable(new \DateTime('@0')); + + $this->assertEquals('1970-01-01T00:00:00.000000+00:00', DateTime::now()->toString()); + } +} diff --git a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/microtime.php b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/microtime.php new file mode 100644 index 000000000..c517e34ca --- /dev/null +++ b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/microtime.php @@ -0,0 +1,26 @@ +