Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pchapl committed Sep 25, 2021
1 parent 2f84b85 commit e3acb74
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 36 deletions.
5 changes: 3 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

namespace PChapl\DoctrineIdBundle\DependencyInjection;

use PChapl\DoctrineIdBundle\DoctrineIdBundle;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

final class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('doctrine_id');
$treeBuilder = new TreeBuilder(DoctrineIdBundle::BUNDLE_ID);

$treeBuilder->getRootNode()
->children()
->arrayNode('types')
->arrayNode(DoctrineIdBundle::CONFIG_TYPES_KEY)
->useAttributeAsKey('name')
->scalarPrototype()->end()
?->end()
Expand Down
9 changes: 5 additions & 4 deletions src/DependencyInjection/DoctrineIdExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PChapl\DoctrineIdBundle\DependencyInjection;

use PChapl\DoctrineIdBundle\DoctrineIdBundle;
use PChapl\DoctrineIdBundle\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
Expand All @@ -12,28 +13,28 @@ public function load(array $configs, ContainerBuilder $container): void
{
$config = $this->processConfiguration(new Configuration(), $configs);

$idTypes = $config['types'] ?? [];
$idTypes = $config[DoctrineIdBundle::CONFIG_TYPES_KEY] ?? [];

$types = $container->hasParameter('doctrine.dbal.connection_factory.types')
? $container->getParameter('doctrine.dbal.connection_factory.types')
: [];

foreach ($idTypes as $typeName => $class) {
if (!class_exists($class)) {
throw new InvalidConfigurationException("Can not load class $class");
throw new InvalidConfigurationException("Can not load class '$class'");
}
if (array_key_exists($typeName, $types)) {
throw new InvalidConfigurationException(
sprintf(
'Duplicate key "%s": type should be described either for %s or %s',
$typeName,
'parameters.doctrine_id.types',
'doctrine_id.types',
'doctrine.dbal.types',
)
);
}
}

$container->setParameter('pchapl.doctrine_id.types', $idTypes);
$container->setParameter(DoctrineIdBundle::TYPES_PARAMETER_NAME, $idTypes);
}
}
10 changes: 6 additions & 4 deletions src/DoctrineIdBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

