Skip to content

Commit

Permalink
Merge branch '2.11.x' into 2.12.x
Browse files Browse the repository at this point in the history
* 2.11.x:
  Bring `FilterCollection` to a "clean" state after hash computation (doctrine#9523)
  • Loading branch information
derrabus committed Feb 20, 2022
2 parents dac1875 + 193c3ab commit 05c35c3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
9 changes: 5 additions & 4 deletions lib/Doctrine/ORM/Query/FilterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
}
Expand Down Expand Up @@ -194,6 +192,9 @@ public function getHash()
$filterHash .= $name . $filter;
}

$this->filterHash = $filterHash;
$this->filtersState = self::FILTERS_STATE_CLEAN;

return $filterHash;
}

Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 22 additions & 3 deletions tests/Doctrine/Tests/ORM/Query/FilterCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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');
}
Expand All @@ -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
Expand Down

0 comments on commit 05c35c3

Please sign in to comment.