From 193c3abf0e5f7a7e2699e7c7ac37c1e713d79ef8 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Sun, 20 Feb 2022 14:09:05 +0100 Subject: [PATCH] Bring `FilterCollection` to a "clean" state after hash computation (#9523) Co-authored-by: Alexander M. Turek --- lib/Doctrine/ORM/Query/FilterCollection.php | 9 ++++--- phpstan-baseline.neon | 5 ---- .../Tests/ORM/Query/FilterCollectionTest.php | 25 ++++++++++++++++--- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/Doctrine/ORM/Query/FilterCollection.php b/lib/Doctrine/ORM/Query/FilterCollection.php index 7e9ac71bab9..87471cf1117 100644 --- a/lib/Doctrine/ORM/Query/FilterCollection.php +++ b/lib/Doctrine/ORM/Query/FilterCollection.php @@ -97,8 +97,7 @@ public function enable($name) // Keep the enabled filters sorted for the hash ksort($this->enabledFilters); - // Now the filter collection is dirty - $this->filtersState = self::FILTERS_STATE_DIRTY; + $this->setFiltersStateDirty(); } return $this->enabledFilters[$name]; @@ -120,8 +119,7 @@ public function disable($name) unset($this->enabledFilters[$name]); - // Now the filter collection is dirty - $this->filtersState = self::FILTERS_STATE_DIRTY; + $this->setFiltersStateDirty(); return $filter; } @@ -194,6 +192,9 @@ public function getHash() $filterHash .= $name . $filter; } + $this->filterHash = $filterHash; + $this->filtersState = self::FILTERS_STATE_CLEAN; + return $filterHash; } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c35eafef735..3fa8ee23057 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -875,11 +875,6 @@ parameters: count: 1 path: lib/Doctrine/ORM/Query/Expr/Select.php - - - message: "#^Property Doctrine\\\\ORM\\\\Query\\\\FilterCollection\\:\\:\\$filterHash is never written, only read\\.$#" - count: 1 - path: lib/Doctrine/ORM/Query/FilterCollection.php - - message: "#^Else branch is unreachable because ternary operator condition is always true\\.$#" count: 3 diff --git a/tests/Doctrine/Tests/ORM/Query/FilterCollectionTest.php b/tests/Doctrine/Tests/ORM/Query/FilterCollectionTest.php index 304c31d169d..b65d857e1c3 100644 --- a/tests/Doctrine/Tests/ORM/Query/FilterCollectionTest.php +++ b/tests/Doctrine/Tests/ORM/Query/FilterCollectionTest.php @@ -4,17 +4,18 @@ namespace Doctrine\Tests\ORM\Query; -use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Query\Filter\SQLFilter; +use Doctrine\Tests\Mocks\EntityManagerMock; use Doctrine\Tests\OrmTestCase; +use InvalidArgumentException; /** * Test case for FilterCollection */ class FilterCollectionTest extends OrmTestCase { - /** @var EntityManagerInterface */ + /** @var EntityManagerMock */ private $em; protected function setUp(): void @@ -64,7 +65,7 @@ public function testIsEnabled(): void public function testGetFilterInvalidArgument(): void { - $this->expectException('InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); $filterCollection = $this->em->getFilters(); $filterCollection->getFilter('testFilter'); } @@ -76,6 +77,24 @@ public function testGetFilter(): void self::assertInstanceOf(MyFilter::class, $filterCollection->getFilter('testFilter')); } + + public function testHashing(): void + { + $filterCollection = $this->em->getFilters(); + + self::assertTrue($filterCollection->isClean()); + + $oldHash = $filterCollection->getHash(); + $filterCollection->enable('testFilter'); + + self::assertFalse($filterCollection->isClean()); + + $hash = $filterCollection->getHash(); + + self::assertNotSame($oldHash, $hash); + self::assertTrue($filterCollection->isClean()); + self::assertSame($hash, $filterCollection->getHash()); + } } class MyFilter extends SQLFilter