[DDC-551] Added tests for SQLFilter
This commit is contained in:
parent
b867744f15
commit
277fc751b6
@ -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);
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user