Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronized support of FilterCollection with ODM by adding missing method #740

Merged
merged 1 commit into from
Jul 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/Doctrine/ORM/Query/FilterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ public function getEnabledFilters()
*/
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
Expand Down Expand Up @@ -154,13 +156,25 @@ public function disable($name)
*/
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.
*
Expand Down Expand Up @@ -194,6 +208,7 @@ public function getHash()
}

$filterHash = '';

foreach ($this->enabledFilters as $name => $filter) {
$filterHash .= $name . $filter;
}
Expand Down
95 changes: 95 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/FilterCollectionTest.php
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/
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');
}
}