From 5915bd125878828dd0a83dc0fc64969a5cf0c1b3 Mon Sep 17 00:00:00 2001 From: Ulrich Schmidt-Goertz Date: Fri, 6 Oct 2023 12:30:02 +0200 Subject: [PATCH] SAT-3057 Make bundle compatible with Symfony 5 and 6 --- BazingaPropelEventDispatcherBundle.php | 2 +- EventDispatcher/LazyEventDispatcher.php | 47 +++------- Injector/DispatcherInjector.php | 24 +++-- ...BazingaPropelEventDispatcherBundleTest.php | 66 ++++++++++---- .../EventListener/MyEventListener.php | 8 +- .../EventListener/MyEventSubscriber.php | 10 +-- .../BaseMyObject.php => Base/MyObject.php} | 4 +- .../BaseMyObject3.php => Base/MyObject3.php} | 4 +- Tests/Fixtures/Model/MyObject.php | 5 +- Tests/Fixtures/Model/MyObject3.php | 2 +- Tests/Fixtures/TestKernel.php | 50 +++++++++++ Tests/Fixtures/app/AppKernel.php | 88 ------------------- Tests/Fixtures/{app => }/config/default.yml | 3 +- Tests/TestCase.php | 7 -- Tests/WebTestCase.php | 53 ----------- composer.json | 21 ++++- phpunit.xml.dist | 23 ++--- 17 files changed, 162 insertions(+), 255 deletions(-) rename Tests/Fixtures/Model/{om/BaseMyObject.php => Base/MyObject.php} (64%) rename Tests/Fixtures/Model/{om/BaseMyObject3.php => Base/MyObject3.php} (88%) create mode 100644 Tests/Fixtures/TestKernel.php delete mode 100644 Tests/Fixtures/app/AppKernel.php rename Tests/Fixtures/{app => }/config/default.yml (93%) delete mode 100644 Tests/TestCase.php delete mode 100644 Tests/WebTestCase.php diff --git a/BazingaPropelEventDispatcherBundle.php b/BazingaPropelEventDispatcherBundle.php index 46f7c04..5f3aeff 100644 --- a/BazingaPropelEventDispatcherBundle.php +++ b/BazingaPropelEventDispatcherBundle.php @@ -12,7 +12,7 @@ class BazingaPropelEventDispatcherBundle extends Bundle { /** - * {@inheritdoc} + * {@inheritdoc} */ public function build(ContainerBuilder $container) { diff --git a/EventDispatcher/LazyEventDispatcher.php b/EventDispatcher/LazyEventDispatcher.php index 1147d74..7b8c6bb 100644 --- a/EventDispatcher/LazyEventDispatcher.php +++ b/EventDispatcher/LazyEventDispatcher.php @@ -6,31 +6,13 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -/** - * @author William Durand - */ class LazyEventDispatcher implements EventDispatcherInterface { - /** - * @var \Symfony\Component\DependencyInjection\ContainerInterface - */ - private $container; - - /** - * @var string - */ - private $serviceId; - - /** - * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface - */ - private $eventDispatcher = null; + private ContainerInterface $container; + private string $serviceId; + private ?EventDispatcherInterface $eventDispatcher = null; - /** - * @param ContainerInterface $container - * @param string $serviceId - */ - public function __construct($container, $serviceId) + public function __construct(ContainerInterface $container, string $serviceId) { $this->container = $container; $this->serviceId = $serviceId; @@ -39,7 +21,7 @@ public function __construct($container, $serviceId) /** * {@inheritdoc} */ - public function dispatch($event, $eventName = null) + public function dispatch($event, $eventName = null): object { return $this->getEventDispatcher()->dispatch($event, $eventName); } @@ -47,7 +29,7 @@ public function dispatch($event, $eventName = null) /** * {@inheritdoc} */ - public function addListener($eventName, $listener, $priority = 0) + public function addListener($eventName, $listener, $priority = 0): void { $this->getEventDispatcher()->addListener($eventName, $listener, $priority); } @@ -55,7 +37,7 @@ public function addListener($eventName, $listener, $priority = 0) /** * {@inheritdoc} */ - public function addSubscriber(EventSubscriberInterface $subscriber) + public function addSubscriber(EventSubscriberInterface $subscriber): void { $this->getEventDispatcher()->addSubscriber($subscriber); } @@ -63,7 +45,7 @@ public function addSubscriber(EventSubscriberInterface $subscriber) /** * {@inheritdoc} */ - public function removeListener($eventName, $listener) + public function removeListener($eventName, $listener): void { $this->getEventDispatcher()->removeListener($eventName, $listener); } @@ -71,7 +53,7 @@ public function removeListener($eventName, $listener) /** * {@inheritdoc} */ - public function removeSubscriber(EventSubscriberInterface $subscriber) + public function removeSubscriber(EventSubscriberInterface $subscriber): void { $this->getEventDispatcher()->removeSubscriber($subscriber); } @@ -79,7 +61,7 @@ public function removeSubscriber(EventSubscriberInterface $subscriber) /** * {@inheritdoc} */ - public function getListeners($eventName = null) + public function getListeners($eventName = null): array { return $this->getEventDispatcher()->getListeners($eventName); } @@ -87,7 +69,7 @@ public function getListeners($eventName = null) /** * {@inheritdoc} */ - public function getListenerPriority($eventName, $listener) + public function getListenerPriority($eventName, $listener): ?int { return $this->getEventDispatcher()->getListenerPriority($eventName, $listener); } @@ -95,15 +77,12 @@ public function getListenerPriority($eventName, $listener) /** * {@inheritdoc} */ - public function hasListeners($eventName = null) + public function hasListeners($eventName = null): bool { return $this->getEventDispatcher()->hasListeners($eventName); } - /** - * @return EventDispatcherInterface - */ - protected function getEventDispatcher() + protected function getEventDispatcher(): EventDispatcherInterface { if (null === $this->eventDispatcher) { $this->eventDispatcher = $this->container->get($this->serviceId); diff --git a/Injector/DispatcherInjector.php b/Injector/DispatcherInjector.php index 7b2e86e..e7c9014 100644 --- a/Injector/DispatcherInjector.php +++ b/Injector/DispatcherInjector.php @@ -8,13 +8,11 @@ class DispatcherInjector { - const MODEL_INTERFACE = 'EventDispatcherAwareModelInterface'; + public const MODEL_INTERFACE = 'EventDispatcherAwareModelInterface'; - private $classes; - - private $container; - - private $logger; + private ContainerInterface $container; + private array $classes; + private ?LoggerInterface $logger; public function __construct(ContainerInterface $container, array $classes, LoggerInterface $logger = null) { @@ -26,10 +24,10 @@ public function __construct(ContainerInterface $container, array $classes, Logge /** * Initializes the EventDispatcher-aware models. * - * This methods has to accept unknown classes as it is triggered during + * This method has to accept unknown classes as it is triggered during * the boot and so will be called before running the propel:build command. */ - public function initializeModels() + public function initializeModels(): void { foreach ($this->classes as $id => $class) { $baseClass = sprintf('%s\\Base\\%s', @@ -39,7 +37,7 @@ public function initializeModels() try { $ref = new \ReflectionClass($baseClass); - } catch (\ReflectionException $e) { + } catch (\ReflectionException) { $this->log(sprintf('The class "%s" does not exist.', $baseClass)); continue; @@ -47,7 +45,7 @@ public function initializeModels() try { $ref = new \ReflectionClass($class); - } catch (\ReflectionException $e) { + } catch (\ReflectionException) { $this->log(sprintf( 'The class "%s" does not exist. Either your model is not generated yet or you have an error in your listener configuration.', $class @@ -70,10 +68,8 @@ public function initializeModels() } } - private function log($message) + private function log($message): void { - if (null !== $this->logger) { - $this->logger->warning($message); - } + $this->logger?->warning($message); } } diff --git a/Tests/BazingaPropelEventDispatcherBundleTest.php b/Tests/BazingaPropelEventDispatcherBundleTest.php index 1a0f03e..9559b78 100644 --- a/Tests/BazingaPropelEventDispatcherBundleTest.php +++ b/Tests/BazingaPropelEventDispatcherBundleTest.php @@ -4,38 +4,66 @@ use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\MyObject; use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\MyObject3; +use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener\MyEventSubscriber; +use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\MyObject2; +use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener\MyEventListener; +use Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\TestKernel; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\HttpKernel\Kernel; class BazingaPropelEventDispatcherBundleTest extends WebTestCase { - public function testGetListener() + protected function setUp(): void { - $listener = $this->getContainer()->get('listener.my_event_listener'); + parent::setUp(); + $this->deleteTmpDir(); + self::bootKernel(); + } + + protected static function getKernelClass(): string + { + return TestKernel::class; + } + + private function deleteTmpDir(): void + { + $dir = sys_get_temp_dir().'/'.Kernel::VERSION; + if (file_exists($dir)) { + $fs = new Filesystem(); + $fs->remove($dir); + } + } + + public function testGetListener(): void + { + $listener = self::getContainer()->get('listener.my_event_listener'); $this->assertNotNull($listener); - $this->assertInstanceOf('Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener\MyEventListener', $listener); + $this->assertInstanceOf(MyEventListener::class, $listener); } - public function testGetListenerWithNonExistentClass() + public function testGetListenerWithNonExistentClass(): void { - $this->assertFalse(class_exists('Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\MyObject2', false)); + $this->assertFalse(class_exists(MyObject2::class, false)); - $listener = $this->getContainer()->get('listener.my_event_listener_2'); + $listener = self::getContainer()->get('listener.my_event_listener_2'); $this->assertNotNull($listener); - $this->assertInstanceOf('Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener\MyEventListener', $listener); + $this->assertInstanceOf(MyEventListener::class, $listener); } - public function testGetListenerWithNonExistentParentClass() + public function testGetListenerWithNonExistentParentClass(): void { - $this->assertFalse(class_exists('Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\Model\MyObject2', false)); + $this->assertFalse(class_exists(MyObject2::class, false)); - $listener = $this->getContainer()->get('listener.my_event_listener_3'); + self::getContainer()->get('listener.my_event_listener_3'); } - public function testFireEvent() + public function testFireEvent(): void { $object = new MyObject(); - $listener = $this->getContainer()->get('listener.my_event_listener'); + $listener = self::getContainer()->get('listener.my_event_listener'); $this->assertCount(0, $listener->getEvents()); @@ -47,9 +75,9 @@ public function testFireEvent() $this->assertSame($object, $subject); } - public function testFireEventWithEarlyBoot() + public function testFireEventWithEarlyBoot(): void { - $listener = $this->getContainer()->get('listener.my_event_listener_4'); + $listener = self::getContainer()->get('listener.my_event_listener_4'); $object = new MyObject3(); $this->assertCount(0, $listener->getEvents()); @@ -62,18 +90,18 @@ public function testFireEventWithEarlyBoot() $this->assertSame($object, $subject); } - public function testSubscriber() + public function testSubscriber(): void { - $subscriber = $this->getContainer()->get('subscriber.my_subscriber_1'); + $subscriber = self::getContainer()->get('subscriber.my_subscriber_1'); $this->assertNotNull($subscriber); - $this->assertInstanceOf('Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener\MyEventSubscriber', $subscriber); + $this->assertInstanceOf(MyEventSubscriber::class, $subscriber); } - public function testFireEventWithSubscriber() + public function testFireEventWithSubscriber(): void { $object = new MyObject3(); - $subscriber = $this->getContainer()->get('subscriber.my_subscriber_1'); + $subscriber = self::getContainer()->get('subscriber.my_subscriber_1'); $this->assertCount(0, $subscriber->getEvents()); diff --git a/Tests/Fixtures/EventListener/MyEventListener.php b/Tests/Fixtures/EventListener/MyEventListener.php index 25bad06..20bb6ba 100644 --- a/Tests/Fixtures/EventListener/MyEventListener.php +++ b/Tests/Fixtures/EventListener/MyEventListener.php @@ -2,18 +2,18 @@ namespace Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener; -use Symfony\Component\EventDispatcher\Event; +use Symfony\Contracts\EventDispatcher\Event; class MyEventListener { - private $events = array(); + private array $events = []; - public function preSave(Event $event) + public function preSave(Event $event): void { $this->events[] = $event; } - public function getEvents() + public function getEvents(): array { return $this->events; } diff --git a/Tests/Fixtures/EventListener/MyEventSubscriber.php b/Tests/Fixtures/EventListener/MyEventSubscriber.php index 6d81b59..d95e8a9 100644 --- a/Tests/Fixtures/EventListener/MyEventSubscriber.php +++ b/Tests/Fixtures/EventListener/MyEventSubscriber.php @@ -2,14 +2,14 @@ namespace Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\EventListener; -use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\EventDispatcher\GenericEvent; class MyEventSubscriber implements EventSubscriberInterface { - private $events = array(); + private array $events = []; - public function preInsert(Event $event) + public function preInsert(GenericEvent $event): void { $subject = $event->getSubject(); $subject->source = 'pre_insert'; @@ -17,12 +17,12 @@ public function preInsert(Event $event) $this->events[] = $event; } - public function getEvents() + public function getEvents(): array { return $this->events; } - public static function getSubscribedEvents() + public static function getSubscribedEvents(): array { return array( 'propel.pre_insert' => 'preInsert', diff --git a/Tests/Fixtures/Model/om/BaseMyObject.php b/Tests/Fixtures/Model/Base/MyObject.php similarity index 64% rename from Tests/Fixtures/Model/om/BaseMyObject.php rename to Tests/Fixtures/Model/Base/MyObject.php index ad7adec..2763d18 100644 --- a/Tests/Fixtures/Model/om/BaseMyObject.php +++ b/Tests/Fixtures/Model/Base/MyObject.php @@ -1,7 +1,7 @@ environment; + } + + public function getLogDir(): string + { + return sys_get_temp_dir().'/'.Kernel::VERSION.'/bazinga-propel-event-dispatcher/logs'; + } + + public function registerContainerConfiguration(LoaderInterface $loader): void + { + $loader->load(__DIR__.'/config/default.yml'); + } + + public function serialize(): string + { + return serialize(array($this->getEnvironment(), $this->isDebug())); + } + + public function unserialize($str): void + { + call_user_func_array(array($this, '__construct'), unserialize($str)); + } +} diff --git a/Tests/Fixtures/app/AppKernel.php b/Tests/Fixtures/app/AppKernel.php deleted file mode 100644 index 7e2b848..0000000 --- a/Tests/Fixtures/app/AppKernel.php +++ /dev/null @@ -1,88 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Functional; - -// get the autoload file -$dir = __DIR__; -$lastDir = null; -while ($dir !== $lastDir) { - $lastDir = $dir; - - if (is_file($dir.'/autoload.php')) { - require_once $dir.'/autoload.php'; - break; - } - - if (is_file($dir.'/autoload.php.dist')) { - require_once $dir.'/autoload.php.dist'; - break; - } - - $dir = dirname($dir); -} - -use Symfony\Component\Config\Loader\LoaderInterface; -use Symfony\Component\HttpKernel\Kernel; - -/** - * App Test Kernel for functional tests. - */ -class AppKernel extends Kernel -{ - public function __construct($environment, $debug) - { - parent::__construct($environment, $debug); - } - - public function registerBundles() - { - return array( - new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(), - new \Bazinga\Bundle\PropelEventDispatcherBundle\BazingaPropelEventDispatcherBundle($this), - new \Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Fixtures\BazingaPropelEventDispatcherTestBundle(), - ); - } - - public function init() - { - } - - public function getRootDir() - { - return __DIR__; - } - - public function getCacheDir() - { - return sys_get_temp_dir().'/'.Kernel::VERSION.'/bazinga-propel-eventdispatcher/cache/'.$this->environment; - } - - public function getLogDir() - { - return sys_get_temp_dir().'/'.Kernel::VERSION.'/bazinga-propel-eventdispatcher/logs'; - } - - public function registerContainerConfiguration(LoaderInterface $loader) - { - $loader->load(__DIR__.'/config/'.$this->environment.'.yml'); - } - - public function serialize() - { - return serialize(array($this->getEnvironment(), $this->isDebug())); - } - - public function unserialize($str) - { - call_user_func_array(array($this, '__construct'), unserialize($str)); - } -} diff --git a/Tests/Fixtures/app/config/default.yml b/Tests/Fixtures/config/default.yml similarity index 93% rename from Tests/Fixtures/app/config/default.yml rename to Tests/Fixtures/config/default.yml index 34557c6..ce210b6 100644 --- a/Tests/Fixtures/app/config/default.yml +++ b/Tests/Fixtures/config/default.yml @@ -1,10 +1,9 @@ framework: secret: test - router: { resource: "%kernel.root_dir%/config/routing.yml" } test: ~ default_locale: en session: - storage_id: session.storage.mock_file + storage_factory_id: session.storage.factory.mock_file profiler: { only_exceptions: false } services: diff --git a/Tests/TestCase.php b/Tests/TestCase.php deleted file mode 100644 index 0d3ab1f..0000000 --- a/Tests/TestCase.php +++ /dev/null @@ -1,7 +0,0 @@ -remove($dir); - } - - protected function getContainer(array $options = array()) - { - if (!static::$kernel) { - static::$kernel = static::createKernel($options); - } - static::$kernel->boot(); - - return static::$kernel->getContainer(); - } - - protected static function getKernelClass() - { - require_once __DIR__.'/Fixtures/app/AppKernel.php'; - - return 'Bazinga\Bundle\PropelEventDispatcherBundle\Tests\Functional\AppKernel'; - } - - protected static function createKernel(array $options = array()) - { - $class = self::getKernelClass(); - - return new $class( - 'default', - isset($options['debug']) ? $options['debug'] : true - ); - } - - public function setUp() - { - parent::setUp(); - $this->deleteTmpDir(); - } -} diff --git a/composer.json b/composer.json index 3769d2a..4d90385 100644 --- a/composer.json +++ b/composer.json @@ -2,13 +2,23 @@ "name": "finanzcheck/propel-event-dispatcher-bundle", "description": "Integrates the Propel EventDispatcherBehavior into Symfony.", "license": "MIT", + "repositories": [ + { + "type": "composer", + "url": "https://repo.packagist.com/finanzcheck/" + }, + { + "packagist.org": false + } + ], "require": { - "php": "^7.4 || ^8.0", + "php": "^8.0", "finanzcheck/propel-event-dispatcher-behavior": "^4.0" }, "require-dev": { - "symfony/framework-bundle": "^4.4", - "symfony/yaml": "^4.4" + "phpunit/phpunit": "^10.4", + "symfony/framework-bundle": "^5.4|^6.0", + "symfony/yaml": "^5.4|^6.0" }, "authors": [ { @@ -19,6 +29,11 @@ "autoload": { "psr-4": { "Bazinga\\Bundle\\PropelEventDispatcherBundle\\": "" } }, + "autoload-dev": { + "psr-4": { + "Bazinga\\Bundle\\PropelEventDispatcherBundle\\Tests\\": "Tests/" + } + }, "extra": { "branch-alias": { "dev-master": "2.0-dev" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 703ebc4..c4d563b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,27 +1,14 @@ ./Tests/ - - - . - - ./vendor - ./Tests - - -