diff --git a/lib/Doctrine/ORM/Query/Filter/SQLFilter.php b/lib/Doctrine/ORM/Query/Filter/SQLFilter.php index 9f7307445..9477636e3 100644 --- a/lib/Doctrine/ORM/Query/Filter/SQLFilter.php +++ b/lib/Doctrine/ORM/Query/Filter/SQLFilter.php @@ -45,17 +45,19 @@ abstract class SQLFilter final function setParameter($name, $value, $type) { // @todo: check for a valid type? - $parameters[$name] = array('value' => $value, 'type' => $type); + $this->parameters[$name] = array('value' => $value, 'type' => $type); + + return $this; } final function getParameter($name) { - if(!isset($parameters[$name])) { - throw new \InvalidArgumentException("Parameter '" . $name . "' is does not exist."); + if(!isset($this->parameters[$name])) { + throw new \InvalidArgumentException("Parameter '" . $name . "' does not exist."); } // @todo: espace the parameter - return $paramaters[$name]['value']; + return $this->conn->convertToDatabaseValue($this->parameters[$name]['value'], $this->parameters[$name]['type']); } abstract function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias); diff --git a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php index 3cb3ce426..482ddb534 100644 --- a/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php @@ -4,6 +4,7 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\ORM\Query\Filter\SQLFilter; use Doctrine\ORM\Mapping\ClassMetaData; +use Doctrine\DBAL\Types\Type; require_once __DIR__ . '/../../TestInit.php'; @@ -127,6 +128,51 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase $config->addFilter("locale", "\Doctrine\Tests\ORM\Functional\MyLocaleFilter"); $config->addFilter("soft_delete", "\Doctrine\Tests\ORM\Functional\MySoftDeleteFilter"); } + + protected function getMockConnection() + { + // Setup connection mock + $conn = $this->getMockBuilder('Doctrine\DBAL\Connection') + ->disableOriginalConstructor() + ->getMock(); + + return $conn; + } + + public function testSQLFilterGetSetParameter() + { + // Setup mock connection + $conn = $this->getMockConnection(); + $conn->expects($this->once()) + ->method('convertToDatabaseValue') + ->with($this->equalTo('en')) + ->will($this->returnValue("'en'")); + + $filter = new MyLocaleFilter($conn); + + $filter->setParameter('locale', 'en', Type::STRING); + + $this->assertEquals("'en'", $filter->getParameter('locale')); + } + + public function testSQLFilterAddConstraint() + { + // Set up metadata mock + $targetEntity = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata') + ->disableOriginalConstructor() + ->getMock(); + + $filter = new MySoftDeleteFilter($this->getMockConnection()); + + // Test for an entity that gets extra filter data + $targetEntity->name = 'MyEntity\SoftDeleteNewsItem'; + $this->assertEquals('t1_.deleted = 0', $filter->addFilterConstraint($targetEntity, 't1_')); + + // Test for an entity that doesn't get extra filter data + $targetEntity->name = 'MyEntity\NoSoftDeleteNewsItem'; + $this->assertEquals('', $filter->addFilterConstraint($targetEntity, 't1_')); + + } } class MySoftDeleteFilter extends SQLFilter