Skip to content

Commit

Permalink
Add ability to mock \Broadway\Domain\DateTime::now().
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinier Kip committed Nov 20, 2014
1 parent defdcfe commit e4563a8
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 2 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
```
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"}],
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* Copyright 2014 SURFnet bv
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Tests;

class BroadwayFixedDateTimeNow
{
/**
* @var DateTime|null
*/
private static $fixedReturnDateTime = null;

/**
* @param \DateTime $fixedReturnDateTime
*/
public static function enable(\DateTime $fixedReturnDateTime)
{
self::$fixedReturnDateTime = $fixedReturnDateTime;
}

public static function disable()
{
self::$fixedReturnDateTime = null;
}

public static function microtime($getAsFloat)
{
if (self::$fixedReturnDateTime) {
if ($getAsFloat) {
return (float) self::$fixedReturnDateTime->format('U.u');
} else {
return self::$fixedReturnDateTime->format('0.u00 U');
}
} else {
return \microtime($getAsFloat);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* Copyright 2014 SURFnet bv
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Broadway\Domain;

use Surfnet\StepupMiddleware\CommandHandlingBundle\Tests\BroadwayFixedDateTimeNow;

/**
* @runTestsInSeparateProcesses
*/
class BroadwayFixedDateTimeNowTest extends \PHPUnit_Framework_TestCase
{
public function testItMocksMicrotime()
{
BroadwayFixedDateTimeNow::enable(new \DateTime('@12345'));

$this->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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Copyright 2014 SURFnet bv
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Broadway\Domain;

use Surfnet\StepupMiddleware\CommandHandlingBundle\Tests\BroadwayFixedDateTimeNow;

function microtime($getAsFloat = false)
{
return BroadwayFixedDateTimeNow::microtime($getAsFloat);
}

0 comments on commit e4563a8

Please sign in to comment.