final class DoctrineIdBundle extends Bundle
{
/**
* @throws Exception|InvalidArgumentException
*/
public const TYPES_PARAMETER_NAME = 'pchapl.doctrine_id.types';
public const BUNDLE_ID = 'doctrine_id';
public const CONFIG_TYPES_KEY = 'types';

/** @throws Exception|InvalidArgumentException */
public function boot(): void
{
$types = $this->container->getParameter('pchapl.doctrine_id.types');
$types = $this->container->getParameter(self::TYPES_PARAMETER_NAME);

foreach ($types as $typeName => $class) {
if (!Type::hasType($typeName)) {
Expand Down
32 changes: 32 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PChapl\Tests\DependencyInjection;

use PChapl\DoctrineIdBundle\DependencyInjection\Configuration;
use PChapl\DoctrineIdBundle\DoctrineIdBundle;
use PHPUnit\Framework\TestCase;

class ConfigurationTest extends TestCase
{
private const TYPES = [
DoctrineIdBundle::CONFIG_TYPES_KEY => [
'account_id' => 'App\Data\AccountId',
'user_id' => 'App\Data\UserId',
],
];

private Configuration $configuration;

protected function setUp(): void
{
$this->configuration = new Configuration();
}

public function testTree(): void
{
$tree = $this->configuration->getConfigTreeBuilder()->buildTree();

self::assertSame(DoctrineIdBundle::BUNDLE_ID, $tree->getName());
self::assertSame(self::TYPES, $tree->normalize(self::TYPES));
}
}
92 changes: 92 additions & 0 deletions tests/DependencyInjection/DoctrineIdExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace PChapl\Tests\DependencyInjection;

use PChapl\DoctrineIdBundle\DependencyInjection\DoctrineIdExtension;
use PChapl\DoctrineIdBundle\DoctrineIdBundle;
use PChapl\DoctrineIdBundle\Exception\InvalidConfigurationException;
use PChapl\Tests\TestId;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class DoctrineIdExtensionTest extends TestCase
{
private const TEST_ID_TYPE_NAME = 'test_id_type_name';
private const CONFIGS = [
[
DoctrineIdBundle::CONFIG_TYPES_KEY => [
self::TEST_ID_TYPE_NAME => TestId::class,
],
],
];

/** @var MockObject&ContainerBuilder */
private MockObject|ContainerBuilder $container;
private DoctrineIdExtension $extension;

protected function setUp(): void
{
$this->extension = new DoctrineIdExtension();

/** @var ContainerBuilder&MockObject $container */
$container = $this->createMock(ContainerBuilder::class);
$this->container = $container;
}

public function testLoad(): void
{
$this->container
->expects($this->once())
->method('setParameter')
->willReturnCallback(
static function (string $name, mixed $value) {
self::assertSame(DoctrineIdBundle::TYPES_PARAMETER_NAME, $name);
self::assertIsArray($value);
self::assertCount(1, $value);

foreach ($value as $k => $v) {
self::assertSame(self::TEST_ID_TYPE_NAME, $k);
self::assertSame(TestId::class, $v);
}
}
);

$this->extension->load(self::CONFIGS, $this->container);
}

public function testLoadFailsOnClass(): void
{
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage("Can not load class 'fake-class'");

$this->extension->load(
[[DoctrineIdBundle::CONFIG_TYPES_KEY => [self::TEST_ID_TYPE_NAME => 'fake-class']]],
$this->container,
);
}

public function testLoadFailsOnDuplicate(): void
{
$duplicatedId = 'test_duplicated_id';

$this->container->method('hasParameter')->willReturnCallback(
static fn (string $name): bool => $name === 'doctrine.dbal.connection_factory.types',
);
$this->container->method('getParameter')->willReturnCallback(
static fn (string $name) => $name === 'doctrine.dbal.connection_factory.types'
? [$duplicatedId => TestId::class]
: null,
);

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage(
"Duplicate key \"$duplicatedId\": type should be described either for doctrine_id.types or doctrine.dbal.types"
);

$this->extension->load(
[[DoctrineIdBundle::CONFIG_TYPES_KEY => [$duplicatedId => TestId::class]]],
$this->container,
);
}
}
39 changes: 39 additions & 0 deletions tests/DoctrineIdBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace PChapl\Tests;

use PChapl\DoctrineIdBundle\DoctrineIdBundle;
use PChapl\DoctrineIdBundle\Id\Type;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

class DoctrineIdBundleTest extends TestCase
{
private const TEST_ID_TYPE_NAME = 'test_id_type_name';

private DoctrineIdBundle $bundle;

private array $parameters = [
DoctrineIdBundle::TYPES_PARAMETER_NAME => [
self::TEST_ID_TYPE_NAME => TestId::class,
],
];

protected function setUp(): void
{
$this->bundle = new DoctrineIdBundle();
/** @var ContainerInterface&MockObject $container */
$container = $this->createMock(ContainerInterface::class);
$this->bundle->setContainer($container);
$container->method('getParameter')->willReturnCallback(fn (string $name) => $this->parameters[$name] ?? null);
}

public function testBoot(): void
{
self::assertFalse(Type::hasType(self::TEST_ID_TYPE_NAME));
$this->bundle->boot();
self::assertTrue(Type::hasType(self::TEST_ID_TYPE_NAME));
self::assertInstanceOf(Type::class, Type::getType(self::TEST_ID_TYPE_NAME));
}
}
23 changes: 0 additions & 23 deletions tests/DoctrineIdExtensionTest.php

This file was deleted.

3 changes: 2 additions & 1 deletion tests/BaseTest.php → tests/Id/BaseTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace PChapl\Tests;
namespace PChapl\Tests\Id;

use PChapl\DoctrineIdBundle\Id\Base;
use PChapl\DoctrineIdBundle\Id\Factory;
use PChapl\Tests\TestId;
use PHPUnit\Framework\TestCase;

class BaseTest extends TestCase
Expand Down
3 changes: 2 additions & 1 deletion tests/TypeFactoryTest.php → tests/Id/TypeFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

namespace PChapl\Tests;
namespace PChapl\Tests\Id;

use PChapl\DoctrineIdBundle\Id\TypeFactory;
use PChapl\Tests\TestId;
use PHPUnit\Framework\TestCase;

class TypeFactoryTest extends TestCase
Expand Down
3 changes: 2 additions & 1 deletion tests/TypeTest.php → tests/Id/TypeTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace PChapl\Tests;
namespace PChapl\Tests\Id;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use PChapl\DoctrineIdBundle\Exception\ConversionException;
use PChapl\DoctrineIdBundle\Id\Base;
use PChapl\DoctrineIdBundle\Id\Type;
use PChapl\DoctrineIdBundle\Id\TypeFactory;
use PChapl\Tests\TestId;
use PHPUnit\Framework\TestCase;
use stdClass;

Expand Down

0 comments on commit e3acb74

Please sign in to comment.