From 0c26922e4b0802d0d2ce43dc323a4a7eef7fd784 Mon Sep 17 00:00:00 2001 From: Renaud Date: Mon, 22 Jul 2024 17:01:09 +0200 Subject: [PATCH] tests: Improve coverage --- src/FlatMapper.php | 10 +-- tests/FlatMapperCreateMappingTest.php | 89 +++++++++++++++++++++++++++ tests/FlatMapperTest.php | 79 +++++------------------- 3 files changed, 107 insertions(+), 71 deletions(-) create mode 100644 tests/FlatMapperCreateMappingTest.php diff --git a/src/FlatMapper.php b/src/FlatMapper.php index 4704aa2..44f787b 100644 --- a/src/FlatMapper.php +++ b/src/FlatMapper.php @@ -145,7 +145,7 @@ private function createMappingRecursive(string $dtoClassName, array& $objectIden public function map(string $dtoClassName, iterable $data): array { $this->createMapping($dtoClassName); - + $objectsMap = []; $referencesMap = []; foreach ($data as $row) { @@ -206,12 +206,8 @@ private function linkObjects(array $referencesMap, array $objectsMap): void foreach ($references as $identifier => $foreignObjects) { foreach ($foreignObjects as $mappedProperty => $foreignObjectIdentifiers) { if (isset($objectsMap[$objectClass][$identifier])) { - try { - $reflectionClass = new ReflectionClass($objectClass); - $arrayProperty = $reflectionClass->getProperty($mappedProperty); - } catch (\ReflectionException $e) { - throw new MappingException($e->getMessage(), $e->getCode(), $e); - } + $reflectionClass = new ReflectionClass($objectClass); + $arrayProperty = $reflectionClass->getProperty($mappedProperty); $arrayProperty->setValue($objectsMap[$objectClass][$identifier], $foreignObjectIdentifiers); } } diff --git a/tests/FlatMapperCreateMappingTest.php b/tests/FlatMapperCreateMappingTest.php new file mode 100644 index 0000000..969326d --- /dev/null +++ b/tests/FlatMapperCreateMappingTest.php @@ -0,0 +1,89 @@ +expectNotToPerformAssertions(); + (new FlatMapper())->createMapping(ColumnArrayDTO::class); + (new FlatMapper())->createMapping(AuthorDTO::class); + } + + public function testCreateMappingWithCacheServiceDoesNotAssert(): void + { + $flatMapper = new FlatMapper(); + $reflectionMethod = (new \ReflectionClass(FlatMapper::class))->getMethod('createMappingRecursive'); + $reflectionMethod->setAccessible(true); + + $cacheInterface = $this->createMock(CacheInterface::class); + $cacheInterface->expects($this->once())->method('get')->willReturn( + $reflectionMethod->invoke($flatMapper, AuthorDTO::class) + ); + + $flatMapper->setCacheService($cacheInterface); + $flatMapper->createMapping(AuthorDTO::class); + } + + public function testCreateMappingWrongClassNameAsserts(): void + { + $this->expectException(MappingCreationException::class); + $this->expectExceptionMessageMatches("/An error occurred during mapping creation: ThisIsNotAValidClassString is not a valid class name/"); + (new FlatMapper())->createMapping('ThisIsNotAValidClassString'); + } + + public function testCreateMappingWithSeveralIdenticalIdentifiersAsserts(): void + { + $this->expectException(MappingCreationException::class); + $this->expectExceptionMessageMatches("/Several data identifiers are identical/"); + (new FlatMapper())->createMapping(InvalidRootDTO::class); + } + + public function testCreateMappingWithTooManyIdentifiersAsserts(): void + { + $this->expectException(MappingCreationException::class); + $this->expectExceptionMessageMatches("/does not contain exactly one #\[Identifier\] attribute/"); + (new FlatMapper())->createMapping(RootDTOWithTooManyIdentifiers::class); + } + + public function testCreateMappingWithNoIdentifierAsserts(): void + { + $this->expectException(MappingCreationException::class); + $this->expectExceptionMessageMatches("/does not contain exactly one #\[Identifier\] attribute/"); + (new FlatMapper())->createMapping(RootDTOWithNoIdentifier::class); + } + + public function testCreateMappingWithNoConstructorAsserts(): void + { + $this->expectException(MappingCreationException::class); + $this->expectExceptionMessageMatches("/does not have a constructor/"); + (new FlatMapper())->createMapping(RootDTOWithoutConstructor::class); + } + + public function testCreateMappingWithEmptyClassIdentifierAsserts(): void + { + $this->expectException(MappingCreationException::class); + $this->expectExceptionMessageMatches("/The Identifier attribute cannot be used without a property name when used as a Class attribute/"); + (new FlatMapper())->createMapping(RootDTOWithEmptyClassIdentifier::class); + } +} diff --git a/tests/FlatMapperTest.php b/tests/FlatMapperTest.php index 84d458a..feb9940 100644 --- a/tests/FlatMapperTest.php +++ b/tests/FlatMapperTest.php @@ -5,14 +5,8 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; -use Pixelshaped\FlatMapperBundle\Exception\MappingCreationException; use Pixelshaped\FlatMapperBundle\Exception\MappingException; use Pixelshaped\FlatMapperBundle\FlatMapper; -use Pixelshaped\FlatMapperBundle\Tests\Examples\Invalid\RootDTO as InvalidRootDTO; -use Pixelshaped\FlatMapperBundle\Tests\Examples\Invalid\RootDTOWithEmptyClassIdentifier; -use Pixelshaped\FlatMapperBundle\Tests\Examples\Invalid\RootDTOWithNoIdentifier; -use Pixelshaped\FlatMapperBundle\Tests\Examples\Invalid\RootDTOWithoutConstructor; -use Pixelshaped\FlatMapperBundle\Tests\Examples\Invalid\RootDTOWithTooManyIdentifiers; use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\ColumnArray\ColumnArrayDTO; use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\Complex\CustomerDTO; use Pixelshaped\FlatMapperBundle\Tests\Examples\Valid\Complex\InvoiceDTO; @@ -23,65 +17,8 @@ #[CoversClass(FlatMapper::class)] #[CoversClass(MappingException::class)] -#[CoversClass(MappingCreationException::class)] class FlatMapperTest extends TestCase { - public function testCreateMappingWithValidDTOsDoesNotAssert(): void - { - $this->expectNotToPerformAssertions(); - $mapper = new FlatMapper(); - $mapper->createMapping(ColumnArrayDTO::class); - $mapper->createMapping(AuthorDTO::class); - } - - public function testCreateMappingWrongClassNameAsserts(): void - { - $this->expectException(MappingCreationException::class); - $this->expectExceptionMessageMatches("/An error occurred during mapping creation: ThisIsNotAValidClassString is not a valid class name/"); - $mapper = new FlatMapper(); - $mapper->createMapping('ThisIsNotAValidClassString'); - } - - public function testCreateMappingWithSeveralIdenticalIdentifiersAsserts(): void - { - $this->expectException(MappingCreationException::class); - $this->expectExceptionMessageMatches("/Several data identifiers are identical/"); - $mapper = new FlatMapper(); - $mapper->createMapping(InvalidRootDTO::class); - } - - public function testCreateMappingWithTooManyIdentifiersAsserts(): void - { - $this->expectException(MappingCreationException::class); - $this->expectExceptionMessageMatches("/does not contain exactly one #\[Identifier\] attribute/"); - $mapper = new FlatMapper(); - $mapper->createMapping(RootDTOWithTooManyIdentifiers::class); - } - - public function testCreateMappingWithNoIdentifierAsserts(): void - { - $this->expectException(MappingCreationException::class); - $this->expectExceptionMessageMatches("/does not contain exactly one #\[Identifier\] attribute/"); - $mapper = new FlatMapper(); - $mapper->createMapping(RootDTOWithNoIdentifier::class); - } - - public function testCreateMappingWithNoConstructorAsserts(): void - { - $this->expectException(MappingCreationException::class); - $this->expectExceptionMessageMatches("/does not have a constructor/"); - $mapper = new FlatMapper(); - $mapper->createMapping(RootDTOWithoutConstructor::class); - } - - public function testCreateMappingWithEmptyClassIdentifierAsserts(): void - { - $this->expectException(MappingCreationException::class); - $this->expectExceptionMessageMatches("/The Identifier attribute cannot be used without a property name when used as a Class attribute/"); - $mapper = new FlatMapper(); - $mapper->createMapping(RootDTOWithEmptyClassIdentifier::class); - } - public function testMappingDataWithMissingIdentifierPropertyAsserts(): void { $this->expectException(MappingException::class); @@ -138,6 +75,20 @@ public function testMappingDataWithMissingPropertyAsserts(): void ((new FlatMapper())->map(AuthorDTO::class, $results)); } + public function testMappingDataWithBadConstructorTypeAsserts(): void + { + $this->expectException(MappingException::class); + $this->expectExceptionMessageMatches('/An error occurred during mapping: Cannot construct object/'); + + $results = [ + ['author_id' => 1, 'author_name' => 'Alice Brian', 'book_id' => 1, 'book_name' => null, 'book_publisher_name' => 'TravelBooks'], + ['author_id' => 1, 'author_name' => 'Alice Brian', 'book_id' => 2, 'book_name' => 'My journeys', 'book_publisher_name' => 'Lorem Press'], + ['author_id' => 2, 'author_name' => 'Bob Schmo', 'book_id' => 1, 'book_name' => null, 'book_publisher_name' => 'TravelBooks'], + ]; + + ((new FlatMapper())->map(AuthorDTO::class, $results)); + } + public function testMapValidNestedDTOs(): void { $results = [ @@ -271,4 +222,4 @@ public function testMapEmptyData(): void var_export([], true) ); } -} \ No newline at end of file +}