Skip to content

Commit

Permalink
Merge pull request #43 from patchlevel/fix-subject-id-with-extends
Browse files Browse the repository at this point in the history
fix subject id with extends
  • Loading branch information
DavidBadura authored Apr 11, 2024
2 parents 0d5518e + a69845c commit 2797c42
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Metadata/AttributeMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,20 @@ private function mergeMetadata(ClassMetadata $parent, ClassMetadata $child): Cla
$properties[$property->fieldName()] = $property;
}

$parentDataSubjectIdField = $parent->dataSubjectIdField();
$childDataSubjectIdField = $child->dataSubjectIdField();

if ($parentDataSubjectIdField !== null && $childDataSubjectIdField !== null) {
$parentProperty = $parent->propertyForField($parentDataSubjectIdField);
$childProperty = $child->propertyForField($childDataSubjectIdField);

throw new MultipleDataSubjectId($parentProperty->propertyName(), $childProperty->propertyName());
}

return new ClassMetadata(
$parent->reflection(),
array_values($properties),
$parentDataSubjectIdField ?? $childDataSubjectIdField,
);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Fixture/ChildWithPersonalDataDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

use Patchlevel\Hydrator\Attribute\PersonalData;

abstract class ChildWithPersonalDataDto
{
public function __construct(
#[EmailNormalizer]
#[PersonalData]
private Email $email,
) {
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Fixture/ParentWithPersonalDataDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Patchlevel\Hydrator\Tests\Unit\Fixture;

use Patchlevel\Hydrator\Attribute\DataSubjectId;

final class ParentWithPersonalDataDto extends ChildWithPersonalDataDto
{
public function __construct(
#[ProfileIdNormalizer]
#[DataSubjectId]
public ProfileId $profileId,
Email $email,
) {
parent::__construct($email);
}
}
24 changes: 24 additions & 0 deletions tests/Unit/Metadata/AttributeMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Patchlevel\Hydrator\Tests\Unit\Fixture\IgnoreParentDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\MissingSubjectIdDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\ParentDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\ParentWithPersonalDataDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileIdNormalizer;
use Patchlevel\Hydrator\Tests\Unit\Fixture\Status;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -334,4 +335,27 @@ public function __construct(
$metadataFactory = new AttributeMetadataFactory();
$metadataFactory->metadata($event::class);
}

public function testExtendsWithPersonalData(): void
{
$metadataFactory = new AttributeMetadataFactory();
$metadata = $metadataFactory->metadata(ParentWithPersonalDataDto::class);

self::assertSame('profileId', $metadata->dataSubjectIdField());
self::assertCount(2, $metadata->properties());

$idPropertyMetadata = $metadata->propertyForField('profileId');

self::assertSame('profileId', $idPropertyMetadata->propertyName());
self::assertSame('profileId', $idPropertyMetadata->fieldName());
self::assertFalse($idPropertyMetadata->isPersonalData());
self::assertInstanceOf(ProfileIdNormalizer::class, $idPropertyMetadata->normalizer());

$emailPropertyMetadata = $metadata->propertyForField('email');

self::assertSame('email', $emailPropertyMetadata->propertyName());
self::assertSame('email', $emailPropertyMetadata->fieldName());
self::assertTrue($emailPropertyMetadata->isPersonalData());
self::assertInstanceOf(EmailNormalizer::class, $emailPropertyMetadata->normalizer());
}
}

0 comments on commit 2797c42

Please sign in to comment.