Skip to content

Commit 43aabeb

Browse files
authored
Allow empty mapping configuration (#127)
1 parent e76a4a6 commit 43aabeb

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

src/DI/OrmExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function getConfigSchema(): Schema
130130
'namespace' => Expect::string()->required(),
131131
]),
132132
Expect::string()
133-
)->required()->assert(fn ($input) => count($input) > 0, 'At least one mapping must be defined'),
133+
)->default([]),
134134
'defaultCache' => (clone $expectService),
135135
'queryCache' => (clone $expectService),
136136
'resultCache' => (clone $expectService),

tests/Cases/DI/OrmExtension.mapping.phpt

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use Doctrine\ORM\Mapping\Driver\AttributeDriver;
77
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
88
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
99
use Nette\DI\Compiler;
10-
use Nette\DI\InvalidConfigurationException;
1110
use Nettrine\DBAL\DI\DbalExtension;
1211
use Nettrine\ORM\DI\OrmExtension;
1312
use Tester\Assert;
@@ -156,21 +155,20 @@ Toolkit::test(function (): void {
156155
Assert::equal([DummyEntity::class], $driver->getAllClassNames());
157156
});
158157

159-
// Empty mapping
158+
// Empty mapping (allowed for cases where mapping is defined in user's DI extension)
160159
Toolkit::test(function (): void {
161-
Assert::exception(function (): void {
162-
ContainerBuilder::of()
163-
->withCompiler(function (Compiler $compiler): void {
164-
$compiler->addExtension('nettrine.dbal', new DbalExtension());
165-
$compiler->addExtension('nettrine.orm', new OrmExtension());
166-
$compiler->addConfig([
167-
'parameters' => [
168-
'tempDir' => Tests::TEMP_PATH,
169-
'fixturesDir' => Tests::FIXTURES_PATH,
170-
],
171-
]);
172-
$compiler->addConfig(Neonkit::load(
173-
<<<'NEON'
160+
$container = ContainerBuilder::of()
161+
->withCompiler(function (Compiler $compiler): void {
162+
$compiler->addExtension('nettrine.dbal', new DbalExtension());
163+
$compiler->addExtension('nettrine.orm', new OrmExtension());
164+
$compiler->addConfig([
165+
'parameters' => [
166+
'tempDir' => Tests::TEMP_PATH,
167+
'fixturesDir' => Tests::FIXTURES_PATH,
168+
],
169+
]);
170+
$compiler->addConfig(Neonkit::load(
171+
<<<'NEON'
174172
nettrine.dbal:
175173
connections:
176174
default:
@@ -184,8 +182,46 @@ Toolkit::test(function (): void {
184182
connection: default
185183
mapping: []
186184
NEON
187-
));
188-
})
189-
->build();
190-
}, InvalidConfigurationException::class, "Failed assertion 'At least one mapping must be defined' for item 'nettrine.orm › managers › default › mapping' with value array.");
185+
));
186+
})
187+
->build();
188+
189+
/** @var MappingDriverChain $driver */
190+
$driver = $container->getService('nettrine.orm.managers.default.mappingDriver');
191+
Assert::count(0, $driver->getDrivers());
192+
});
193+
194+
// No mapping key at all (uses default empty array)
195+
Toolkit::test(function (): void {
196+
$container = ContainerBuilder::of()
197+
->withCompiler(function (Compiler $compiler): void {
198+
$compiler->addExtension('nettrine.dbal', new DbalExtension());
199+
$compiler->addExtension('nettrine.orm', new OrmExtension());
200+
$compiler->addConfig([
201+
'parameters' => [
202+
'tempDir' => Tests::TEMP_PATH,
203+
'fixturesDir' => Tests::FIXTURES_PATH,
204+
],
205+
]);
206+
$compiler->addConfig(Neonkit::load(
207+
<<<'NEON'
208+
nettrine.dbal:
209+
connections:
210+
default:
211+
driver: pdo_sqlite
212+
password: test
213+
user: test
214+
path: ":memory:"
215+
nettrine.orm:
216+
managers:
217+
default:
218+
connection: default
219+
NEON
220+
));
221+
})
222+
->build();
223+
224+
/** @var MappingDriverChain $driver */
225+
$driver = $container->getService('nettrine.orm.managers.default.mappingDriver');
226+
Assert::count(0, $driver->getDrivers());
191227
});

0 commit comments

Comments
 (0)