From e1d7f77530d9af3ede52cfcbe5799a9bbc550f2b Mon Sep 17 00:00:00 2001 From: DRvanR Date: Tue, 10 Feb 2015 10:33:59 +0100 Subject: [PATCH] Change DateTime add to use DateInterval, improved DateTimeHelper semantics Also ensured that all tests that use the DataTimeHelper to influence the current time run in isolation, as not doing this may lead to weird behavior in other tests --- src/Surfnet/Stepup/DateTime/DateTime.php | 6 +++--- .../Stepup/Identity/Entity/UnverifiedSecondFactor.php | 5 +++-- .../Stepup/Identity/Entity/VerifiedSecondFactor.php | 4 ++-- .../CommandHandlingBundle/Tests/DateTimeHelper.php | 2 +- .../CommandHandlingBundle/Tests/DateTimeHelperTest.php | 6 +++--- .../CommandHandler/IdentityCommandHandlerTest.php | 9 ++++++--- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Surfnet/Stepup/DateTime/DateTime.php b/src/Surfnet/Stepup/DateTime/DateTime.php index 5ef0b245a..4c1e4f85b 100644 --- a/src/Surfnet/Stepup/DateTime/DateTime.php +++ b/src/Surfnet/Stepup/DateTime/DateTime.php @@ -78,13 +78,13 @@ public function __construct(CoreDateTime $dateTime = null) } /** - * @param string $intervalSpec + * @param DateInterval $intervalSpec * @return DateTime */ - public function add($intervalSpec) + public function add(DateInterval $intervalSpec) { $dateTime = clone $this->dateTime; - $dateTime->add(new DateInterval($intervalSpec)); + $dateTime->add($intervalSpec); return new self($dateTime); } diff --git a/src/Surfnet/Stepup/Identity/Entity/UnverifiedSecondFactor.php b/src/Surfnet/Stepup/Identity/Entity/UnverifiedSecondFactor.php index 8bc9d1a7f..f6103064f 100644 --- a/src/Surfnet/Stepup/Identity/Entity/UnverifiedSecondFactor.php +++ b/src/Surfnet/Stepup/Identity/Entity/UnverifiedSecondFactor.php @@ -19,6 +19,7 @@ namespace Surfnet\Stepup\Identity\Entity; use Broadway\EventSourcing\EventSourcedEntity; +use DateInterval; use Surfnet\Stepup\DateTime\DateTime; use Surfnet\Stepup\Exception\DomainException; use Surfnet\Stepup\Exception\InvalidArgumentException; @@ -123,7 +124,7 @@ public function getId() public function wouldVerifyEmail($verificationNonce) { return $this->verificationNonce === $verificationNonce - && !DateTime::now()->comesAfter($this->verificationRequestedAt->add('P1D')); + && !DateTime::now()->comesAfter($this->verificationRequestedAt->add(new DateInterval('P1D'))); } /** @@ -140,7 +141,7 @@ public function verifyEmail($verificationNonce) ); } - if (DateTime::now()->comesAfter($this->verificationRequestedAt->add('P1D'))) { + if (DateTime::now()->comesAfter($this->verificationRequestedAt->add(new DateInterval('P1D')))) { throw new DomainException( sprintf( "Cannot verify possession of e-mail for second factor '%s': " . diff --git a/src/Surfnet/Stepup/Identity/Entity/VerifiedSecondFactor.php b/src/Surfnet/Stepup/Identity/Entity/VerifiedSecondFactor.php index f4d51fc5e..49f42cf26 100644 --- a/src/Surfnet/Stepup/Identity/Entity/VerifiedSecondFactor.php +++ b/src/Surfnet/Stepup/Identity/Entity/VerifiedSecondFactor.php @@ -116,7 +116,7 @@ public function wouldBeVettedBy($registrationCode, $secondFactorIdentifier, $doc return strcasecmp($registrationCode, $this->registrationCode) === 0 && $secondFactorIdentifier === $this->secondFactorIdentifier && $identityVerified === true - && !DateTime::now()->comesAfter($this->registrationRequestedAt->add('P14D')); + && !DateTime::now()->comesAfter($this->registrationRequestedAt->add(new \DateInterval('P14D'))); } /** @@ -139,7 +139,7 @@ public function vet($registrationCode, $secondFactorIdentifier, $documentNumber, throw new DomainException("Cannot vet this second factor: real identity wasn't verified by an RA."); } - if (DateTime::now()->comesAfter($this->registrationRequestedAt->add('P14D'))) { + if (DateTime::now()->comesAfter($this->registrationRequestedAt->add(new \DateInterval('P14D')))) { throw new DomainException('Cannot vet this second factor: registration window has closed.'); } diff --git a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/DateTimeHelper.php b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/DateTimeHelper.php index 61cdb747e..3089d4085 100644 --- a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/DateTimeHelper.php +++ b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/DateTimeHelper.php @@ -28,7 +28,7 @@ class DateTimeHelper * * @param DateTime|null $now */ - public static function stubNow(DateTime $now = null) + public static function setCurrentTime(DateTime $now = null) { $nowProperty = new ReflectionProperty('Surfnet\Stepup\DateTime\DateTime', 'now'); $nowProperty->setAccessible(true); diff --git a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/DateTimeHelperTest.php b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/DateTimeHelperTest.php index 0a7d96a7a..ef266ed9f 100644 --- a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/DateTimeHelperTest.php +++ b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/DateTimeHelperTest.php @@ -32,7 +32,7 @@ class DateTimeHelperTest extends \PHPUnit_Framework_TestCase */ public function it_mocks_now() { - DateTimeHelper::stubNow(new DateTime(new CoreDateTime('@12345'))); + DateTimeHelper::setCurrentTime(new DateTime(new CoreDateTime('@12345'))); $this->assertEquals(new DateTime(new CoreDateTime('@12345')), DateTime::now()); } @@ -43,10 +43,10 @@ public function it_mocks_now() */ public function it_can_be_disabled_in_the_same_process() { - DateTimeHelper::stubNow(new DateTime(new CoreDateTime('@12345'))); + DateTimeHelper::setCurrentTime(new DateTime(new CoreDateTime('@12345'))); $this->assertEquals(new DateTime(new CoreDateTime('@12345')), DateTime::now()); - DateTimeHelper::stubNow(null); + DateTimeHelper::setCurrentTime(null); $this->assertTrue((new DateTime())->comesAfterOrIsEqual(DateTime::now())); } diff --git a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/Identity/CommandHandler/IdentityCommandHandlerTest.php b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/Identity/CommandHandler/IdentityCommandHandlerTest.php index 5eb425d8f..aa06df685 100644 --- a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/Identity/CommandHandler/IdentityCommandHandlerTest.php +++ b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/Identity/CommandHandler/IdentityCommandHandlerTest.php @@ -47,6 +47,9 @@ use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\CommandHandler\IdentityCommandHandler; use Surfnet\StepupMiddleware\CommandHandlingBundle\Tests\DateTimeHelper; +/** + * @runTestsInSeparateProcesses + */ class IdentityCommandHandlerTest extends CommandHandlerTest { /** @var MockInterface */ @@ -79,7 +82,7 @@ protected function createCommandHandler(EventStoreInterface $eventStore, EventBu */ public function a_yubikey_possession_can_be_proven() { - DateTimeHelper::stubNow(new DateTime(new CoreDateTime('@12345'))); + DateTimeHelper::setCurrentTime(new DateTime(new CoreDateTime('@12345'))); m::mock('alias:Surfnet\Stepup\Token\TokenGenerator') ->shouldReceive('generateHumanReadableToken')->once()->andReturn('code') @@ -162,7 +165,7 @@ public function yubikey_possession_cannot_be_proven_twice() */ public function a_phone_possession_can_be_proven() { - DateTimeHelper::stubNow(new DateTime(new CoreDateTime('@12345'))); + DateTimeHelper::setCurrentTime(new DateTime(new CoreDateTime('@12345'))); m::mock('alias:Surfnet\Stepup\Token\TokenGenerator') ->shouldReceive('generateHumanReadableToken')->once()->andReturn('code') @@ -285,7 +288,7 @@ public function cannot_prove_possession_of_arbitrary_second_factor_type_twice() */ public function an_unverified_second_factors_email_can_be_verified() { - DateTimeHelper::stubNow(new DateTime(new CoreDateTime('@12345'))); + DateTimeHelper::setCurrentTime(new DateTime(new CoreDateTime('@12345'))); m::mock('alias:Surfnet\Stepup\Token\TokenGenerator') ->shouldReceive('generateHumanReadableToken')->once()->andReturn('regcode');