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