Skip to content

Commit

Permalink
Handling the null/not null value (#3)
Browse files Browse the repository at this point in the history
* Handling the null/not null value

* Added tests for the null/not null filters

* Fixed function names
  • Loading branch information
pierreallard authored and alterphp committed Nov 20, 2017
1 parent 4865335 commit 593b8c7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/EventListener/PostQueryBuilderSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,20 @@ protected function filterQueryBuilder(QueryBuilder $queryBuilder, string $field,
// For multiple value, use an IN clause, equality otherwise
if (is_array($value)) {
$filterDqlPart = $field.' IN (:'.$parameter.')';
} elseif ('_NULL' === $value) {
$parameter = null;
$filterDqlPart = $field.' IS NULL';
} elseif ('_NOT_NULL' === $value) {
$parameter = null;
$filterDqlPart = $field.' IS NOT NULL';
} else {
$filterDqlPart = $field.' = :'.$parameter;
}

$queryBuilder
->andWhere($filterDqlPart)
->setParameter($parameter, $value)
;
$queryBuilder->andWhere($filterDqlPart);
if (null !== $parameter) {
$queryBuilder->setParameter($parameter, $value);
}
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Controller/RequestParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,24 @@ public function testRequestFilterIsAppliedToSearchAction()

$this->assertSame(10, $crawler->filter('#main tr[data-id]')->count());
}

public function testRequestNullFilterIsApplied()
{
$crawler = $this->requestListView(
'Product', array('entity.phone' => '_NULL')
);

$this->assertSame(10, $crawler->filter('#main tr[data-id]')->count());
}

public function testRequestNotNullFilterIsApplied()
{
$crawler = $this->requestListView(
'Product', array('entity.phone' => '_NOT_NULL')
);
$this->assertContains(
'1 - 15 of 90',
$crawler->filter('#main .list-pagination')->text()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function load(ObjectManager $manager)
$product->setCategories($this->getRandomCategories());
$product->setDescription($this->getRandomDescription());
$product->setHtmlFeatures($this->getRandomHtmlFeatures());
$product->setPhone($i <= 10 ? null : '0123456789');

$this->addReference('product-'.$i, $product);
$manager->persist($product);
Expand Down
23 changes: 23 additions & 0 deletions tests/Fixtures/AppTestBundle/Entity/FunctionalTests/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ class Product
*/
protected $image;

/**
* @var string
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $phone;

/**
* Constructor of the Category class.
* (Initialize some fields).
Expand Down Expand Up @@ -463,4 +470,20 @@ public function __toString()
{
return $this->getName();
}

/**
* @param string $phone
*/
public function setPhone($phone)
{
$this->phone = $phone;
}

/**
* @return string
*/
public function getPhone()
{
return $this->phone;
}
}

0 comments on commit 593b8c7

Please sign in to comment.