[DDC-551] Add type inference to SQLFilter::setParameter() + cleaned tests
This commit is contained in:
parent
29fabbd81f
commit
bd07f8d3dd
@ -19,8 +19,9 @@
|
|||||||
|
|
||||||
namespace Doctrine\ORM\Query\Filter;
|
namespace Doctrine\ORM\Query\Filter;
|
||||||
|
|
||||||
use Doctrine\ORM\Mapping\ClassMetaData;
|
use Doctrine\ORM\EntityManager,
|
||||||
use Doctrine\ORM\EntityManager;
|
Doctrine\ORM\Mapping\ClassMetaData,
|
||||||
|
Doctrine\ORM\Query\ParameterTypeInferer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base class that user defined filters should extend.
|
* The base class that user defined filters should extend.
|
||||||
@ -60,12 +61,18 @@ abstract class SQLFilter
|
|||||||
*
|
*
|
||||||
* @param string $name Name of the parameter.
|
* @param string $name Name of the parameter.
|
||||||
* @param string $value Value of the parameter.
|
* @param string $value Value of the parameter.
|
||||||
* @param string $type Type of the parameter.
|
* @param string $type The parameter type. If specified, the given value will be run through
|
||||||
|
* the type conversion of this type. This is usually not needed for
|
||||||
|
* strings and numeric types.
|
||||||
*
|
*
|
||||||
* @return SQLFilter The current SQL filter.
|
* @return SQLFilter The current SQL filter.
|
||||||
*/
|
*/
|
||||||
final public function setParameter($name, $value, $type)
|
final public function setParameter($name, $value, $type = null)
|
||||||
{
|
{
|
||||||
|
if (null === $type) {
|
||||||
|
$type = ParameterTypeInferer::inferType($value);
|
||||||
|
}
|
||||||
|
|
||||||
$this->parameters[$name] = array('value' => $value, 'type' => $type);
|
$this->parameters[$name] = array('value' => $value, 'type' => $type);
|
||||||
|
|
||||||
// Keep the parameters sorted for the hash
|
// Keep the parameters sorted for the hash
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Doctrine\Tests\ORM\Functional;
|
namespace Doctrine\Tests\ORM\Functional;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Types\Type;
|
||||||
use Doctrine\ORM\Query\Filter\SQLFilter;
|
use Doctrine\ORM\Query\Filter\SQLFilter;
|
||||||
use Doctrine\ORM\Mapping\ClassMetaData;
|
use Doctrine\ORM\Mapping\ClassMetaData;
|
||||||
use Doctrine\Common\Cache\ArrayCache;
|
use Doctrine\Common\Cache\ArrayCache;
|
||||||
@ -213,7 +214,33 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
|
|
||||||
$filter = new MyLocaleFilter($em);
|
$filter = new MyLocaleFilter($em);
|
||||||
|
|
||||||
$filter->setParameter('locale', 'en', \Doctrine\DBAL\Types\Type::STRING);
|
$filter->setParameter('locale', 'en', Type::STRING);
|
||||||
|
|
||||||
|
$this->assertEquals("'en'", $filter->getParameter('locale'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSQLFilterSetParameterInfersType()
|
||||||
|
{
|
||||||
|
// Setup mock connection
|
||||||
|
$conn = $this->getMockConnection();
|
||||||
|
$conn->expects($this->once())
|
||||||
|
->method('quote')
|
||||||
|
->with($this->equalTo('en'))
|
||||||
|
->will($this->returnValue("'en'"));
|
||||||
|
|
||||||
|
$em = $this->getMockEntityManager($conn);
|
||||||
|
$em->expects($this->once())
|
||||||
|
->method('getConnection')
|
||||||
|
->will($this->returnValue($conn));
|
||||||
|
|
||||||
|
$filterCollection = $this->addMockFilterCollection($em);
|
||||||
|
$filterCollection
|
||||||
|
->expects($this->once())
|
||||||
|
->method('setFiltersStateDirty');
|
||||||
|
|
||||||
|
$filter = new MyLocaleFilter($em);
|
||||||
|
|
||||||
|
$filter->setParameter('locale', 'en');
|
||||||
|
|
||||||
$this->assertEquals("'en'", $filter->getParameter('locale'));
|
$this->assertEquals("'en'", $filter->getParameter('locale'));
|
||||||
}
|
}
|
||||||
@ -243,16 +270,16 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$filterCollection = $this->addMockFilterCollection($em);
|
$filterCollection = $this->addMockFilterCollection($em);
|
||||||
|
|
||||||
$filter = new MyLocaleFilter($em);
|
$filter = new MyLocaleFilter($em);
|
||||||
$filter->setParameter('locale', 'en', \Doctrine\DBAL\Types\Type::STRING);
|
$filter->setParameter('locale', 'en', Type::STRING);
|
||||||
$filter->setParameter('foo', 'bar', \Doctrine\DBAL\Types\Type::STRING);
|
$filter->setParameter('foo', 'bar', Type::STRING);
|
||||||
|
|
||||||
$filter2 = new MyLocaleFilter($em);
|
$filter2 = new MyLocaleFilter($em);
|
||||||
$filter2->setParameter('foo', 'bar', \Doctrine\DBAL\Types\Type::STRING);
|
$filter2->setParameter('foo', 'bar', Type::STRING);
|
||||||
$filter2->setParameter('locale', 'en', \Doctrine\DBAL\Types\Type::STRING);
|
$filter2->setParameter('locale', 'en', Type::STRING);
|
||||||
|
|
||||||
$parameters = array(
|
$parameters = array(
|
||||||
'foo' => array('value' => 'bar', 'type' => \Doctrine\DBAL\Types\Type::STRING),
|
'foo' => array('value' => 'bar', 'type' => Type::STRING),
|
||||||
'locale' => array('value' => 'en', 'type' => \Doctrine\DBAL\Types\Type::STRING),
|
'locale' => array('value' => 'en', 'type' => Type::STRING),
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(serialize($parameters), ''.$filter);
|
$this->assertEquals(serialize($parameters), ''.$filter);
|
||||||
@ -292,7 +319,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$conf = $this->_em->getConfiguration();
|
$conf = $this->_em->getConfiguration();
|
||||||
$conf->addFilter("country", "\Doctrine\Tests\ORM\Functional\CMSCountryFilter");
|
$conf->addFilter("country", "\Doctrine\Tests\ORM\Functional\CMSCountryFilter");
|
||||||
$this->_em->getFilters()->enable("country")
|
$this->_em->getFilters()->enable("country")
|
||||||
->setParameter("country", "en", \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::STRING)->getBindingType());
|
->setParameter("country", "en", Type::STRING);
|
||||||
|
|
||||||
$this->assertNotEquals($firstSQLQuery, $query->getSQL());
|
$this->assertNotEquals($firstSQLQuery, $query->getSQL());
|
||||||
}
|
}
|
||||||
@ -309,7 +336,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
|
|
||||||
$conf = $this->_em->getConfiguration();
|
$conf = $this->_em->getConfiguration();
|
||||||
$conf->addFilter("country", "\Doctrine\Tests\ORM\Functional\CMSCountryFilter");
|
$conf->addFilter("country", "\Doctrine\Tests\ORM\Functional\CMSCountryFilter");
|
||||||
$this->_em->getFilters()->enable("country")->setParameter("country", "Germany", \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::STRING)->getBindingType());
|
$this->_em->getFilters()->enable("country")->setParameter("country", "Germany", Type::STRING);
|
||||||
|
|
||||||
// We get one user after enabling the filter
|
// We get one user after enabling the filter
|
||||||
$this->assertEquals(1, count($query->getResult()));
|
$this->assertEquals(1, count($query->getResult()));
|
||||||
@ -325,7 +352,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
|
|
||||||
$conf = $this->_em->getConfiguration();
|
$conf = $this->_em->getConfiguration();
|
||||||
$conf->addFilter("group_prefix", "\Doctrine\Tests\ORM\Functional\CMSGroupPrefixFilter");
|
$conf->addFilter("group_prefix", "\Doctrine\Tests\ORM\Functional\CMSGroupPrefixFilter");
|
||||||
$this->_em->getFilters()->enable("group_prefix")->setParameter("prefix", "bar_%", \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::STRING)->getBindingType());
|
$this->_em->getFilters()->enable("group_prefix")->setParameter("prefix", "bar_%", Type::STRING);
|
||||||
|
|
||||||
// We get one user after enabling the filter
|
// We get one user after enabling the filter
|
||||||
$this->assertEquals(1, count($query->getResult()));
|
$this->assertEquals(1, count($query->getResult()));
|
||||||
@ -342,7 +369,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
|
|
||||||
$conf = $this->_em->getConfiguration();
|
$conf = $this->_em->getConfiguration();
|
||||||
$conf->addFilter("group_prefix", "\Doctrine\Tests\ORM\Functional\CMSGroupPrefixFilter");
|
$conf->addFilter("group_prefix", "\Doctrine\Tests\ORM\Functional\CMSGroupPrefixFilter");
|
||||||
$this->_em->getFilters()->enable("group_prefix")->setParameter("prefix", "bar_%", \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::STRING)->getBindingType());
|
$this->_em->getFilters()->enable("group_prefix")->setParameter("prefix", "bar_%", Type::STRING);
|
||||||
|
|
||||||
// We get one user after enabling the filter
|
// We get one user after enabling the filter
|
||||||
$this->assertEquals(1, count($query->getResult()));
|
$this->assertEquals(1, count($query->getResult()));
|
||||||
@ -361,7 +388,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
{
|
{
|
||||||
$conf = $this->_em->getConfiguration();
|
$conf = $this->_em->getConfiguration();
|
||||||
$conf->addFilter("article_topic", "\Doctrine\Tests\ORM\Functional\CMSArticleTopicFilter");
|
$conf->addFilter("article_topic", "\Doctrine\Tests\ORM\Functional\CMSArticleTopicFilter");
|
||||||
$this->_em->getFilters()->enable("article_topic")->setParameter("topic", "Test1", \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::STRING)->getBindingType());
|
$this->_em->getFilters()->enable("article_topic")->setParameter("topic", "Test1", Type::STRING);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testOneToMany_ExtraLazyCountWithFilter()
|
public function testOneToMany_ExtraLazyCountWithFilter()
|
||||||
@ -408,7 +435,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
{
|
{
|
||||||
$conf = $this->_em->getConfiguration();
|
$conf = $this->_em->getConfiguration();
|
||||||
$conf->addFilter("group_prefix", "\Doctrine\Tests\ORM\Functional\CMSGroupPrefixFilter");
|
$conf->addFilter("group_prefix", "\Doctrine\Tests\ORM\Functional\CMSGroupPrefixFilter");
|
||||||
$this->_em->getFilters()->enable("group_prefix")->setParameter("prefix", "foo%", \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::STRING)->getBindingType());
|
$this->_em->getFilters()->enable("group_prefix")->setParameter("prefix", "foo%", Type::STRING);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testManyToMany_ExtraLazyCountWithFilter()
|
public function testManyToMany_ExtraLazyCountWithFilter()
|
||||||
@ -529,7 +556,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter");
|
$conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter");
|
||||||
$this->_em->getFilters()
|
$this->_em->getFilters()
|
||||||
->enable("person_name")
|
->enable("person_name")
|
||||||
->setParameter("name", "Guilh%", \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::STRING)->getBindingType());
|
->setParameter("name", "Guilh%", Type::STRING);
|
||||||
|
|
||||||
$managers = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyManager')->findAll();
|
$managers = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyManager')->findAll();
|
||||||
$this->assertEquals(1, count($managers));
|
$this->assertEquals(1, count($managers));
|
||||||
@ -549,7 +576,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter");
|
$conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter");
|
||||||
$this->_em->getFilters()
|
$this->_em->getFilters()
|
||||||
->enable("person_name")
|
->enable("person_name")
|
||||||
->setParameter("name", "Guilh%", \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::STRING)->getBindingType());
|
->setParameter("name", "Guilh%", Type::STRING);
|
||||||
|
|
||||||
$persons = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyPerson')->findAll();
|
$persons = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyPerson')->findAll();
|
||||||
$this->assertEquals(1, count($persons));
|
$this->assertEquals(1, count($persons));
|
||||||
@ -595,7 +622,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$conf->addFilter("completed_contract", "\Doctrine\Tests\ORM\Functional\CompletedContractFilter");
|
$conf->addFilter("completed_contract", "\Doctrine\Tests\ORM\Functional\CompletedContractFilter");
|
||||||
$this->_em->getFilters()
|
$this->_em->getFilters()
|
||||||
->enable("completed_contract")
|
->enable("completed_contract")
|
||||||
->setParameter("completed", true, \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::BOOLEAN)->getBindingType());
|
->setParameter("completed", true, Type::BOOLEAN);
|
||||||
|
|
||||||
$this->assertEquals(1, count($this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyFlexUltraContract')->findAll()));
|
$this->assertEquals(1, count($this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyFlexUltraContract')->findAll()));
|
||||||
$this->assertEquals(1, count($this->_em->createQuery("SELECT cfc FROM Doctrine\Tests\Models\Company\CompanyFlexUltraContract cfc")->getResult()));
|
$this->assertEquals(1, count($this->_em->createQuery("SELECT cfc FROM Doctrine\Tests\Models\Company\CompanyFlexUltraContract cfc")->getResult()));
|
||||||
@ -612,7 +639,7 @@ class SQLFilterTest extends \Doctrine\Tests\OrmFunctionalTestCase
|
|||||||
$conf->addFilter("completed_contract", "\Doctrine\Tests\ORM\Functional\CompletedContractFilter");
|
$conf->addFilter("completed_contract", "\Doctrine\Tests\ORM\Functional\CompletedContractFilter");
|
||||||
$this->_em->getFilters()
|
$this->_em->getFilters()
|
||||||
->enable("completed_contract")
|
->enable("completed_contract")
|
||||||
->setParameter("completed", true, \Doctrine\DBAL\Types\Type::getType(\Doctrine\DBAL\Types\Type::BOOLEAN)->getBindingType());
|
->setParameter("completed", true, Type::BOOLEAN);
|
||||||
|
|
||||||
$this->assertEquals(2, count($this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyFlexContract')->findAll()));
|
$this->assertEquals(2, count($this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyFlexContract')->findAll()));
|
||||||
$this->assertEquals(2, count($this->_em->createQuery("SELECT cfc FROM Doctrine\Tests\Models\Company\CompanyFlexContract cfc")->getResult()));
|
$this->assertEquals(2, count($this->_em->createQuery("SELECT cfc FROM Doctrine\Tests\Models\Company\CompanyFlexContract cfc")->getResult()));
|
||||||
|
Loading…
Reference in New Issue
Block a user