From 6c7f4c0e290b1b92206773076c630182d5c4bda2 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Wed, 3 Mar 2021 19:31:03 +0100 Subject: [PATCH] Allow php 8 (#114) by @Jean85 --- .github/workflows/ci.yaml | 3 ++ CHANGELOG.md | 4 +++ README.MD | 2 +- composer.json | 4 +-- phpstan-baseline.neon | 35 ------------------- src/Capsule/Collection.php | 22 +++++------- src/DataCollector/MongoQuerySerializer.php | 26 +++++++------- tests/Functional/Capsule/CollectionTest.php | 26 +++++++------- tests/Unit/Capsule/DatabaseTest.php | 8 ++--- .../MongoDbDataCollectorTest.php | 14 ++++---- 10 files changed, 54 insertions(+), 90 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 52df4f80..fad27b77 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -64,6 +64,9 @@ jobs: - php: 7.4 mongo-ext: 1.9.0 mongo-img: 4.4 + - php: 8.0 + mongo-ext: 1.9.0 + mongo-img: 4.4 steps: - name: Checkout diff --git a/CHANGELOG.md b/CHANGELOG.md index 72a77f28..d4956d19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## Unreleased +### Added +* PHP 8 support (#114) by [@Jean85](https://github.com/Jean85). + ## 1.3.0 (2021-02-28) ### Added * This changelog with (#109) by [@Jean85](https://github.com/Jean85). diff --git a/README.MD b/README.MD index c2340948..80fb5798 100644 --- a/README.MD +++ b/README.MD @@ -2,7 +2,7 @@ Bundle service integration of official [mongodb/mongo-php-library](https://github.com/mongodb/mongo-php-library) driver library, ([mongodb/mongodb](https://packagist.org/packages/mongodb/mongodb) on packagist) -[![PHP](https://img.shields.io/badge/php-%5E7.2-blue.svg)](https://img.shields.io/badge/php-%5E7.0-blue.svg) +[![PHP](https://img.shields.io/badge/php-%5E7.2%7C%5E8.0-blue.svg)](https://img.shields.io/badge/php-%5E7.0-blue.svg) [![MongoDB](https://img.shields.io/badge/MongoDB-%5E3.0-lightgrey.svg)](https://img.shields.io/badge/MongoDB-%5E3.0-lightgrey.svg) [![ext-mongodb](https://img.shields.io/badge/ext_mongodb-%5E1.1.5-orange.svg)](https://img.shields.io/badge/ext_mongodb-%5E1.1.5-orange.svg) diff --git a/composer.json b/composer.json index 94371822..2e723a18 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ } ], "require": { - "php": "^7.2", + "php": "^7.2 || ^8.0", "ext-mongodb": "^1.1.5", "mongodb/mongodb": "^1.0", "symfony/framework-bundle": "^3.4 || ^4.3 || ^5.0" @@ -28,7 +28,7 @@ "matthiasnoback/symfony-dependency-injection-test": "^4", "symfony/web-profiler-bundle": "^3.4 || ^4.3 || ^5.0", "symfony/console": "^3.4 || ^4.3 || ^5.0", - "phpunit/phpunit": "^8.0", + "phpunit/phpunit": "^8.5.14", "symfony/phpunit-bridge": "^5.2", "facile-it/facile-coding-standard": "^0.4.0", "phpstan/phpstan": "^0.12", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 8d7f82f7..7c4a31d6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -900,11 +900,6 @@ parameters: count: 1 path: tests/Functional/Capsule/CollectionTest.php - - - message: "#^Parameter \\#6 \\$eventDispatcher of class Facile\\\\MongoDbBundle\\\\Capsule\\\\Collection constructor expects Symfony\\\\Component\\\\EventDispatcher\\\\EventDispatcherInterface, object given\\.$#" - count: 13 - path: tests/Functional/Capsule/CollectionTest.php - - message: "#^Method Facile\\\\MongoDbBundle\\\\Tests\\\\Functional\\\\Capsule\\\\CollectionTest\\:\\:test_insertOne\\(\\) has no return typehint specified\\.$#" count: 1 @@ -1140,41 +1135,11 @@ parameters: count: 1 path: tests/Unit/Capsule/ClientTest.php - - - message: "#^Method Facile\\\\MongoDbBundle\\\\Tests\\\\Unit\\\\Capsule\\\\DatabaseTest\\:\\:test_selectCollection\\(\\) has no return typehint specified\\.$#" - count: 1 - path: tests/Unit/Capsule/DatabaseTest.php - - - - message: "#^Parameter \\#5 \\$eventDispatcher of class Facile\\\\MongoDbBundle\\\\Capsule\\\\Database constructor expects Symfony\\\\Component\\\\EventDispatcher\\\\EventDispatcherInterface, object given\\.$#" - count: 2 - path: tests/Unit/Capsule/DatabaseTest.php - - - - message: "#^Method Facile\\\\MongoDbBundle\\\\Tests\\\\Unit\\\\Capsule\\\\DatabaseTest\\:\\:test_withOptions\\(\\) has no return typehint specified\\.$#" - count: 1 - path: tests/Unit/Capsule/DatabaseTest.php - - - - message: "#^Method Facile\\\\MongoDbBundle\\\\Tests\\\\unit\\\\DataCollector\\\\MongoDbDataCollectorTest\\:\\:test_construction_logger\\(\\) has no return typehint specified\\.$#" - count: 1 - path: tests/Unit/DataCollector/MongoDbDataCollectorTest.php - - - - message: "#^Method Facile\\\\MongoDbBundle\\\\Tests\\\\unit\\\\DataCollector\\\\MongoDbDataCollectorTest\\:\\:getUtcDateTime\\(\\) has no return typehint specified\\.$#" - count: 1 - path: tests/Unit/DataCollector/MongoDbDataCollectorTest.php - - message: "#^Parameter \\#1 \\$milliseconds of class MongoDB\\\\BSON\\\\UTCDateTime constructor expects DateTimeInterface\\|int, string given\\.$#" count: 1 path: tests/Unit/DataCollector/MongoDbDataCollectorTest.php - - - message: "#^Parameter \\#1 \\$version1 of function version_compare expects string, string\\|false given\\.$#" - count: 1 - path: tests/Unit/DataCollector/MongoDbDataCollectorTest.php - - message: "#^Method Facile\\\\MongoDbBundle\\\\Tests\\\\Unit\\\\DataCollector\\\\MongoQuerySerializerTest\\:\\:test_serializer\\(\\) has no return typehint specified\\.$#" count: 1 diff --git a/src/Capsule/Collection.php b/src/Capsule/Collection.php index ebc31e86..49e84757 100644 --- a/src/Capsule/Collection.php +++ b/src/Capsule/Collection.php @@ -13,8 +13,6 @@ use Symfony\Component\HttpKernel\Kernel; /** - * Class Collection. - * * @internal */ final class Collection extends MongoCollection @@ -29,16 +27,12 @@ final class Collection extends MongoCollection private $databaseName; /** - * Collection constructor. - * * @param Manager $manager * @param string $clientName * @param string $databaseName * @param string $collectionName * @param array $options * @param EventDispatcherInterface $eventDispatcher - * - * @internal param DataCollectorLoggerInterface $logger */ public function __construct( Manager $manager, @@ -59,7 +53,7 @@ public function __construct( */ public function aggregate(array $pipeline, array $options = []) { - $query = $this->prepareQuery(__FUNCTION__, null, $pipeline, $options); + $query = $this->prepareQuery(__FUNCTION__, [], $pipeline, $options); $result = parent::aggregate($query->getData(), $query->getOptions()); $this->notifyQueryExecution($query); @@ -71,7 +65,7 @@ public function aggregate(array $pipeline, array $options = []) */ public function count($filter = [], array $options = []) { - $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); + $query = $this->prepareQuery(__FUNCTION__, $filter, [], $options); $result = parent::count($query->getFilters(), $query->getOptions()); $this->notifyQueryExecution($query); @@ -83,7 +77,7 @@ public function count($filter = [], array $options = []) */ public function find($filter = [], array $options = []) { - $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); + $query = $this->prepareQuery(__FUNCTION__, $filter, [], $options); $result = parent::find($query->getFilters(), $query->getOptions()); $this->notifyQueryExecution($query); @@ -95,7 +89,7 @@ public function find($filter = [], array $options = []) */ public function findOne($filter = [], array $options = []) { - $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); + $query = $this->prepareQuery(__FUNCTION__, $filter, [], $options); $result = parent::findOne($query->getFilters(), $query->getOptions()); $this->notifyQueryExecution($query); @@ -119,7 +113,7 @@ public function findOneAndUpdate($filter, $update, array $options = []) */ public function findOneAndDelete($filter, array $options = []) { - $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); + $query = $this->prepareQuery(__FUNCTION__, $filter, [], $options); $result = parent::findOneAndDelete($query->getFilters(), $query->getOptions()); $this->notifyQueryExecution($query); @@ -131,7 +125,7 @@ public function findOneAndDelete($filter, array $options = []) */ public function deleteMany($filter, array $options = []) { - $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); + $query = $this->prepareQuery(__FUNCTION__, $filter, [], $options); $result = parent::deleteMany($query->getFilters(), $query->getOptions()); $this->notifyQueryExecution($query); @@ -143,7 +137,7 @@ public function deleteMany($filter, array $options = []) */ public function deleteOne($filter, array $options = []) { - $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); + $query = $this->prepareQuery(__FUNCTION__, $filter, [], $options); $result = parent::deleteOne($query->getFilters(), $query->getOptions()); $this->notifyQueryExecution($query); @@ -206,7 +200,7 @@ public function distinct($fieldName, $filter = [], array $options = []) * * @return Query */ - private function prepareQuery(string $method, $filters = null, $data = null, array $options): Query + private function prepareQuery(string $method, $filters, $data, array $options): Query { $query = new Query(); $query->setFilters($filters ?? []); diff --git a/src/DataCollector/MongoQuerySerializer.php b/src/DataCollector/MongoQuerySerializer.php index b6e99574..7fb2791e 100644 --- a/src/DataCollector/MongoQuerySerializer.php +++ b/src/DataCollector/MongoQuerySerializer.php @@ -58,23 +58,23 @@ public static function prepareItemData($item) return self::prepareUnserializableData((array) $item); } - if (method_exists($item, 'getArrayCopy')) { - return self::prepareUnserializableData($item->getArrayCopy()); - } + if (\is_object($item)) { + if (method_exists($item, 'getArrayCopy')) { + return self::prepareUnserializableData($item->getArrayCopy()); + } - if (method_exists($item, 'toDateTime')) { - return 'ISODate("' . $item->toDateTime()->format('c') . '")'; - } + if (method_exists($item, 'toDateTime')) { + return 'ISODate("' . $item->toDateTime()->format('c') . '")'; + } - if (method_exists($item, '__toString')) { - return $item->__toString(); - } + if (method_exists($item, '__toString')) { + return $item->__toString(); + } - if ($item instanceof Serializable) { - return $item->bsonSerialize(); - } + if ($item instanceof Serializable) { + return $item->bsonSerialize(); + } - if (\is_object($item)) { return self::prepareUnserializableData((array) $item); } diff --git a/tests/Functional/Capsule/CollectionTest.php b/tests/Functional/Capsule/CollectionTest.php index 61ada3fc..c16c39ef 100644 --- a/tests/Functional/Capsule/CollectionTest.php +++ b/tests/Functional/Capsule/CollectionTest.php @@ -27,7 +27,7 @@ private function getManager(): Manager public function test_construction() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -37,7 +37,7 @@ public function test_construction() public function test_insertOne() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -48,7 +48,7 @@ public function test_insertOne() public function test_updateOne() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -59,7 +59,7 @@ public function test_updateOne() public function test_count() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -70,7 +70,7 @@ public function test_count() public function test_find() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -81,7 +81,7 @@ public function test_find() public function test_findOne() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -92,7 +92,7 @@ public function test_findOne() public function test_findOneAndUpdate() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -103,7 +103,7 @@ public function test_findOneAndUpdate() public function test_findOneAndDelete() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -114,7 +114,7 @@ public function test_findOneAndDelete() public function test_deleteOne() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -125,7 +125,7 @@ public function test_deleteOne() public function test_replaceOne() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -136,7 +136,7 @@ public function test_replaceOne() public function test_aggregate() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -167,7 +167,7 @@ public function test_aggregate() public function test_deleteMany() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); @@ -178,7 +178,7 @@ public function test_deleteMany() public function test_distinct() { $manager = $this->getManager(); - $ev = self::prophesize(EventDispatcherInterface::class); + $ev = $this->prophesize(EventDispatcherInterface::class); $this->assertEventsDispatching($ev); $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); diff --git a/tests/Unit/Capsule/DatabaseTest.php b/tests/Unit/Capsule/DatabaseTest.php index 0e9efbcf..0df6407d 100644 --- a/tests/Unit/Capsule/DatabaseTest.php +++ b/tests/Unit/Capsule/DatabaseTest.php @@ -13,10 +13,10 @@ class DatabaseTest extends TestCase { - public function test_selectCollection() + public function test_selectCollection(): void { $manager = new Manager('mongodb://localhost'); - $logger = self::prophesize(EventDispatcherInterface::class); + $logger = $this->prophesize(EventDispatcherInterface::class); $db = new Database($manager, 'client_name', 'testdb', [], $logger->reveal()); self::assertInstanceOf(\MongoDB\Database::class, $db); @@ -30,10 +30,10 @@ public function test_selectCollection() self::assertEquals('testdb', $debugInfo['databaseName']); } - public function test_withOptions() + public function test_withOptions(): void { $manager = new Manager('mongodb://localhost'); - $logger = self::prophesize(EventDispatcherInterface::class); + $logger = $this->prophesize(EventDispatcherInterface::class); $db = new Database($manager, 'client_name', 'testdb', [], $logger->reveal()); self::assertInstanceOf(\MongoDB\Database::class, $db); diff --git a/tests/Unit/DataCollector/MongoDbDataCollectorTest.php b/tests/Unit/DataCollector/MongoDbDataCollectorTest.php index def993e0..9e52cfa0 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; @@ -15,7 +15,7 @@ class MongoDbDataCollectorTest extends TestCase { - public function test_construction_logger() + public function test_construction_logger(): void { $logEvent = new Query(); $logEvent->setData( @@ -27,7 +27,7 @@ public function test_construction_logger() $logger = new MongoQueryLogger(); $logger->logQuery($logEvent); - $logger->addConnection('test_conenction'); + $logger->addConnection('test_connection'); $collector = new MongoDbDataCollector(); $collector->setLogger($logger); @@ -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,14 +45,12 @@ public function test_construction_logger() self::assertEquals('mongodb', $collector->getName()); } - public function getUtcDateTime() + public function getUtcDateTime(): UTCDateTime { if (phpversion('mongodb') === '1.2.0-dev') { return new UTCDateTime('1000'); } - if (-1 === version_compare(phpversion('mongodb'), '1.2.0')) { - return new UTCDateTime(1000); - } + return new UTCDateTime(1000); } }