Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #740 from doctrine/FilterCollectionEnhancement

Synchronized support of FilterCollection with ODM by adding missing method
  • Loading branch information...
commit 0e010994a7588de868dc57b49d081a8a9a6e9b1d 2 parents 7055ccb + d4814de
@guilhermeblanco guilhermeblanco authored
View
21 lib/Doctrine/ORM/Query/FilterCollection.php
@@ -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
@@ -154,7 +156,7 @@ 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.");
}
@@ -162,6 +164,18 @@ public function getFilter($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.
*
* @param string $name Name of the filter.
@@ -194,6 +208,7 @@ public function getHash()
}
$filterHash = '';
+
foreach ($this->enabledFilters as $name => $filter) {
$filterHash .= $name . $filter;
}
View
95 tests/Doctrine/Tests/ORM/Query/FilterCollectionTest.php
@@ -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');
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.