1
0
mirror of synced 2025-01-18 22:41:43 +03:00

Merge pull request #740 from doctrine/FilterCollectionEnhancement

Synchronized support of FilterCollection with ODM by adding missing method
This commit is contained in:
Guilherme Blanco 2013-07-29 18:44:10 -07:00
commit 0e010994a7
2 changed files with 113 additions and 3 deletions

View File

@ -104,11 +104,13 @@ class FilterCollection
*/
public function enable($name)
{
if (null === $filterClass = $this->config->getFilterClassName($name)) {
if ( ! $this->has($name)) {
throw new \InvalidArgumentException("Filter '" . $name . "' does not exist.");
}
if (!isset($this->enabledFilters[$name])) {
if ( ! $this->isEnabled($name)) {
$filterClass = $this->config->getFilterClassName($name);
$this->enabledFilters[$name] = new $filterClass($this->em);
// Keep the enabled filters sorted for the hash
@ -154,13 +156,25 @@ class FilterCollection
*/
public function getFilter($name)
{
if (!isset($this->enabledFilters[$name])) {
if ( ! $this->isEnabled($name)) {
throw new \InvalidArgumentException("Filter '" . $name . "' is not enabled.");
}
return $this->enabledFilters[$name];
}
/**
* Checks whether filter with given name is defined.
*
* @param string $name Name of the filter.
*
* @return bool true if the filter exists, false if not.
*/
public function has($name)
{
return null !== $this->config->getFilterClassName($name);
}
/**
* Checks if a filter is enabled.
*
@ -194,6 +208,7 @@ class FilterCollection
}
$filterHash = '';
foreach ($this->enabledFilters as $name => $filter) {
$filterHash .= $name . $filter;
}

View File

@ -0,0 +1,95 @@
<?php
namespace Doctrine\Tests\ORM\Query;
require_once __DIR__ . '/../../TestInit.php';
use Doctrine\ORM\Mapping\ClassMetaData,
Doctrine\ORM\Query\Filter\SQLFilter;
/**
* Test case for FilterCollection
*
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
*/
class FilterCollectionTest extends \Doctrine\Tests\OrmTestCase
{
/**
* @var \Doctrine\ORM\EntityManager
*/
private $em;
protected function setUp()
{
$this->em = $this->_getTestEntityManager();
$this->em->getConfiguration()->addFilter('testFilter', 'Doctrine\Tests\ORM\Query\MyFilter');
}
public function testEnable()
{
$filterCollection = $this->em->getFilters();
$this->assertCount(0, $filterCollection->getEnabledFilters());
$filterCollection->enable('testFilter');
$enabledFilters = $filterCollection->getEnabledFilters();
$this->assertCount(1, $enabledFilters);
$this->assertContainsOnly('Doctrine\Tests\ORM\Query\MyFilter', $enabledFilters);
$filterCollection->disable('testFilter');
$this->assertCount(0, $filterCollection->getEnabledFilters());
}
public function testHasFilter()
{
$filterCollection = $this->em->getFilters();
$this->assertTrue($filterCollection->has('testFilter'));
$this->assertFalse($filterCollection->has('fakeFilter'));
}
/**
* @depends testEnable
*/
public function testIsEnabled()
{
$filterCollection = $this->em->getFilters();
$this->assertFalse($filterCollection->isEnabled('testFilter'));
$filterCollection->enable('testFilter');
$this->assertTrue($filterCollection->isEnabled('testFilter'));
}
/**
* @expectedException InvalidArgumentException
*/
public function testGetFilterInvalidArgument()
{
$filterCollection = $this->em->getFilters();
$filterCollection->getFilter('testFilter');
}
public function testGetFilter()
{
$filterCollection = $this->em->getFilters();
$filterCollection->enable('testFilter');
$this->assertInstanceOf('Doctrine\Tests\ORM\Query\MyFilter', $filterCollection->getFilter('testFilter'));
}
}
class MyFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
// getParameter applies quoting automatically
return $targetTableAlias . '.id = ' . $this->getParameter('id');
}
}