Skip to content

Commit

Permalink
Some cleanup & improvements in the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed Apr 2, 2024
1 parent 67449d5 commit 7b0c5a8
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 70 deletions.
2 changes: 0 additions & 2 deletions tests/Converter/GenericConverterIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions tests/Converter/GenericExtendedConverterIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@
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<User, Person, GenericContext> $converter */
$converter = self::getContainer()->get('test.person.converter.extended');

// 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('[email protected]', $target->getMail());
self::assertSame(39, $target->getAge());
}
}
16 changes: 4 additions & 12 deletions tests/Fixtures/Config/person.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ~
8 changes: 5 additions & 3 deletions tests/Fixtures/Model/Source/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> */
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/Fixtures/Model/Target/Factory/PersonWithDefaultsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Neusta\ConverterBundle\Tests\Fixtures\Model\Target\Factory;

use Neusta\ConverterBundle\Converter\Context\GenericContext;
use Neusta\ConverterBundle\TargetFactory;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Target\Person;

/**
* @implements TargetFactory<Person, GenericContext>
*/
class PersonWithDefaultsFactory implements TargetFactory
{
public function create(?object $ctx = null): Person
{
return (new Person())
->setMail('[email protected]')
->setAge(39);
}
}
12 changes: 12 additions & 0 deletions tests/Fixtures/Model/Target/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/Populator/PersonAddressPopulatorIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
89 changes: 42 additions & 47 deletions tests/Populator/PropertyMappingPopulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 7b0c5a8

Please sign in to comment.