From b867744f15b40e19604056481c388a7606f1a728 Mon Sep 17 00:00:00 2001
From: Alexander <iam.asm89@gmail.com>
Date: Fri, 22 Jul 2011 13:08:49 +0200
Subject: [PATCH] [DDC-551] Added tests for SQLFilter functionality + small
 fixes

---
 lib/Doctrine/ORM/EntityManager.php            |   8 +-
 lib/Doctrine/ORM/Query/Filter/SQLFilter.php   |   1 +
 .../Tests/ORM/Functional/SQLFilterTest.php    | 154 ++++++++++++++++++
 3 files changed, 159 insertions(+), 4 deletions(-)
 create mode 100644 tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php

diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php
index c127eefff..2605e0cec 100644
--- a/lib/Doctrine/ORM/EntityManager.php
+++ b/lib/Doctrine/ORM/EntityManager.php
@@ -115,7 +115,7 @@ class EntityManager implements ObjectManager
      *
      * @var array
      */
-    private $enabledFilters;
+    private $enabledFilters = array();
 
     /**
      * Creates a new EntityManager that operates on the given database connection
@@ -750,15 +750,15 @@ class EntityManager implements ObjectManager
     /** @return SQLFilter[] */
     public function getEnabledFilters()
     {
-        return $enabledFilters;
+        return $this->enabledFilters;
     }
 
     /** Throws exception if filter does not exist. No-op if the filter is alrady enabled.
     * @return SQLFilter */
     public function enableFilter($name)
     {
-        if(null === $filterClass = $this->config->getFilter($name)) {
-            throw new \InvalidArgumentException("Filter '" . $name . "' is does not exist.");
+        if(null === $filterClass = $this->config->getFilterClassName($name)) {
+            throw new \InvalidArgumentException("Filter '" . $name . "' does not exist.");
         }
 
         if(!isset($this->enabledFilters[$name])) {
diff --git a/lib/Doctrine/ORM/Query/Filter/SQLFilter.php b/lib/Doctrine/ORM/Query/Filter/SQLFilter.php
index 454c554c1..9f7307445 100644
--- a/lib/Doctrine/ORM/Query/Filter/SQLFilter.php
+++ b/lib/Doctrine/ORM/Query/Filter/SQLFilter.php
@@ -20,6 +20,7 @@
 namespace Doctrine\ORM\Query\Filter;
 
 use Doctrine\DBAL\Connection;
+use Doctrine\ORM\Mapping\ClassMetaData;
 
 /**
  * The base class that user defined filters should extend.
diff --git a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php
new file mode 100644
index 000000000..3cb3ce426
--- /dev/null
+++ b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php
@@ -0,0 +1,154 @@
+<?php
+
+namespace Doctrine\Tests\ORM\Functional;
+
+use Doctrine\ORM\Query\Filter\SQLFilter;
+use Doctrine\ORM\Mapping\ClassMetaData;
+
+require_once __DIR__ . '/../../TestInit.php';
+
+/**
+ * Tests SQLFilter functionality.
+ *
+ * @author Alexander <iam.asm89@gmail.com>
+ */
+class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
+{
+    public function setUp()
+    {
+        parent::setUp();
+    }
+
+    public function testConfigureFilter()
+    {
+        $config = new \Doctrine\ORM\Configuration();
+
+        $config->addFilter("locale", "\Doctrine\Tests\ORM\Functional\MyLocaleFilter");
+
+        $this->assertEquals("\Doctrine\Tests\ORM\Functional\MyLocaleFilter", $config->getFilterClassName("locale"));
+        $this->assertNull($config->getFilterClassName("foo"));
+    }
+
+    public function testEntityManagerEnableFilter()
+    {
+        $em = $this->_getEntityManager();
+        $this->configureFilters($em);
+
+        // Enable an existing filter
+        $filter = $em->enableFilter("locale");
+        $this->assertTrue($filter instanceof \Doctrine\Tests\ORM\Functional\MyLocaleFilter);
+
+        // Enable the filter again
+        $filter2 = $em->enableFilter("locale");
+        $this->assertEquals($filter, $filter2);
+
+        // Enable a non-existing filter
+        $exceptionThrown = false;
+        try {
+            $filter = $em->enableFilter("foo");
+        } catch (\InvalidArgumentException $e) {
+            $exceptionThrown = true;
+        }
+        $this->assertTrue($exceptionThrown);
+    }
+
+    public function testEntityManagerEnabledFilters()
+    {
+        $em = $this->_getEntityManager();
+
+        // No enabled filters
+        $this->assertEquals(array(), $em->getEnabledFilters());
+
+        $this->configureFilters($em);
+        $filter = $em->enableFilter("locale");
+        $filter = $em->enableFilter("soft_delete");
+
+        // Two enabled filters
+        $this->assertEquals(2, count($em->getEnabledFilters()));
+
+    }
+
+    public function testEntityManagerDisableFilter()
+    {
+        $em = $this->_getEntityManager();
+        $this->configureFilters($em);
+
+        // Enable the filter
+        $filter = $em->enableFilter("locale");
+
+        // Disable it
+        $this->assertEquals($filter, $em->disableFilter("locale"));
+        $this->assertEquals(0, count($em->getEnabledFilters()));
+
+        // Disable a non-existing filter
+        $exceptionThrown = false;
+        try {
+            $filter = $em->disableFilter("foo");
+        } catch (\InvalidArgumentException $e) {
+            $exceptionThrown = true;
+        }
+        $this->assertTrue($exceptionThrown);
+
+        // Disable a non-enabled filter
+        $exceptionThrown = false;
+        try {
+            $filter = $em->disableFilter("locale");
+        } catch (\InvalidArgumentException $e) {
+            $exceptionThrown = true;
+        }
+        $this->assertTrue($exceptionThrown);
+    }
+
+    public function testEntityManagerGetFilter()
+    {
+        $em = $this->_getEntityManager();
+        $this->configureFilters($em);
+
+        // Enable the filter
+        $filter = $em->enableFilter("locale");
+
+        // Get the filter
+        $this->assertEquals($filter, $em->getFilter("locale"));
+
+        // Get a non-enabled filter
+        $exceptionThrown = false;
+        try {
+            $filter = $em->getFilter("soft_delete");
+        } catch (\InvalidArgumentException $e) {
+            $exceptionThrown = true;
+        }
+        $this->assertTrue($exceptionThrown);
+    }
+
+    protected function configureFilters($em)
+    {
+        // Add filters to the configuration of the EM
+        $config = $em->getConfiguration();
+        $config->addFilter("locale", "\Doctrine\Tests\ORM\Functional\MyLocaleFilter");
+        $config->addFilter("soft_delete", "\Doctrine\Tests\ORM\Functional\MySoftDeleteFilter");
+    }
+}
+
+class MySoftDeleteFilter extends SQLFilter
+{
+    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
+    {
+        if ($targetEntity->name != "MyEntity\SoftDeleteNewsItem") {
+            return "";
+        }
+
+        return $targetTableAlias.'.deleted = 0';
+    }
+}
+
+class MyLocaleFilter extends SQLFilter
+{
+    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
+    {
+        if (!in_array("LocaleAware", $targetEntity->reflClass->getInterfaceNames())) {
+            return "";
+        }
+
+        return $targetTableAlias.'.locale = ' . $this->getParam('locale'); // getParam uses connection to quote the value.
+    }
+}