Skip to content

Commit

Permalink
DbalExtension: simplify custom types registration
Browse files Browse the repository at this point in the history
  • Loading branch information
xificurk authored and f3l1x committed Apr 4, 2020
1 parent 42fdb21 commit a253cd3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/DI/DbalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,23 @@ public function getConfigSchema(): Schema
'filterSchemaAssetsExpression' => Expect::string()->nullable(),
'autoCommit' => Expect::bool(true),
]),
'connection' => Expect::array()->default([
'driver' => 'pdo_sqlite',
'types' => [],
'typesMapping' => [],
]),
'connection' => Expect::structure([
'driver' => Expect::mixed('pdo_sqlite'),
'types' => Expect::arrayOf(
Expect::structure([
'class' => Expect::string()->required(),
'commented' => Expect::bool(false),
])
->before(function ($type) {
if (is_string($type)) {
return ['class' => $type];
}
return $type;
})
->castTo('array')
),
'typesMapping' => Expect::array(),
])->otherItems()->castTo('array'),
]);
}

Expand Down
33 changes: 33 additions & 0 deletions tests/cases/Unit/DI/DbalExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
use Doctrine\DBAL\Types\IntegerType;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
Expand Down Expand Up @@ -82,4 +85,34 @@ public function testNoCache(): void
new $class();
}

public function testTypes(): void
{
$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig([
'parameters' => [
'tempDir' => TEMP_PATH,
],
'dbal' => ['connection' => [
'driver' => 'pdo_sqlite',
'types' => [
'foo' => ['class' => StringType::class],
'bar' => IntegerType::class,
],
]],
]);
}, 'di3');

/** @var Container $container */
$container = new $class();

/** @var Connection $connection */
$container->getByType(Connection::class);

$this->assertInstanceOf(StringType::class, Type::getType('foo'));
$this->assertInstanceOf(IntegerType::class, Type::getType('bar'));
}

}

0 comments on commit a253cd3

Please sign in to comment.