ITimeProviderTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Tests\Providers\Time;
  3. use PHPUnit\Framework\TestCase;
  4. use Tests\MightNotMakeAssertions;
  5. use RobThree\Auth\TwoFactorAuthException;
  6. use RobThree\Auth\TwoFactorAuth;
  7. class ITimeProviderTest extends TestCase
  8. {
  9. use MightNotMakeAssertions;
  10. /**
  11. * @return void
  12. */
  13. public function testEnsureCorrectTimeDoesNotThrowForCorrectTime()
  14. {
  15. $tpr1 = new TestTimeProvider(123);
  16. $tpr2 = new TestTimeProvider(128);
  17. $tfa = new TwoFactorAuth('Test', 6, 30, 'sha1', null, null, $tpr1);
  18. $tfa->ensureCorrectTime(array($tpr2)); // 128 - 123 = 5 => within default leniency
  19. $this->noAssertionsMade();
  20. }
  21. /**
  22. * @return void
  23. */
  24. public function testEnsureCorrectTimeThrowsOnIncorrectTime()
  25. {
  26. $tpr1 = new TestTimeProvider(123);
  27. $tpr2 = new TestTimeProvider(124);
  28. $tfa = new TwoFactorAuth('Test', 6, 30, 'sha1', null, null, $tpr1);
  29. $this->expectException(TwoFactorAuthException::class);
  30. $tfa->ensureCorrectTime(array($tpr2), 0); // We force a leniency of 0, 124-123 = 1 so this should throw
  31. }
  32. /**
  33. * @return void
  34. */
  35. public function testEnsureDefaultTimeProviderReturnsCorrectTime()
  36. {
  37. $tfa = new TwoFactorAuth('Test', 6, 30, 'sha1');
  38. $tfa->ensureCorrectTime(array(new TestTimeProvider(time())), 1); // Use a leniency of 1, should the time change between both time() calls
  39. $this->noAssertionsMade();
  40. }
  41. }