Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests #70

Merged
merged 5 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"ergebnis/composer-normalize": "^2.42",
"friendsofphp/php-cs-fixer": "^3.11",
"jangregor/phpstan-prophecy": "^1.0",
"matthiasnoback/symfony-dependency-injection-test": "^5.1",
"nyholm/symfony-bundle-test": "^3.0",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.9",
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ parameters:
paths:
- src

symfony:
containerXmlPath: tests/app/var/cache/test/TestKernelTestDebugContainer.xml

checkGenericClassInNonGenericObjectType: false
60 changes: 60 additions & 0 deletions tests/ConfigurableKernelTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

namespace Neusta\ConverterBundle\Tests;

use Neusta\ConverterBundle\Tests\Support\Attribute\ConfigureContainer;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

abstract class ConfigurableKernelTestCase extends KernelTestCase
{
/** @var list<ConfigureContainer> */
private static iterable $kernelConfigurations = [];

protected static function getKernelClass(): string
{
return \TestKernel::class;
}

protected static function createKernel(array $options = []): \TestKernel
{
$kernel = parent::createKernel($options);
\assert($kernel instanceof \TestKernel);

foreach (self::$kernelConfigurations as $configuration) {
$configuration->configure($kernel);
}

$kernel->handleOptions($options);

return $kernel;
}

/**
* @internal
*
* @before
*/
public function _getKernelConfigurationFromAttributes(): void
{
$class = new \ReflectionClass($this);
$method = $class->getMethod($this->getName(false));

$attributes = [];
foreach ($class->getAttributes(ConfigureContainer::class) as $attribute) {
$attributes[] = $attribute->newInstance();
}

foreach ($method->getAttributes(ConfigureContainer::class) as $attribute) {
$attributes[] = $attribute->newInstance();
}

self::$kernelConfigurations = $attributes;
}

protected function tearDown(): void
{
self::$kernelConfigurations = [];
parent::tearDown();
}
}
25 changes: 8 additions & 17 deletions tests/Converter/GenericConverterIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,22 @@

namespace Neusta\ConverterBundle\Tests\Converter;

use Neusta\ConverterBundle\Converter;
use Neusta\ConverterBundle\Converter\Context\GenericContext;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Person;
use Neusta\ConverterBundle\Tests\ConfigurableKernelTestCase;
use Neusta\ConverterBundle\Tests\Fixtures\Model\User;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Neusta\ConverterBundle\Tests\Support\Attribute\ConfigureContainer;

class GenericConverterIntegrationTest extends KernelTestCase
#[ConfigureContainer(__DIR__ . '/../Fixtures/Config/person.yaml')]
class GenericConverterIntegrationTest extends ConfigurableKernelTestCase
{
/** @var Converter<User, Person, GenericContext> */
private Converter $converter;

protected function setUp(): void
{
parent::setUp();
$this->converter = self::getContainer()->get('test.person.converter');
}

public function testConvert(): void
{
// Test Fixture
$source = (new User())->setFirstname('Max')->setLastname('Mustermann');
// Test Execution
$target = $this->converter->convert($source);
$target = self::getContainer()->get('test.person.converter')->convert($source);
// Test Assertion
self::assertEquals('Max Mustermann', $target->getFullName());
self::assertSame('Max Mustermann', $target->getFullName());
}

public function testConvertWithContext(): void
Expand All @@ -37,8 +28,8 @@ public function testConvertWithContext(): void
$source = (new User())->setFirstname('Max')->setLastname('Mustermann');
$ctx = (new GenericContext())->setValue('separator', ', ');
// Test Execution
$target = $this->converter->convert($source, $ctx);
$target = self::getContainer()->get('test.person.converter')->convert($source, $ctx);
// Test Assertion
self::assertEquals('Max, Mustermann', $target->getFullName());
self::assertSame('Max, Mustermann', $target->getFullName());
}
}
285 changes: 122 additions & 163 deletions tests/DependencyInjection/NeustaConverterExtensionTest.php

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions tests/Fixtures/Config/activities.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
test.person.activities.populator:
public: true
parent: 'neusta_converter.array_property_mapping_populator'
arguments:
$sourceArrayItemProperty: 'label'
$sourceArrayProperty: 'hobbies'
$targetProperty: 'activities'
34 changes: 34 additions & 0 deletions tests/Fixtures/Config/address.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
neusta_converter:
populator:
test.person.address.populator:
converter: test.address.converter
property:
address: ~

services:
Neusta\ConverterBundle\Tests\Fixtures\Model\AddressFactory: ~
Neusta\ConverterBundle\Tests\Fixtures\Populator\AddressPopulator: ~

test.address.converter:
public: true
parent: 'neusta_converter.generic_converter'
arguments:
$factory: '@Neusta\ConverterBundle\Tests\Fixtures\Model\AddressFactory'
$populators:
- '@Neusta\ConverterBundle\Tests\Fixtures\Populator\AddressPopulator'

test.person.wrong.source.type.populator:
public: true
parent: 'neusta_converter.converting_populator'
arguments:
$converter: '@test.address.converter'
$sourcePropertyName: 'fieldWithUnknownType'
$targetPropertyName: 'address'

