-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ease multiple Orm extension instances initialization with connection …
…reference [closes #658]
- Loading branch information
Showing
5 changed files
with
151 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/cases/integration/BridgeNetteDI/dic-extension-multiple.neon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
extensions: | ||
nextras.orm1: Nextras\Orm\Bridges\NetteDI\OrmExtension | ||
nextras.orm2: Nextras\Orm\Bridges\NetteDI\OrmExtension | ||
nextras.dbal1: Nextras\Dbal\Bridges\NetteDI\DbalExtension | ||
nextras.dbal2: Nextras\Dbal\Bridges\NetteDI\DbalExtension | ||
nette.cache: Nette\Bridges\CacheDI\CacheExtension(%tempDir%) | ||
|
||
nextras.orm1: | ||
model: NextrasTests\Orm\Model | ||
connection: @nextras.dbal1.connection | ||
|
||
nextras.orm2: | ||
model: NextrasTests\Orm\Integration\BridgeNetteDI\Model2 | ||
connection: @nextras.dbal2.connection | ||
autowiredInternalServices: false | ||
|
||
nextras.dbal1: | ||
driver: mysqli | ||
|
||
nextras.dbal2: | ||
driver: pgsql | ||
|
||
services: | ||
- Nette\Caching\Cache |
76 changes: 76 additions & 0 deletions
76
tests/cases/integration/BridgeNetteDI/dic-extension-multiple.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace NextrasTests\Orm\Integration\BridgeNetteDI; | ||
|
||
|
||
use Nette\DI\Compiler; | ||
use Nette\DI\Container; | ||
use Nette\DI\ContainerLoader; | ||
use Nette\DI\Extensions\ExtensionsExtension; | ||
use Nextras\Orm\Entity\Entity;use Nextras\Orm\Mapper\Dbal\DbalMapper; | ||
use Nextras\Orm\Mapper\Dbal\DbalMapperCoordinator; | ||
use Nextras\Orm\Model\IModel; | ||
use Nextras\Orm\Model\Model; | ||
use Nextras\Orm\Repository\Repository; | ||
use NextrasTests\Orm\BooksRepository; | ||
use Tester\Assert; | ||
|
||
|
||
require_once __DIR__ . '/../../../bootstrap.php'; | ||
|
||
function buildDic(string $config): Container | ||
{ | ||
$cacheDir = TEMP_DIR . '/cache/bridge-nette-dic-extension-multiple'; | ||
$loader = new ContainerLoader($cacheDir); | ||
$key = __FILE__ . ':' . __LINE__ . ':' . $config; | ||
$className = $loader->load(function (Compiler $compiler) use ($config, $cacheDir): void { | ||
$compiler->addExtension('extensions', new ExtensionsExtension()); | ||
$compiler->addConfig(['parameters' => ['tempDir' => $cacheDir]]); | ||
$compiler->loadConfig($config); | ||
}, $key); | ||
|
||
/** @var Container $dic */ | ||
$dic = new $className(); | ||
return $dic; | ||
} | ||
|
||
/** | ||
* @property int $id {primary} | ||
*/ | ||
class TestEntity extends Entity | ||
{ | ||
} | ||
|
||
/** | ||
* @extends Repository<TestEntity> | ||
*/ | ||
class TestRepository extends Repository | ||
{ | ||
public static function getEntityClassNames(): array | ||
{ | ||
return [TestEntity::class]; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @extends DbalMapper<TestEntity> | ||
*/ | ||
class TestMapper extends DbalMapper | ||
{ | ||
} | ||
|
||
/** | ||
* @property-read TestRepository $test | ||
*/ | ||
class Model2 extends Model | ||
{ | ||
} | ||
|
||
$container = buildDic(__DIR__ . '/dic-extension-multiple.neon'); | ||
Assert::count(2, $container->findByType(IModel::class)); | ||
Assert::count(2, $container->findByType(DbalMapperCoordinator::class)); | ||
// check that returns only one instance | ||
Assert::type(DbalMapperCoordinator::class, $container->getByType(DbalMapperCoordinator::class)); | ||
Assert::type(BooksRepository::class, $container->getByType(BooksRepository::class)); | ||
Assert::type(TestRepository::class, $container->getByType(TestRepository::class)); |