Skip to content

Commit

Permalink
Refactor tests (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Albrecht <[email protected]>
  • Loading branch information
jdreesen and mike4git authored Mar 22, 2024
1 parent 3f9c1fa commit 89bbb64
Show file tree
Hide file tree
Showing 35 changed files with 381 additions and 390 deletions.
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();
}
}
27 changes: 9 additions & 18 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\Fixtures\Model\User;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Neusta\ConverterBundle\Tests\ConfigurableKernelTestCase;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Source\User;
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());
}
}
6 changes: 3 additions & 3 deletions tests/Converter/GenericConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Neusta\ConverterBundle\Converter\Context\GenericContext;
use Neusta\ConverterBundle\Converter\GenericConverter;
use Neusta\ConverterBundle\Populator\ContextMappingPopulator;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Person;
use Neusta\ConverterBundle\Tests\Fixtures\Model\PersonFactory;
use Neusta\ConverterBundle\Tests\Fixtures\Model\User;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Source\User;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Target\Factory\PersonFactory;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Target\Person;
use Neusta\ConverterBundle\Tests\Fixtures\Populator\PersonNamePopulator;
use PHPUnit\Framework\TestCase;

Expand Down
4 changes: 2 additions & 2 deletions tests/Converter/StrategicConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Neusta\ConverterBundle\Converter\StrategicConverter;
use Neusta\ConverterBundle\Converter\Strategy\ConverterSelector;
use Neusta\ConverterBundle\Exception\ConverterException;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Person;
use Neusta\ConverterBundle\Tests\Fixtures\Model\User;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Source\User;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Target\Person;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
Expand Down
Loading

0 comments on commit 89bbb64

Please sign in to comment.