Skip to content

Commit 20f5f99

Browse files
committed
test(cryptography): demonstrate use of Stringable DataSubjectId
1 parent efbbce8 commit 20f5f99

3 files changed

Lines changed: 75 additions & 0 deletions

File tree

tests/Unit/Cryptography/PersonalDataPayloadCryptographerTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Patchlevel\Hydrator\Tests\Unit\Fixture\Email;
2020
use Patchlevel\Hydrator\Tests\Unit\Fixture\PersonalDataProfileCreated;
2121
use Patchlevel\Hydrator\Tests\Unit\Fixture\PersonalDataProfileCreatedFallbackCallback;
22+
use Patchlevel\Hydrator\Tests\Unit\Fixture\PersonalDataWithStringableSubjectId;
23+
use Patchlevel\Hydrator\Tests\Unit\Fixture\StringableSubjectId;
2224
use PHPUnit\Framework\TestCase;
2325

2426
/** @covers \Patchlevel\Hydrator\Cryptography\PersonalDataPayloadCryptographer */
@@ -405,6 +407,41 @@ public function testMissingSubjectId(): void
405407
$cryptographer->decrypt($this->metadata(PersonalDataProfileCreated::class), ['email' => 'encrypted']);
406408
}
407409

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+
408445
public function testCreateWithOpenssl(): void
409446
{
410447
$cipherKeyStore = $this->createMock(CipherKeyStore::class);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\Hydrator\Tests\Unit\Fixture;
6+
7+
use Patchlevel\Hydrator\Attribute\DataSubjectId;
8+
use Patchlevel\Hydrator\Attribute\PersonalData;
9+
10+
class PersonalDataWithStringableSubjectId
11+
{
12+
public function __construct(
13+
#[DataSubjectId]
14+
public StringableSubjectId $subjectId,
15+
#[PersonalData]
16+
public string $name,
17+
) {
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\Hydrator\Tests\Unit\Fixture;
6+
7+
use Stringable;
8+
9+
class StringableSubjectId implements Stringable
10+
{
11+
public function __construct(private string $id)
12+
{
13+
}
14+
15+
public function __toString(): string
16+
{
17+
return $this->id;
18+
}
19+
}

0 commit comments

Comments
 (0)