|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SnapAuth; |
| 6 | + |
| 7 | +use DateTimeImmutable; |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\Attributes\Small; |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +use function assert; |
| 14 | +use function file_get_contents; |
| 15 | +use function is_array; |
| 16 | +use function json_decode; |
| 17 | +use function sprintf; |
| 18 | + |
| 19 | +use const JSON_THROW_ON_ERROR; |
| 20 | + |
| 21 | +#[CoversClass(Credential::class)] |
| 22 | +#[Small] |
| 23 | +class CredentialTest extends TestCase |
| 24 | +{ |
| 25 | + public function testDecodingFromApiResponse(): void |
| 26 | + { |
| 27 | + $data = $this->readFixture('credential1.json'); |
| 28 | + $cred = new Credential($data); |
| 29 | + |
| 30 | + self::assertSame('ctl_2893f2Vg86463c8xV7wVv5PG', $cred->id); |
| 31 | + self::assertSame('fbfc3007-154e-4ecc-8c0b-6e020557d7bd', $cred->aaguid); |
| 32 | + self::assertTrue($cred->isActive); |
| 33 | + self::assertTrue($cred->isBackedUp); |
| 34 | + self::assertTrue($cred->isBackupEligible); |
| 35 | + self::assertTrue($cred->isUvInitialized); |
| 36 | + self::assertSame('iCloud Keychain', $cred->name); |
| 37 | + self::assertSame([ |
| 38 | + WebAuthn\AuthenticatorTransport::Hybrid, |
| 39 | + WebAuthn\AuthenticatorTransport::Internal, |
| 40 | + ], $cred->transports); |
| 41 | + self::assertEquals(new DateTimeImmutable('2024-03-07T20:02:04Z'), $cred->createdAt); |
| 42 | + } |
| 43 | + |
| 44 | + public function testDecodingUsbFromApiResponse(): void |
| 45 | + { |
| 46 | + $data = $this->readFixture('credential2.json'); |
| 47 | + $cred = new Credential($data); |
| 48 | + |
| 49 | + self::assertSame('ctl_28CWCw4G3R4MGCg2cc2ccvGr', $cred->id); |
| 50 | + self::assertSame('00000000-0000-0000-0000-000000000000', $cred->aaguid); |
| 51 | + self::assertTrue($cred->isActive); |
| 52 | + self::assertFalse($cred->isBackedUp); |
| 53 | + self::assertFalse($cred->isBackupEligible); |
| 54 | + self::assertFalse($cred->isUvInitialized); |
| 55 | + self::assertSame('Passkey', $cred->name); |
| 56 | + self::assertSame([WebAuthn\AuthenticatorTransport::Usb], $cred->transports); |
| 57 | + self::assertEquals(new DateTimeImmutable('2024-08-05T21:35:48Z'), $cred->createdAt); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return mixed[] |
| 62 | + */ |
| 63 | + private function readFixture(string $path): array |
| 64 | + { |
| 65 | + $path = sprintf('%s/%s/%s', __DIR__, 'fixtures', $path); |
| 66 | + $json = file_get_contents($path); |
| 67 | + assert($json !== false); |
| 68 | + $data = json_decode($json, true, flags: JSON_THROW_ON_ERROR); |
| 69 | + assert(is_array($data)); |
| 70 | + return $data; |
| 71 | + } |
| 72 | +} |
0 commit comments