test.person.wrong.converter.populator:
public: true
parent: 'neusta_converter.converting_populator'
arguments:
$converter: '@test.person.converter' # wrong converter for testing
$sourcePropertyName: 'address'
$targetPropertyName: 'address'
17 changes: 17 additions & 0 deletions tests/Fixtures/Config/contact_numbers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
neusta_converter:
converter:
test.contactnumber.converter:
target_factory: Neusta\ConverterBundle\Tests\Fixtures\Model\ContactNumberFactory
properties:
phoneNumber: number

services:
Neusta\ConverterBundle\Tests\Fixtures\Model\ContactNumberFactory: ~

test.person.contactnumbers.populator:
public: true
parent: 'neusta_converter.array_converting_populator'
arguments:
$converter: '@test.contactnumber.converter'
$sourceArrayPropertyName: 'phones'
$targetPropertyName: 'contactNumbers'
7 changes: 7 additions & 0 deletions tests/Fixtures/Config/full_name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
test.person.fullName.populator:
public: true
parent: 'neusta_converter.property_mapping_populator'
arguments:
$targetProperty: 'fullName'
$sourceProperty: 'fullName'
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ neusta_converter:
# group: ~ # same property name
# locale: language # different property names

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\PersonFactory: ~
Neusta\ConverterBundle\Tests\Fixtures\Populator\PersonNamePopulator: ~
45 changes: 16 additions & 29 deletions tests/Populator/ArrayConvertingPopulatorIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,32 @@

namespace Neusta\ConverterBundle\Tests\Populator;

use Neusta\ConverterBundle\Populator\ArrayConvertingPopulator;
use Neusta\ConverterBundle\Tests\ConfigurableKernelTestCase;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Person;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Phone;
use Neusta\ConverterBundle\Tests\Fixtures\Model\User;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Neusta\ConverterBundle\Tests\Support\Attribute\ConfigureContainer;

class ArrayConvertingPopulatorIntegrationTest extends KernelTestCase
class ArrayConvertingPopulatorIntegrationTest extends ConfigurableKernelTestCase
{
private ArrayConvertingPopulator $populator;

protected function setUp(): void
{
parent::setUp();
$this->populator = self::getContainer()->get('test.person.contactnumbers.populator');
}

#[ConfigureContainer(__DIR__ . '/../Fixtures/Config/contact_numbers.yaml')]
public function testPopulate(): void
{
$phone1 = (new Phone())->setType('mobile')->setNumber('0171 2456543');
$phone2 = (new Phone())->setType('mobile')->setNumber('0172 2456543');
$phone3 = (new Phone())->setType('home')->setNumber('0421 2456543');

$user = (new User())->setPhones([$phone1, $phone2, $phone3]);
$phone1 = '0171 2456543';
$phone2 = '0172 2456543';
$phone3 = '0421 2456543';
$user = (new User())->setPhones([
(new Phone())->setType('mobile')->setNumber($phone1),
(new Phone())->setType('mobile')->setNumber($phone2),
(new Phone())->setType('home')->setNumber($phone3),
]);
$person = new Person();

$this->populator->populate($person, $user);
self::getContainer()->get('test.person.contactnumbers.populator')->populate($person, $user);

self::assertEquals(
[
'0171 2456543',
'0172 2456543',
'0421 2456543',
],
array_map(
function ($item) {
return $item->getPhoneNumber();
},
$person->getContactNumbers()
)
self::assertSame(
[$phone1, $phone2, $phone3],
array_map(fn ($item) => $item->getPhoneNumber(), $person->getContactNumbers()),
);
}
}
21 changes: 7 additions & 14 deletions tests/Populator/ArrayPropertyMappingPopulatorIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@

namespace Neusta\ConverterBundle\Tests\Populator;

use Neusta\ConverterBundle\Populator\ArrayPropertyMappingPopulator;
use Neusta\ConverterBundle\Tests\ConfigurableKernelTestCase;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Hobby;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Person;
use Neusta\ConverterBundle\Tests\Fixtures\Model\User;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Neusta\ConverterBundle\Tests\Support\Attribute\ConfigureContainer;

class ArrayPropertyMappingPopulatorIntegrationTest extends KernelTestCase
class ArrayPropertyMappingPopulatorIntegrationTest extends ConfigurableKernelTestCase
{
private ArrayPropertyMappingPopulator $populator;

protected function setUp(): void
{
parent::setUp();
$this->populator = self::getContainer()->get('test.person.activities.populator');
}

#[ConfigureContainer(__DIR__ . '/../Fixtures/Config/activities.yaml')]
public function testPopulate(): void
{
$user = (new User())->setHobbies([
Expand All @@ -29,15 +22,15 @@ public function testPopulate(): void
]);
$person = new Person();

$this->populator->populate($person, $user);
self::getContainer()->get('test.person.activities.populator')->populate($person, $user);

self::assertEquals(
self::assertSame(
[
'reading',
'swimming',
'computers',
],
$person->getActivities()
$person->getActivities(),
);
}
}
Loading
Loading