diff --git a/.travis.yml b/.travis.yml index dfd15a0..6002f3c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,7 @@ env: - PHP_IMG="php-7.2-mongoext-1.3.0-20200327" MONGO_IMG="mongo:3.4" SYMFONY="3.4.*" - PHP_IMG="php-7.2-mongoext-1.3.0-20200327" MONGO_IMG="mongo:3.4" SYMFONY="4.3.*" - PHP_IMG="php-7.2-mongoext-1.3.0-20200327" MONGO_IMG="mongo:3.4" SYMFONY="4.4.*" + - PHP_IMG="php-7.2-mongoext-1.3.0-20200327" MONGO_IMG="mongo:3.4" SYMFONY="5.*" - PHP_IMG="php-7.2-mongoext-1.5.3-20200327" MONGO_IMG="percona/percona-server-mongodb:4.0" - PHP_IMG="php-7.3-mongoext-1.5.3-20200327" MONGO_IMG="mongo:4.0" - PHP_IMG="php-7.3-mongoext-1.5.3-20200327" MONGO_IMG="percona/percona-server-mongodb:4.0" diff --git a/composer.json b/composer.json index 6c9f665..e861866 100644 --- a/composer.json +++ b/composer.json @@ -22,14 +22,14 @@ "php": "^7.2", "ext-mongodb": "^1.1.5", "mongodb/mongodb": "^1.0", - "symfony/framework-bundle": "^3.4 || ^4.3" + "symfony/framework-bundle": "^3.4 || ^4.3 || ^5.0" }, "require-dev": { "matthiasnoback/symfony-dependency-injection-test": "^4", - "symfony/web-profiler-bundle": "^3.4 || ^4.3", - "symfony/console": "^3.4 || ^4.3", + "symfony/web-profiler-bundle": "^3.4 || ^4.3 || ^5.0", + "symfony/console": "^3.4 || ^4.3 || ^5.0", "phpunit/phpunit": "^8.5.2", - "symfony/phpunit-bridge": "^4.2", + "symfony/phpunit-bridge": "^5.0", "facile-it/facile-coding-standard": "^0.3.1", "phpstan/phpstan": "^0.12" }, diff --git a/docker-compose.yml b/docker-compose.yml index 2fdf503..a55c256 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: "3.1" services: php: - image: ilariopierbattista/mongodb-bundle-php:base-7.2-mongoext-1.3.0 + image: ilariopierbattista/mongodb-bundle-php:php-7.2-mongoext-1.3.0-20200327 volumes: - ./:/home/user-dev/project tty: true diff --git a/src/DataCollector/MongoDbDataCollector.php b/src/DataCollector/MongoDbDataCollector.php index c67d1a3..7ed47e3 100644 --- a/src/DataCollector/MongoDbDataCollector.php +++ b/src/DataCollector/MongoDbDataCollector.php @@ -51,8 +51,16 @@ public function setLogger(DataCollectorLoggerInterface $logger) $this->logger = $logger; } - public function collect(Request $request, Response $response, \Exception $exception = null) + public function collect(Request $request, Response $response, $exception = null) { + if ($exception && ! $exception instanceof \Throwable) { + throw new \InvalidArgumentException(sprintf( + 'Argument 3 passed to %s must be an instance of \Throwable or null, %s given', + __METHOD__, + is_object($exception) ? 'instance of ' . get_class($exception) : gettype($exception) + )); + } + while ($this->logger->hasLoggedEvents()) { /** @var Query $event */ $event = $this->logger->getLoggedEvent(); diff --git a/tests/Unit/DataCollector/MongoDbDataCollectorTest.php b/tests/Unit/DataCollector/MongoDbDataCollectorTest.php index def993e..6530d49 100644 --- a/tests/Unit/DataCollector/MongoDbDataCollectorTest.php +++ b/tests/Unit/DataCollector/MongoDbDataCollectorTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Facile\MongoDbBundle\Tests\unit\DataCollector; +namespace Facile\MongoDbBundle\Tests\Unit\DataCollector; use Facile\MongoDbBundle\DataCollector\MongoDbDataCollector; use Facile\MongoDbBundle\Models\Query; @@ -37,7 +37,7 @@ public function test_construction_logger() self::assertEquals(1, $collector->getQueryCount()); self::assertNotEmpty($collector->getQueries()); - self::assertTrue(is_float($collector->getTime())); + self::assertIsFloat($collector->getTime()); self::assertNotEmpty($collector->getConnections()); self::assertEquals(1, $collector->getConnectionsCount()); @@ -45,6 +45,21 @@ public function test_construction_logger() self::assertEquals('mongodb', $collector->getName()); } + public function testCollectWithWrongType(): void + { + $collector = new MongoDbDataCollector(); + $collector->setLogger(new MongoQueryLogger()); + + self::expectException(\InvalidArgumentException::class); + self::expectExceptionMessage('must be an instance of \\Throwable'); + + $collector->collect( + $this->prophesize(Request::class)->reveal(), + $this->prophesize(Response::class)->reveal(), + new \DateTime() + ); + } + public function getUtcDateTime() { if (phpversion('mongodb') === '1.2.0-dev') { diff --git a/tests/Unit/Services/ClientRegistryTest.php b/tests/Unit/Services/ClientRegistryTest.php index dd303ab..47b526f 100644 --- a/tests/Unit/Services/ClientRegistryTest.php +++ b/tests/Unit/Services/ClientRegistryTest.php @@ -4,17 +4,18 @@ namespace Facile\MongoDbBundle\Tests\Unit\Services; +use Facile\MongoDbBundle\Event\ConnectionEvent; use Facile\MongoDbBundle\Services\ClientRegistry; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; class ClientRegistryTest extends TestCase { public function test_client_connection_url_provided_manually() { - $ed = $this->prophesize(EventDispatcherInterface::class); - - $registry = new ClientRegistry($ed->reveal(), false, null); + $registry = new ClientRegistry($this->mockEventDispatcher(), false, null); $testConf = [ 'test_client' => [ @@ -40,9 +41,7 @@ public function test_client_connection_url_provided_manually() public function test_client_connection_url_generation_singlehost() { - $ed = $this->prophesize(EventDispatcherInterface::class); - - $registry = new ClientRegistry($ed->reveal(), false, null); + $registry = new ClientRegistry($this->mockEventDispatcher(), false, null); $testConf = [ 'test_client' => [ @@ -70,9 +69,7 @@ public function test_client_connection_url_generation_singlehost() public function test_client_connection_url_generation_multihost() { - $ed = $this->prophesize(EventDispatcherInterface::class); - - $registry = new ClientRegistry($ed->reveal(), false, null); + $registry = new ClientRegistry($this->mockEventDispatcher(), false, null); $testConf = [ 'test_client' => [ @@ -96,4 +93,20 @@ public function test_client_connection_url_generation_multihost() $this->assertEquals('mongodb://host1:8080,host2:8081', $client->__debugInfo()['uri']); } + + private function mockEventDispatcher(): EventDispatcherInterface + { + $ed = $this->prophesize(EventDispatcherInterface::class); + + if (class_exists(LegacyEventDispatcherProxy::class)) { + $ed->dispatch(Argument::type(ConnectionEvent::class), ConnectionEvent::CLIENT_CREATED) + ->shouldBeCalledOnce() + ->willReturnArgument(0); + } else { + $ed->dispatch(ConnectionEvent::CLIENT_CREATED, Argument::type(ConnectionEvent::class)) + ->shouldBeCalledOnce(); + } + + return $ed->reveal(); + } }