Skip to content

Commit 3f839d3

Browse files
authored
Merge pull request #113 from wikando-ck/feat/stringable-subject-id
Add support for Stringable as DataSubjectId
2 parents a2f2cc9 + c9d738f commit 3f839d3

4 files changed

Lines changed: 80 additions & 0 deletions

File tree

src/Cryptography/PersonalDataPayloadCryptographer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Patchlevel\Hydrator\Cryptography\Store\CipherKeyStore;
1414
use Patchlevel\Hydrator\Metadata\ClassMetadata;
1515
use Patchlevel\Hydrator\Metadata\PropertyMetadata;
16+
use Stringable;
1617

1718
use function array_key_exists;
1819
use function is_int;
@@ -143,6 +144,10 @@ private function subjectId(ClassMetadata $metadata, array $data): string|null
143144
$subjectId = (string)$subjectId;
144145
}
145146

147+
if ($subjectId instanceof Stringable) {
148+
$subjectId = $subjectId->__toString();
149+
}
150+
146151
if (!is_string($subjectId)) {
147152
throw new UnsupportedSubjectId($metadata->className(), $fieldName, $subjectId);
148153
}

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)