-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
232 additions
and
30 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
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,48 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nettrine\DBAL\Events; | ||
|
||
use Doctrine\Common\EventManager; | ||
use Nette\DI\Container; | ||
|
||
class ContainerEventManager extends EventManager | ||
{ | ||
|
||
protected Container $container; | ||
|
||
public function __construct(Container $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* @param string|string[] $events | ||
*/ | ||
public function addServiceSubscriber(string|array $events, string $service): void | ||
{ | ||
$this->addEventListener($events, new class($this->container, $service) { | ||
|
||
public function __construct( | ||
private readonly Container $container, | ||
private readonly string $service | ||
) | ||
{ | ||
} | ||
|
||
/** | ||
* @param mixed[] $arguments | ||
*/ | ||
public function __call(string $name, array $arguments): mixed | ||
{ | ||
$subscriber = $this->container->getByName($this->service); | ||
|
||
$callback = [$subscriber, $name]; | ||
assert(is_callable($callback)); | ||
|
||
return call_user_func_array($callback, $arguments); | ||
} | ||
|
||
}); | ||
} | ||
|
||
} |
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,77 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Cases\E2E; | ||
|
||
use Contributte\Tester\Toolkit; | ||
use Contributte\Tester\Utils\ContainerBuilder; | ||
use Contributte\Tester\Utils\Neonkit; | ||
use Doctrine\Common\EventManager; | ||
use Doctrine\ORM\Events; | ||
use Nette\DI\Compiler; | ||
use Nettrine\Cache\DI\CacheExtension; | ||
use Nettrine\DBAL\DI\DbalExtension; | ||
use Tester\Assert; | ||
use Tests\Toolkit\Tests; | ||
use Tracy\Bridges\Nette\TracyExtension; | ||
|
||
require_once __DIR__ . '/../../bootstrap.php'; | ||
|
||
// Empty events | ||
Toolkit::test(function (): void { | ||
$container = ContainerBuilder::of() | ||
->withCompiler(static function (Compiler $compiler): void { | ||
$compiler->addExtension('nettrine.dbal', new DbalExtension()); | ||
$compiler->addExtension('nettrine.cache', new CacheExtension()); | ||
$compiler->addConfig([ | ||
'parameters' => [ | ||
'tempDir' => Tests::TEMP_PATH, | ||
'appDir' => Tests::APP_PATH, | ||
], | ||
]); | ||
$compiler->addConfig(Neonkit::load(<<<'NEON' | ||
nettrine.dbal: | ||
connection: | ||
driver: pdo_sqlite | ||
NEON | ||
)); | ||
})->build(); | ||
|
||
/** @var EventManager $eventManager */ | ||
$eventManager = $container->getByType(EventManager::class); | ||
|
||
Assert::count(0, $eventManager->getAllListeners()); | ||
}); | ||
|
||
// Subscriber | ||
Toolkit::test(function (): void { | ||
$container = ContainerBuilder::of() | ||
->withCompiler(static function (Compiler $compiler): void { | ||
$compiler->addExtension('nettrine.dbal', new DbalExtension()); | ||
$compiler->addExtension('nettrine.cache', new CacheExtension()); | ||
$compiler->addExtension('nette.tracy', new TracyExtension()); | ||
$compiler->addConfig([ | ||
'parameters' => [ | ||
'tempDir' => Tests::TEMP_PATH, | ||
'appDir' => Tests::APP_PATH, | ||
], | ||
]); | ||
$compiler->addConfig(Neonkit::load(<<<'NEON' | ||
nettrine.dbal: | ||
connection: | ||
driver: pdo_sqlite | ||
types: | ||
foo: { class: Doctrine\DBAL\Types\StringType } | ||
bar: Doctrine\DBAL\Types\IntegerType | ||
services: | ||
- Tests\Fixtures\Events\DummyOnClearSubscriber | ||
NEON | ||
)); | ||
})->build(); | ||
|
||
/** @var EventManager $eventManager */ | ||
$eventManager = $container->getByType(EventManager::class); | ||
|
||
Assert::count(1, $eventManager->getAllListeners()); | ||
Assert::count(1, $eventManager->getAllListeners()[Events::onClear]); | ||
}); |
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,52 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Cases\Events; | ||
|
||
use Contributte\Tester\Toolkit; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Event\OnClearEventArgs; | ||
use Doctrine\ORM\Events; | ||
use Mockery; | ||
use Nette\DI\Container; | ||
use Nettrine\DBAL\Events\ContainerEventManager; | ||
use Tester\Assert; | ||
use Tests\Fixtures\Events\DummyOnClearSubscriber; | ||
|
||
require_once __DIR__ . '/../../bootstrap.php'; | ||
|
||
Toolkit::test(function (): void { | ||
$entityManager = Mockery::mock(EntityManager::class); | ||
|
||
$container = new Container(); | ||
$eventManager = new ContainerEventManager($container); | ||
|
||
$subscriber = new DummyOnClearSubscriber(); | ||
$eventManager->addEventSubscriber($subscriber); | ||
|
||
$event = new OnClearEventArgs($entityManager, 'foo'); | ||
|
||
Assert::count(0, $subscriber->events); | ||
$eventManager->dispatchEvent(Events::onClear, $event); | ||
Assert::count(1, $subscriber->events); | ||
|
||
Assert::same([$event], $subscriber->events); | ||
}); | ||
|
||
Toolkit::test(function (): void { | ||
$entityManager = Mockery::mock(EntityManager::class); | ||
$subscriber = new DummyOnClearSubscriber(); | ||
|
||
$container = new Container(); | ||
$container->addService('dummySubscriber', $subscriber); | ||
$eventManager = new ContainerEventManager($container); | ||
|
||
$eventManager->addServiceSubscriber(Events::onClear, 'dummySubscriber'); | ||
|
||
$event = new OnClearEventArgs($entityManager, 'foo'); | ||
|
||
Assert::count(0, $subscriber->events); | ||
$eventManager->dispatchEvent(Events::onClear, $event); | ||
Assert::count(1, $subscriber->events); | ||
|
||
Assert::same([$event], $subscriber->events); | ||
}); |
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,28 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Fixtures\Events; | ||
|
||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\ORM\Event\OnClearEventArgs; | ||
use Doctrine\ORM\Events; | ||
|
||
final class DummyOnClearSubscriber implements EventSubscriber | ||
{ | ||
|
||
/** @var OnClearEventArgs[] */ | ||
public array $events = []; | ||
|
||
public function onClear(OnClearEventArgs $args): void | ||
{ | ||
$this->events[] = $args; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getSubscribedEvents(): array | ||
{ | ||
return [Events::onClear]; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.