Skip to content

Commit

Permalink
Change DateTime add to use DateInterval, improved DateTimeHelper sema…
Browse files Browse the repository at this point in the history
…ntics

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
  • Loading branch information
DRvanR committed Feb 10, 2015
1 parent b118a0c commit e1d7f77
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/Surfnet/Stepup/DateTime/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Surfnet/Stepup/Identity/Entity/UnverifiedSecondFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')));
}

/**
Expand All @@ -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': " .
Expand Down
4 changes: 2 additions & 2 deletions src/Surfnet/Stepup/Identity/Entity/VerifiedSecondFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}

/**
Expand All @@ -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.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\CommandHandler\IdentityCommandHandler;
use Surfnet\StepupMiddleware\CommandHandlingBundle\Tests\DateTimeHelper;

/**
* @runTestsInSeparateProcesses
*/
class IdentityCommandHandlerTest extends CommandHandlerTest
{
/** @var MockInterface */
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit e1d7f77

Please sign in to comment.