Skip to content

Commit

Permalink
Merge pull request #227 from asm89/ddc-551-parameter-inference
Browse files Browse the repository at this point in the history
[DDC-551] Add type inference to SQLFilter::setParameter() + cleaned tests
  • Loading branch information
beberlei committed Dec 19, 2011
2 parents a8478d5 + bd07f8d commit 7f8f391
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
15 changes: 11 additions & 4 deletions lib/Doctrine/ORM/Query/Filter/SQLFilter.php
Expand Up @@ -19,8 +19,9 @@

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.
Expand Down Expand Up @@ -60,12 +61,18 @@ final public function __construct(EntityManager $em)
*
* @param string $name Name 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.
*/
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);

// Keep the parameters sorted for the hash
Expand Down
61 changes: 44 additions & 17 deletions tests/Doctrine/Tests/ORM/Functional/SQLFilterTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Query\Filter\SQLFilter;
use Doctrine\ORM\Mapping\ClassMetaData;
use Doctrine\Common\Cache\ArrayCache;
Expand Down Expand Up @@ -213,7 +214,33 @@ public function testSQLFilterGetSetParameter()

$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'));
}
Expand Down Expand Up @@ -243,16 +270,16 @@ public function testSQLFilterToString()
$filterCollection = $this->addMockFilterCollection($em);

$filter = new MyLocaleFilter($em);
$filter->setParameter('locale', 'en', \Doctrine\DBAL\Types\Type::STRING);
$filter->setParameter('foo', 'bar', \Doctrine\DBAL\Types\Type::STRING);
$filter->setParameter('locale', 'en', Type::STRING);
$filter->setParameter('foo', 'bar', Type::STRING);

$filter2 = new MyLocaleFilter($em);
$filter2->setParameter('foo', 'bar', \Doctrine\DBAL\Types\Type::STRING);
$filter2->setParameter('locale', 'en', \Doctrine\DBAL\Types\Type::STRING);
$filter2->setParameter('foo', 'bar', Type::STRING);
$filter2->setParameter('locale', 'en', Type::STRING);

$parameters = array(
'foo' => array('value' => 'bar', 'type' => \Doctrine\DBAL\Types\Type::STRING),
'locale' => array('value' => 'en', 'type' => \Doctrine\DBAL\Types\Type::STRING),
'foo' => array('value' => 'bar', 'type' => Type::STRING),
'locale' => array('value' => 'en', 'type' => Type::STRING),
);

$this->assertEquals(serialize($parameters), ''.$filter);
Expand Down Expand Up @@ -292,7 +319,7 @@ public function testQueryGeneration_DependsOnFilters()
$conf = $this->_em->getConfiguration();
$conf->addFilter("country", "\Doctrine\Tests\ORM\Functional\CMSCountryFilter");
$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());
}
Expand All @@ -309,7 +336,7 @@ public function testToOneFilter()

$conf = $this->_em->getConfiguration();
$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
$this->assertEquals(1, count($query->getResult()));
Expand All @@ -325,7 +352,7 @@ public function testManyToManyFilter()

$conf = $this->_em->getConfiguration();
$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
$this->assertEquals(1, count($query->getResult()));
Expand All @@ -342,7 +369,7 @@ public function testWhereFilter()

$conf = $this->_em->getConfiguration();
$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
$this->assertEquals(1, count($query->getResult()));
Expand All @@ -361,7 +388,7 @@ private function useCMSArticleTopicFilter()
{
$conf = $this->_em->getConfiguration();
$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()
Expand Down Expand Up @@ -408,7 +435,7 @@ private function useCMSGroupPrefixFilter()
{
$conf = $this->_em->getConfiguration();
$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()
Expand Down Expand Up @@ -529,7 +556,7 @@ public function testJoinSubclassPersister_FilterOnlyOnRootTableWhenFetchingSubEn
$conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter");
$this->_em->getFilters()
->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();
$this->assertEquals(1, count($managers));
Expand All @@ -549,7 +576,7 @@ public function testJoinSubclassPersister_FilterOnlyOnRootTableWhenFetchingRootE
$conf->addFilter("person_name", "\Doctrine\Tests\ORM\Functional\CompanyPersonNameFilter");
$this->_em->getFilters()
->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();
$this->assertEquals(1, count($persons));
Expand Down Expand Up @@ -595,7 +622,7 @@ public function testSingleTableInheritance_FilterOnlyOnRootTableWhenFetchingSubE
$conf->addFilter("completed_contract", "\Doctrine\Tests\ORM\Functional\CompletedContractFilter");
$this->_em->getFilters()
->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->createQuery("SELECT cfc FROM Doctrine\Tests\Models\Company\CompanyFlexUltraContract cfc")->getResult()));
Expand All @@ -612,7 +639,7 @@ public function testSingleTableInheritance_FilterOnlyOnRootTableWhenFetchingRoot
$conf->addFilter("completed_contract", "\Doctrine\Tests\ORM\Functional\CompletedContractFilter");
$this->_em->getFilters()
->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->createQuery("SELECT cfc FROM Doctrine\Tests\Models\Company\CompanyFlexContract cfc")->getResult()));
Expand Down

0 comments on commit 7f8f391

Please sign in to comment.