diff --git a/.travis.yml b/.travis.yml index 6002f3c..511ede7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -68,7 +68,7 @@ jobs: - stage: "Analysis" name: "phpstan" script: - - docker-compose run --no-deps --rm php bin/phpstan analyze -l7 src/ tests/ + - docker-compose run --no-deps --rm php bin/phpstan analyze --memory-limit -l7 src/ tests/ - stage: "Analysis" name: "cs-check" diff --git a/src/Capsule/Collection.php b/src/Capsule/Collection.php index f00ee63..2c66a4b 100644 --- a/src/Capsule/Collection.php +++ b/src/Capsule/Collection.php @@ -4,13 +4,13 @@ namespace Facile\MongoDbBundle\Capsule; +use Facile\MongoDbBundle\Event\EventDispatcherCheck; use Facile\MongoDbBundle\Event\QueryEvent; use Facile\MongoDbBundle\Models\Query; use MongoDB\Collection as MongoCollection; use MongoDB\Driver\Manager; use MongoDB\Driver\ReadPreference; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; /** * Class Collection. @@ -221,7 +221,7 @@ private function prepareQuery(string $method, $filters = null, $data = null, arr ); $event = new QueryEvent($query); - if (class_exists(LegacyEventDispatcherProxy::class)) { + if (EventDispatcherCheck::isPSR14Compliant()) { $this->eventDispatcher->dispatch($event, QueryEvent::QUERY_PREPARED); } else { $this->eventDispatcher->dispatch(QueryEvent::QUERY_PREPARED, $event); @@ -261,7 +261,7 @@ private function notifyQueryExecution(Query $queryLog) $queryLog->setExecutionTime(microtime(true) - $queryLog->getStart()); $event = new QueryEvent($queryLog); - if (class_exists(LegacyEventDispatcherProxy::class)) { + if (EventDispatcherCheck::isPSR14Compliant()) { $this->eventDispatcher->dispatch($event, QueryEvent::QUERY_EXECUTED); } else { $this->eventDispatcher->dispatch(QueryEvent::QUERY_EXECUTED, $event); diff --git a/src/DependencyInjection/MongoDbBundleExtension.php b/src/DependencyInjection/MongoDbBundleExtension.php index 83bfe3d..4f46ece 100644 --- a/src/DependencyInjection/MongoDbBundleExtension.php +++ b/src/DependencyInjection/MongoDbBundleExtension.php @@ -5,6 +5,7 @@ namespace Facile\MongoDbBundle\DependencyInjection; use Facile\MongoDbBundle\Event\ConnectionEvent; +use Facile\MongoDbBundle\Event\EventDispatcherCheck; use Facile\MongoDbBundle\Event\QueryEvent; use Facile\MongoDbBundle\Services\ClientRegistry; use Facile\MongoDbBundle\Services\ConnectionFactory; @@ -16,6 +17,8 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; use Symfony\Component\HttpKernel\DependencyInjection\Extension; /** @@ -34,6 +37,7 @@ public function load(array $configs, ContainerBuilder $container) $loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('services.xml'); + $this->decorateEventDispatcher($container); $this->defineClientRegistry($config, $container->getParameter('kernel.debug')); $this->defineConnectionFactory(); $this->defineConnections($config['connections']); @@ -118,4 +122,17 @@ private function defineDriverOptionsFactory(array $config) { return isset($config['driverOptions']) ? new Reference($config['driverOptions']) : null; } + + /** + * This is needed to avoid the EventDispatcher deprecation from 4.3 + */ + private function decorateEventDispatcher(): void + { + if (EventDispatcherCheck::shouldUseLegacyProxy()) { + $definition = $this->containerBuilder->getDefinition('facile_mongo_db.event_dispatcher'); + $definition->setClass(LegacyEventDispatcherProxy::class); + $definition->setFactory([LegacyEventDispatcherProxy::class, 'decorate']); + $definition->setArguments([new Definition(EventDispatcher::class)]); + } + } } diff --git a/src/Event/EventDispatcherCheck.php b/src/Event/EventDispatcherCheck.php new file mode 100644 index 0000000..9665e93 --- /dev/null +++ b/src/Event/EventDispatcherCheck.php @@ -0,0 +1,23 @@ +clients[$clientKey] = $this->buildClient($name, $conf->getUri(), $options, $conf->getDriverOptions()); $event = new ConnectionEvent($clientKey); - if (class_exists(LegacyEventDispatcherProxy::class)) { + if (EventDispatcherCheck::isPSR14Compliant()) { $this->eventDispatcher->dispatch($event, ConnectionEvent::CLIENT_CREATED); } else { $this->eventDispatcher->dispatch(ConnectionEvent::CLIENT_CREATED, $event); diff --git a/tests/Functional/Capsule/CollectionTest.php b/tests/Functional/Capsule/CollectionTest.php index eee420f..1dcac23 100644 --- a/tests/Functional/Capsule/CollectionTest.php +++ b/tests/Functional/Capsule/CollectionTest.php @@ -2,7 +2,10 @@ declare(strict_types=1); +namespace Facile\MongoDbBundle\Tests\Functional\Capsule; + use Facile\MongoDbBundle\Capsule\Collection; +use Facile\MongoDbBundle\Event\EventDispatcherCheck; use Facile\MongoDbBundle\Event\QueryEvent; use Facile\MongoDbBundle\Tests\Functional\AppTestCase; use MongoDB\Driver\Manager; @@ -25,9 +28,9 @@ private function getManager(): Manager public function test_construction() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->mockEventDispatcher(0); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); self::assertInstanceOf(\MongoDB\Collection::class, $coll); } @@ -35,10 +38,9 @@ public function test_construction() public function test_insertOne() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->insertOne(['test' => 1]); } @@ -46,10 +48,9 @@ public function test_insertOne() public function test_updateOne() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->updateOne(['filter' => 1], ['$set' => ['testField' => 1]]); } @@ -57,10 +58,9 @@ public function test_updateOne() public function test_count() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->count(['test' => 1]); } @@ -68,10 +68,9 @@ public function test_count() public function test_find() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->find([]); } @@ -79,10 +78,9 @@ public function test_find() public function test_findOne() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->findOne([]); } @@ -90,10 +88,9 @@ public function test_findOne() public function test_findOneAndUpdate() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->findOneAndUpdate([], ['$set' => ['country' => 'us']]); } @@ -101,10 +98,9 @@ public function test_findOneAndUpdate() public function test_findOneAndDelete() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->findOneAndDelete([]); } @@ -112,10 +108,9 @@ public function test_findOneAndDelete() public function test_deleteOne() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->deleteOne([]); } @@ -123,10 +118,9 @@ public function test_deleteOne() public function test_replaceOne() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->replaceOne([], []); } @@ -134,10 +128,9 @@ public function test_replaceOne() public function test_aggregate() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(5); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->deleteMany([]); @@ -165,10 +158,9 @@ public function test_aggregate() public function test_deleteMany() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->deleteMany([]); } @@ -176,25 +168,35 @@ public function test_deleteMany() public function test_distinct() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); - $this->assertEventsDispatching($ev); + $ev = $this->mockEventDispatcher(1); - $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev); $coll->distinct('field'); } - /** - * @param $ev - */ - protected function assertEventsDispatching($ev) + private function mockEventDispatcher(int $times): EventDispatcherInterface { - if (class_exists(LegacyEventDispatcherProxy::class)) { - $ev->dispatch(Argument::type(QueryEvent::class), QueryEvent::QUERY_PREPARED)->shouldBeCalled(); - $ev->dispatch(Argument::type(QueryEvent::class), QueryEvent::QUERY_EXECUTED)->shouldBeCalled(); + $ed = $this->prophesize(EventDispatcherInterface::class); + + if (EventDispatcherCheck::isPSR14Compliant()) { + $ed->dispatch(Argument::type(QueryEvent::class), QueryEvent::QUERY_PREPARED) + ->shouldBeCalledTimes($times) + ->willReturnArgument(0); + $ed->dispatch(Argument::type(QueryEvent::class), QueryEvent::QUERY_EXECUTED) + ->shouldBeCalledTimes($times) + ->willReturnArgument(0); } else { - $ev->dispatch(QueryEvent::QUERY_PREPARED, Argument::type(QueryEvent::class))->shouldBeCalled(); - $ev->dispatch(QueryEvent::QUERY_EXECUTED, Argument::type(QueryEvent::class))->shouldBeCalled(); + $ed->dispatch(QueryEvent::QUERY_PREPARED, Argument::type(QueryEvent::class)) + ->shouldBeCalledTimes($times); + $ed->dispatch(QueryEvent::QUERY_EXECUTED, Argument::type(QueryEvent::class)) + ->shouldBeCalledTimes($times); + } + + if (EventDispatcherCheck::shouldUseLegacyProxy()) { + return LegacyEventDispatcherProxy::decorate($ed->reveal()); } + + return $ed->reveal(); } } diff --git a/tests/Functional/Command/LoadFixturesCommandTest.php b/tests/Functional/Command/LoadFixturesCommandTest.php index 9527257..05070eb 100644 --- a/tests/Functional/Command/LoadFixturesCommandTest.php +++ b/tests/Functional/Command/LoadFixturesCommandTest.php @@ -11,7 +11,7 @@ class LoadFixturesCommandTest extends AppTestCase { - /** @var Database $conn */ + /** @var Database */ private $conn; protected function setUp(): void diff --git a/tests/Unit/Services/ClientRegistryTest.php b/tests/Unit/Services/ClientRegistryTest.php index 47b526f..24631c9 100644 --- a/tests/Unit/Services/ClientRegistryTest.php +++ b/tests/Unit/Services/ClientRegistryTest.php @@ -5,6 +5,7 @@ namespace Facile\MongoDbBundle\Tests\Unit\Services; use Facile\MongoDbBundle\Event\ConnectionEvent; +use Facile\MongoDbBundle\Event\EventDispatcherCheck; use Facile\MongoDbBundle\Services\ClientRegistry; use PHPUnit\Framework\TestCase; use Prophecy\Argument; @@ -98,7 +99,7 @@ private function mockEventDispatcher(): EventDispatcherInterface { $ed = $this->prophesize(EventDispatcherInterface::class); - if (class_exists(LegacyEventDispatcherProxy::class)) { + if (EventDispatcherCheck::isPSR14Compliant()) { $ed->dispatch(Argument::type(ConnectionEvent::class), ConnectionEvent::CLIENT_CREATED) ->shouldBeCalledOnce() ->willReturnArgument(0); @@ -107,6 +108,10 @@ private function mockEventDispatcher(): EventDispatcherInterface ->shouldBeCalledOnce(); } + if (EventDispatcherCheck::shouldUseLegacyProxy()) { + return LegacyEventDispatcherProxy::decorate($ed->reveal()); + } + return $ed->reveal(); } }