Skip to content

Commit

Permalink
Validate mapping if flag is set
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixelshaped committed Jun 3, 2024
1 parent 6343024 commit 059a8e0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
28 changes: 15 additions & 13 deletions src/FlatMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FlatMapper

// TODO for now those are unused
private ?CacheInterface $cacheService = null; // @phpstan-ignore-line
private bool $validateMapping = true; // @phpstan-ignore-line
private bool $validateMapping = true;

public function setCacheService(CacheInterface $cacheService): void
{
Expand Down Expand Up @@ -57,26 +57,22 @@ private function createMappingRecursive(string $dtoClassName, string $rootDtoCla

$reflectionClass = new ReflectionClass($dtoClassName);

foreach ($reflectionClass->getProperties() as $reflectionProperty) {
$identifiersCount = 0;
foreach ($reflectionClass->getConstructor()->getParameters() as $reflectionProperty) {

Check failure on line 61 in src/FlatMapper.php

View workflow job for this annotation

GitHub Actions / symfony-tests

Cannot call method getParameters() on ReflectionMethod|null.
$propertyName = $reflectionProperty->getName();
$isIdentifier = false;
foreach ($reflectionProperty->getAttributes() as $attribute) {
if ($attribute->getName() === ReferencesArray::class) {
$this->objectsMapping[$rootDtoClassName][$dtoClassName][$propertyName] = (string)$attribute->getArguments()[0];
$this->createMappingRecursive($attribute->getArguments()[0], $rootDtoClassName);
continue 2;
}

if ($attribute->getName() === ColumnArray::class) {
} else if ($attribute->getName() === ColumnArray::class) {
$this->objectsMapping[$rootDtoClassName][$dtoClassName][$propertyName] = (string)$attribute->getArguments()[0];
continue 2;
}

if ($attribute->getName() === Identifier::class) {
} else if ($attribute->getName() === Identifier::class) {
$identifiersCount++;
$isIdentifier = true;
}

if ($attribute->getName() === InboundPropertyName::class) {
} else if ($attribute->getName() === InboundPropertyName::class) {
$propertyName = $attribute->getArguments()[0];
}
}
Expand All @@ -88,8 +84,14 @@ private function createMappingRecursive(string $dtoClassName, string $rootDtoCla
$this->objectsMapping[$rootDtoClassName][$dtoClassName][$propertyName] = null;
}

if (count($this->objectIdentifiers[$rootDtoClassName]) !== count(array_unique($this->objectIdentifiers[$rootDtoClassName]))) {
throw new RuntimeException('Several data identifiers are identical: ' . print_r($this->objectIdentifiers[$rootDtoClassName], true));
if($this->validateMapping) {
if($identifiersCount !== 1) {
throw new RuntimeException($dtoClassName.' contains more than one #[Identifier] attribute.');
}

if (count($this->objectIdentifiers[$rootDtoClassName]) !== count(array_unique($this->objectIdentifiers[$rootDtoClassName]))) {
throw new RuntimeException('Several data identifiers are identical: ' . print_r($this->objectIdentifiers[$rootDtoClassName], true));
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Examples/Invalid/RootDTOWithTooManyIdentifiers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace Pixelshaped\FlatMapperBundle\Tests\Examples\Invalid;

use Pixelshaped\FlatMapperBundle\Attributes\Identifier;
use Pixelshaped\FlatMapperBundle\Attributes\InboundPropertyName;
use Pixelshaped\FlatMapperBundle\Attributes\ReferencesArray;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Invalid\LeafDTO;

class RootDTOWithTooManyIdentifiers
{
/**
* @param array<LeafDTO> $leafs
*/
public function __construct(
#[Identifier]
#[InboundPropertyName('object1_id')]
public int $id,
#[Identifier]
#[InboundPropertyName('object1_name')]
public string $name,
#[ReferencesArray(LeafDTO::class)]
public array $leafs,
) {}
}
11 changes: 10 additions & 1 deletion tests/FlatMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\TestCase;
use Pixelshaped\FlatMapperBundle\FlatMapper;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Invalid\RootDTOWithTooManyIdentifiers;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\ColumnArrayDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\LeafDTO;
use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\RootDTO as ValidRootDTO;
Expand All @@ -24,14 +25,22 @@ public function testCreateMappingWithValidDTOsDoesNotAssert(): void
$mapper->createMapping(ValidRootDTO::class);
}

public function testCreateMappingWithInvalidDTOsAsserts(): void
public function testCreateMappingWithSeveralIdenticalIdentifiersAsserts(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches("/Several data identifiers are identical/");
$mapper = new FlatMapper();
$mapper->createMapping(InvalidRootDTO::class);
}

public function testCreateMappingWithTooManyIdentifiersAsserts(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessageMatches("/contains more than one #\[Identifier\] attribute/");
$mapper = new FlatMapper();
$mapper->createMapping(RootDTOWithTooManyIdentifiers::class);
}

public function testMapValidNestedDTOs(): void
{
$results = [
Expand Down

0 comments on commit 059a8e0

Please sign in to comment.