|
2 | 2 |
|
3 | 3 | namespace TinyBlocks\Ksuid; |
4 | 4 |
|
| 5 | +use Exception; |
5 | 6 | use PHPUnit\Framework\TestCase; |
| 7 | +use TinyBlocks\Ksuid\Internal\Exceptions\InvalidKsuidForInspection; |
6 | 8 |
|
7 | 9 | class KsuidTest extends TestCase |
8 | 10 | { |
@@ -59,4 +61,52 @@ public function testFromPayloadAndTimestamp(): void |
59 | 61 | self::assertEquals(20, strlen($ksuid->getBytes())); |
60 | 62 | self::assertEquals(Ksuid::ENCODED_SIZE, strlen($ksuid->getValue())); |
61 | 63 | } |
| 64 | + |
| 65 | + /** |
| 66 | + * @dataProvider providerForTestInspectFrom |
| 67 | + */ |
| 68 | + public function testInspectFrom(string $ksuid, array $expected): void |
| 69 | + { |
| 70 | + $actual = Ksuid::inspectFrom(ksuid: $ksuid); |
| 71 | + |
| 72 | + self::assertEquals($expected, $actual); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @throws Exception |
| 77 | + */ |
| 78 | + public function testExceptionWhenInvalidKsuidForInspection(): void |
| 79 | + { |
| 80 | + /** @Given a invalid KSUID */ |
| 81 | + $ksuid = random_bytes(5); |
| 82 | + $template = 'The KSUID <%s> is not valid for inspection.'; |
| 83 | + |
| 84 | + /** @Then an exception indicating that KSUID is invalid for inspection should occur */ |
| 85 | + $this->expectException(InvalidKsuidForInspection::class); |
| 86 | + $this->expectExceptionMessage(sprintf($template, $ksuid)); |
| 87 | + |
| 88 | + Ksuid::inspectFrom(ksuid: $ksuid); |
| 89 | + } |
| 90 | + |
| 91 | + public function providerForTestInspectFrom(): array |
| 92 | + { |
| 93 | + return [ |
| 94 | + [ |
| 95 | + 'ksuid' => '2QzPUGEaAKHhVcQYrqQodbiZat1', |
| 96 | + 'expected' => [ |
| 97 | + 'time' => '2023-06-09 23:30:50 +0000 GMT+0000', |
| 98 | + 'payload' => '464932c1194da98e752145d72b8f0aab', |
| 99 | + 'timestamp' => 286353450 |
| 100 | + ] |
| 101 | + ], |
| 102 | + [ |
| 103 | + 'ksuid' => '0ujzPyRiIAffKhBux4PvQdDqMHY', |
| 104 | + 'expected' => [ |
| 105 | + 'time' => '2017-10-10 04:46:20 +0000 GMT+0000', |
| 106 | + 'payload' => '73fc1aa3b2446246d6e89fcd909e8fe8', |
| 107 | + 'timestamp' => 107610780 |
| 108 | + ] |
| 109 | + ] |
| 110 | + ]; |
| 111 | + } |
62 | 112 | } |
0 commit comments