-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to mock
\Broadway\Domain\DateTime::now()
.
- Loading branch information
Reinier Kip
committed
Nov 20, 2014
1 parent
defdcfe
commit e4563a8
Showing
6 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/BroadwayFixedDateTimeNow.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/BroadwayFixedDateTimeNowTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/microtime.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |