From 7b0c5a872c1729fa7c619e07f708b99bc1a7108d Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Tue, 2 Apr 2024 18:28:34 +0200 Subject: [PATCH] Some cleanup & improvements in the tests --- .../GenericConverterIntegrationTest.php | 2 - ...enericExtendedConverterIntegrationTest.php | 11 +-- tests/Fixtures/Config/person.yaml | 16 +--- tests/Fixtures/Model/Source/User.php | 8 +- .../Factory/PersonWithDefaultsFactory.php | 22 +++++ tests/Fixtures/Model/Target/Person.php | 12 +++ .../PersonAddressPopulatorIntegrationTest.php | 1 - .../PropertyMappingPopulatorTest.php | 89 +++++++++---------- 8 files changed, 91 insertions(+), 70 deletions(-) create mode 100644 tests/Fixtures/Model/Target/Factory/PersonWithDefaultsFactory.php diff --git a/tests/Converter/GenericConverterIntegrationTest.php b/tests/Converter/GenericConverterIntegrationTest.php index 85756ad..0efd614 100644 --- a/tests/Converter/GenericConverterIntegrationTest.php +++ b/tests/Converter/GenericConverterIntegrationTest.php @@ -10,8 +10,6 @@ use Neusta\ConverterBundle\Tests\Support\Attribute\ConfigureContainer; #[ConfigureContainer(__DIR__ . '/../Fixtures/Config/person.yaml')] -#[ConfigureContainer(__DIR__ . '/../Fixtures/Config/address.yaml')] -#[ConfigureContainer(__DIR__ . '/../Fixtures/Config/contact_numbers.yaml')] class GenericConverterIntegrationTest extends ConfigurableKernelTestCase { public function testConvert(): void diff --git a/tests/Converter/GenericExtendedConverterIntegrationTest.php b/tests/Converter/GenericExtendedConverterIntegrationTest.php index 716489f..2a29838 100644 --- a/tests/Converter/GenericExtendedConverterIntegrationTest.php +++ b/tests/Converter/GenericExtendedConverterIntegrationTest.php @@ -12,11 +12,9 @@ use Neusta\ConverterBundle\Tests\Support\Attribute\ConfigureContainer; #[ConfigureContainer(__DIR__ . '/../Fixtures/Config/person.yaml')] -#[ConfigureContainer(__DIR__ . '/../Fixtures/Config/address.yaml')] -#[ConfigureContainer(__DIR__ . '/../Fixtures/Config/contact_numbers.yaml')] class GenericExtendedConverterIntegrationTest extends ConfigurableKernelTestCase { - public function testConvert_with_null_safety_property(): void + public function test_convert_with_skip_null(): void { /** @var Converter $converter */ $converter = self::getContainer()->get('test.person.converter.extended'); @@ -24,12 +22,15 @@ public function testConvert_with_null_safety_property(): void // Test Fixture $source = (new User()) ->setFullName(null) - ->setAgeInYears(null); + ->setAgeInYears(null) + ->setEmail(null); // Test Execution $target = $converter->convert($source); // Test Assertion - self::assertEquals('Hans Herrmann', $target->getFullName()); + self::assertSame('Hans Herrmann', $target->getFullName()); + self::assertSame('default@test.de', $target->getMail()); + self::assertSame(39, $target->getAge()); } } diff --git a/tests/Fixtures/Config/person.yaml b/tests/Fixtures/Config/person.yaml index c7aff5d..a9e2747 100644 --- a/tests/Fixtures/Config/person.yaml +++ b/tests/Fixtures/Config/person.yaml @@ -13,25 +13,17 @@ neusta_converter: # locale: language # different property names test.person.converter.extended: - target_factory: Neusta\ConverterBundle\Tests\Fixtures\Model\Target\Factory\PersonFactory + target_factory: Neusta\ConverterBundle\Tests\Fixtures\Model\Target\Factory\PersonWithDefaultsFactory properties: fullName: source: fullName default: 'Hans Herrmann' + mail: + source: email skip_null: true age?: ageInYears - test.contactnumber.converter: - target_factory: Neusta\ConverterBundle\Tests\Fixtures\Model\ContactNumberFactory - properties: - phoneNumber: number - - populator: - test.person.address.populator: - converter: test.address.converter - property: - address: ~ - services: + Neusta\ConverterBundle\Tests\Fixtures\Model\Target\Factory\PersonWithDefaultsFactory: ~ Neusta\ConverterBundle\Tests\Fixtures\Model\Target\Factory\PersonFactory: ~ Neusta\ConverterBundle\Tests\Fixtures\Populator\PersonNamePopulator: ~ diff --git a/tests/Fixtures/Model/Source/User.php b/tests/Fixtures/Model/Source/User.php index b544211..1b75a17 100644 --- a/tests/Fixtures/Model/Source/User.php +++ b/tests/Fixtures/Model/Source/User.php @@ -13,7 +13,7 @@ class User private string $lastname; private ?string $fullName; private ?int $ageInYears; - private string $email; + private ?string $email; private ?Address $address; /** @var array */ @@ -87,14 +87,16 @@ public function setAgeInYears(?int $ageInYears): self return $this; } - public function getEmail(): string + public function getEmail(): ?string { return $this->email; } - public function setEmail(string $email): void + public function setEmail(?string $email): self { $this->email = $email; + + return $this; } public function getAddress(): ?Address diff --git a/tests/Fixtures/Model/Target/Factory/PersonWithDefaultsFactory.php b/tests/Fixtures/Model/Target/Factory/PersonWithDefaultsFactory.php new file mode 100644 index 0000000..eb0d0fd --- /dev/null +++ b/tests/Fixtures/Model/Target/Factory/PersonWithDefaultsFactory.php @@ -0,0 +1,22 @@ + + */ +class PersonWithDefaultsFactory implements TargetFactory +{ + public function create(?object $ctx = null): Person + { + return (new Person()) + ->setMail('default@test.de') + ->setAge(39); + } +} diff --git a/tests/Fixtures/Model/Target/Person.php b/tests/Fixtures/Model/Target/Person.php index 3b8e95f..d6ae6d7 100644 --- a/tests/Fixtures/Model/Target/Person.php +++ b/tests/Fixtures/Model/Target/Person.php @@ -65,6 +65,18 @@ public function setAge(?int $age): self return $this; } + public function getMail(): ?string + { + return $this->mail; + } + + public function setMail(?string $mail): self + { + $this->mail = $mail; + + return $this; + } + public function getAddress(): ?PersonAddress { return $this->address; diff --git a/tests/Populator/PersonAddressPopulatorIntegrationTest.php b/tests/Populator/PersonAddressPopulatorIntegrationTest.php index 18d83be..5d0f94b 100644 --- a/tests/Populator/PersonAddressPopulatorIntegrationTest.php +++ b/tests/Populator/PersonAddressPopulatorIntegrationTest.php @@ -15,7 +15,6 @@ #[ConfigureContainer(__DIR__ . '/../Fixtures/Config/person.yaml')] #[ConfigureContainer(__DIR__ . '/../Fixtures/Config/address.yaml')] -#[ConfigureContainer(__DIR__ . '/../Fixtures/Config/contact_numbers.yaml')] class PersonAddressPopulatorIntegrationTest extends ConfigurableKernelTestCase { public function testPopulate_regular_case(): void diff --git a/tests/Populator/PropertyMappingPopulatorTest.php b/tests/Populator/PropertyMappingPopulatorTest.php index 1521a08..57af128 100644 --- a/tests/Populator/PropertyMappingPopulatorTest.php +++ b/tests/Populator/PropertyMappingPopulatorTest.php @@ -17,84 +17,79 @@ class PropertyMappingPopulatorTest extends TestCase public function test_populate(): void { - $populator = new PropertyMappingPopulator('age', 'ageInYears'); - $user = (new User())->setAgeInYears(37); - $person = new Person(); + $populator = new PropertyMappingPopulator( + targetProperty: 'age', + sourceProperty: 'ageInYears', + ); + $source = (new User())->setAgeInYears(37); + $target = new Person(); - $populator->populate($person, $user); + $populator->populate($target, $source); - self::assertEquals(37, $person->getAge()); + self::assertEquals(37, $target->getAge()); } public function test_populate_default_value(): void { - $populator = new PropertyMappingPopulator('fullName', 'fullName', 'default'); - $user = (new User())->setFullName(null); - $person = new Person(); + $populator = new PropertyMappingPopulator( + targetProperty: 'fullName', + sourceProperty: 'fullName', + defaultValue: 'default', + ); + $source = (new User())->setFullName(null); + $target = new Person(); - $populator->populate($person, $user); + $populator->populate($target, $source); - self::assertSame('default', $person->getFullName()); + self::assertSame('default', $target->getFullName()); } - public function test_populate_null_safety(): void + public function test_populate_skip_null(): void { $populator = new PropertyMappingPopulator( - 'fullName', - 'fullName', - null, - null, - null, - true + targetProperty: 'fullName', + sourceProperty: 'fullName', + skipNull: true, ); - $user = (new User())->setFullName(null); - $person = new Person(); - $person->setFullName('old Name'); + $source = (new User())->setFullName(null); + $target = new Person(); + $target->setFullName('old Name'); - $populator->populate($person, $user); + $populator->populate($target, $source); - self::assertSame('old Name', $person->getFullName()); + self::assertSame('old Name', $target->getFullName()); } - public function test_populate_with_dot_operator(): void + public function test_populate_with_sub_fields(): void { $populator = new PropertyMappingPopulator( - 'placeOfResidence', - 'address.city', - null, - null, - null, - true + targetProperty: 'placeOfResidence', + sourceProperty: 'address.city', ); - $user = (new User())->setAddress((new Address())->setCity('Bremen')); - - $person = new Person(); + $source = (new User())->setAddress((new Address())->setCity('Bremen')); + $target = new Person(); - $populator->populate($person, $user); + $populator->populate($target, $source); - self::assertSame('Bremen', $person->getPlaceOfResidence()); + self::assertSame('Bremen', $target->getPlaceOfResidence()); } /** * @requires function \Symfony\Component\PropertyAccess\PropertyPath::isNullSafe */ - public function test_populate_with_dot_operator_and_null_safety(): void + public function test_populate_skip_null_with_sub_fields_and_null_safety(): void { $populator = new PropertyMappingPopulator( - 'placeOfResidence', - 'address?.city', - null, - null, - null, - true + targetProperty: 'placeOfResidence', + sourceProperty: 'address?.city', + skipNull: true, ); - $user = (new User())->setAddress(null); - - $person = new Person(); - $person->setPlaceOfResidence('Old City'); + $source = (new User())->setAddress(null); + $target = new Person(); + $target->setPlaceOfResidence('Old City'); - $populator->populate($person, $user); + $populator->populate($target, $source); - self::assertSame('Old City', $person->getPlaceOfResidence()); + self::assertSame('Old City', $target->getPlaceOfResidence()); } }