|
19 | 19 | use Patchlevel\Hydrator\Tests\Unit\Fixture\Email; |
20 | 20 | use Patchlevel\Hydrator\Tests\Unit\Fixture\PersonalDataProfileCreated; |
21 | 21 | use Patchlevel\Hydrator\Tests\Unit\Fixture\PersonalDataProfileCreatedFallbackCallback; |
| 22 | +use Patchlevel\Hydrator\Tests\Unit\Fixture\PersonalDataWithStringableSubjectId; |
| 23 | +use Patchlevel\Hydrator\Tests\Unit\Fixture\StringableSubjectId; |
22 | 24 | use PHPUnit\Framework\TestCase; |
23 | 25 |
|
24 | 26 | /** @covers \Patchlevel\Hydrator\Cryptography\PersonalDataPayloadCryptographer */ |
@@ -405,6 +407,41 @@ public function testMissingSubjectId(): void |
405 | 407 | $cryptographer->decrypt($this->metadata(PersonalDataProfileCreated::class), ['email' => 'encrypted']); |
406 | 408 | } |
407 | 409 |
|
| 410 | + public function testStringableSubjectId(): void |
| 411 | + { |
| 412 | + $cipherKey = new CipherKey( |
| 413 | + 'user-123', |
| 414 | + 'bar', |
| 415 | + 'baz', |
| 416 | + ); |
| 417 | + |
| 418 | + $cipherKeyStore = $this->createMock(CipherKeyStore::class); |
| 419 | + $cipherKeyStore->method('get')->willThrowException(new CipherKeyNotExists('user-123')); |
| 420 | + $cipherKeyStore->expects($this->once())->method('store')->with('user-123', $cipherKey); |
| 421 | + |
| 422 | + $cipherKeyFactory = $this->createMock(CipherKeyFactory::class); |
| 423 | + $cipherKeyFactory->expects($this->once())->method('__invoke')->willReturn($cipherKey); |
| 424 | + |
| 425 | + $cipher = $this->createMock(Cipher::class); |
| 426 | + $cipher->expects($this->once())->method('encrypt')->with($cipherKey, 'John Doe') |
| 427 | + ->willReturn('encrypted'); |
| 428 | + |
| 429 | + $cryptographer = new PersonalDataPayloadCryptographer( |
| 430 | + $cipherKeyStore, |
| 431 | + $cipherKeyFactory, |
| 432 | + $cipher, |
| 433 | + ); |
| 434 | + |
| 435 | + $subjectId = new StringableSubjectId('user-123'); |
| 436 | + |
| 437 | + $result = $cryptographer->encrypt( |
| 438 | + $this->metadata(PersonalDataWithStringableSubjectId::class), |
| 439 | + ['subjectId' => $subjectId, 'name' => 'John Doe'], |
| 440 | + ); |
| 441 | + |
| 442 | + self::assertEquals(['subjectId' => $subjectId, 'name' => 'encrypted'], $result); |
| 443 | + } |
| 444 | + |
408 | 445 | public function testCreateWithOpenssl(): void |
409 | 446 | { |
410 | 447 | $cipherKeyStore = $this->createMock(CipherKeyStore::class); |
|
0 commit comments