Skip to content

Commit 4650775

Browse files
vincentchalamonteohhanhui
authored andcommitted
Add tests
1 parent 00118a8 commit 4650775

25 files changed

+2733
-3290
lines changed

Doctrine/Orm/Extension/PaginationExtension.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public function applyToCollection(ResourceInterface $resource, QueryBuilder $que
6363

6464
$queryBuilder
6565
->setFirstResult(($this->getPage($resource, $request) - 1) * $itemsPerPage)
66-
->setMaxResults($itemsPerPage)
67-
;
66+
->setMaxResults($itemsPerPage);
6867
}
6968

7069
/**
@@ -74,7 +73,9 @@ public function applyToCollection(ResourceInterface $resource, QueryBuilder $que
7473
*/
7574
public function supportsResult(ResourceInterface $resource)
7675
{
77-
return $this->isPaginationEnabled($resource, $this->requestStack->getCurrentRequest());
76+
$request = $this->requestStack->getCurrentRequest();
77+
78+
return $request !== null && $this->isPaginationEnabled($resource, $request);
7879
}
7980

8081
/**
@@ -132,7 +133,8 @@ private function getPage(ResourceInterface $resource, Request $request)
132133
private function getItemsPerPage(ResourceInterface $resource, Request $request)
133134
{
134135
if ($resource->isClientAllowedToChangeItemsPerPage()
135-
&& $itemsPerPage = $request->get($resource->getItemsPerPageParameter())) {
136+
&& $itemsPerPage = $request->get($resource->getItemsPerPageParameter())
137+
) {
136138
return (float) $itemsPerPage;
137139
}
138140

Doctrine/Orm/Filter/DateFilter.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ public function apply(ResourceInterface $resource, QueryBuilder $queryBuilder)
7171
foreach ($this->extractProperties($request) as $property => $values) {
7272
// Expect $values to be an array having the period as keys and the date value as values
7373
if (
74-
!$this->isPropertyEnabled($property)
75-
|| !$this->isPropertyMapped($property, $resource)
76-
|| !$this->isDateField($property, $resource)
77-
|| !is_array($values)
74+
!$this->isPropertyEnabled($property) ||
75+
!$this->isPropertyMapped($property, $resource) ||
76+
!$this->isDateField($property, $resource) ||
77+
!is_array($values)
7878
) {
7979
continue;
8080
}
@@ -144,8 +144,7 @@ private function addWhere(QueryBuilder $queryBuilder, $alias, $field, $operator,
144144
if (null === $nullManagement || self::EXCLUDE_NULL === $nullManagement) {
145145
$queryBuilder->andWhere($baseWhere);
146146
} elseif (
147-
(self::PARAMETER_BEFORE === $operator && self::INCLUDE_NULL_BEFORE === $nullManagement)
148-
||
147+
(self::PARAMETER_BEFORE === $operator && self::INCLUDE_NULL_BEFORE === $nullManagement) ||
149148
(self::PARAMETER_AFTER === $operator && self::INCLUDE_NULL_AFTER === $nullManagement)
150149
) {
151150
$queryBuilder->andWhere($queryBuilder->expr()->orX(
@@ -175,7 +174,7 @@ public function getDescription(ResourceInterface $resource)
175174
}
176175

177176
foreach ($properties as $property => $nullManagement) {
178-
if (!$this->isPropertyMapped($property, $resource)) {
177+
if (!$this->isPropertyMapped($property, $resource) || !$this->isDateField($property, $resource)) {
179178
continue;
180179
}
181180

@@ -208,7 +207,8 @@ private function getFilterDescription($property, $period)
208207
/**
209208
* Determines whether the given property refers to a date field.
210209
*
211-
* @param string $property
210+
* @param string $property
211+
* @param ResourceInterface $resource
212212
*
213213
* @return bool
214214
*/

Doctrine/Orm/Filter/OrderFilter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121
/**
2222
* Order the collection by given properties.
2323
*
24-
* @author Théo FIDRY <theo.fidry@gmail.com>
2524
* @author Kévin Dunglas <dunglas@gmail.com>
25+
* @author Théo FIDRY <theo.fidry@gmail.com>
2626
*/
2727
class OrderFilter extends AbstractFilter
2828
{
2929
/**
3030
* @var string Keyword used to retrieve the value.
3131
*/
3232
private $orderParameter;
33+
3334
/**
3435
* @var RequestStack
3536
*/
@@ -75,7 +76,7 @@ public function apply(ResourceInterface $resource, QueryBuilder $queryBuilder)
7576
continue;
7677
}
7778

78-
if ('' === $order && isset($this->properties[$property])) {
79+
if (empty($order) && isset($this->properties[$property])) {
7980
$order = $this->properties[$property];
8081
}
8182

Doctrine/Orm/Filter/SearchFilter.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class SearchFilter extends AbstractFilter
3131
* @var string Exact matching.
3232
*/
3333
const STRATEGY_EXACT = 'exact';
34+
3435
/**
3536
* @var string The value must be contained in the field.
3637
*/
@@ -52,10 +53,12 @@ class SearchFilter extends AbstractFilter
5253
* @var IriConverterInterface
5354
*/
5455
private $iriConverter;
56+
5557
/**
5658
* @var PropertyAccessorInterface
5759
*/
5860
private $propertyAccessor;
61+
5962
/**
6063
* @var RequestStack
6164
*/
@@ -94,9 +97,9 @@ public function apply(ResourceInterface $resource, QueryBuilder $queryBuilder)
9497

9598
foreach ($this->extractProperties($request) as $property => $value) {
9699
if (
97-
!$this->isPropertyEnabled($property)
98-
|| !$this->isPropertyMapped($property, $resource, true)
99-
|| null === $value
100+
!$this->isPropertyEnabled($property) ||
101+
!$this->isPropertyMapped($property, $resource, true) ||
102+
null === $value
100103
) {
101104
continue;
102105
}
@@ -148,8 +151,7 @@ public function apply(ResourceInterface $resource, QueryBuilder $queryBuilder)
148151
$queryBuilder
149152
->join(sprintf('%s.%s', $alias, $association), $associationAlias)
150153
->andWhere(sprintf('%s.id = :%s', $associationAlias, $valueParameter))
151-
->setParameter($valueParameter, $value)
152-
;
154+
->setParameter($valueParameter, $value);
153155
} elseif ($metadata->isCollectionValuedAssociation($field)) {
154156
$values = $value;
155157
if (!is_array($values)) {
@@ -174,8 +176,7 @@ public function apply(ResourceInterface $resource, QueryBuilder $queryBuilder)
174176
$queryBuilder
175177
->join(sprintf('%s.%s', $alias, $association), $associationAlias)
176178
->andWhere(sprintf('%s.id IN (:%s)', $associationAlias, $valuesParameter))
177-
->setParameter($valuesParameter, $values)
178-
;
179+
->setParameter($valuesParameter, $values);
179180
}
180181
}
181182
}
@@ -202,33 +203,28 @@ private function addWhereByStrategy($strategy, QueryBuilder $queryBuilder, $alia
202203
case self::STRATEGY_EXACT:
203204
return $queryBuilder
204205
->andWhere(sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
205-
->setParameter($valueParameter, $value)
206-
;
206+
->setParameter($valueParameter, $value);
207207

208208
case self::STRATEGY_PARTIAL:
209209
return $queryBuilder
210210
->andWhere(sprintf('%s.%s LIKE :%s', $alias, $field, $valueParameter))
211-
->setParameter($valueParameter, sprintf('%%%s%%', $value))
212-
;
211+
->setParameter($valueParameter, sprintf('%%%s%%', $value));
213212

214213
case self::STRATEGY_START:
215214
return $queryBuilder
216215
->andWhere(sprintf('%s.%s LIKE :%s', $alias, $field, $valueParameter))
217-
->setParameter($valueParameter, sprintf('%s%%', $value))
218-
;
216+
->setParameter($valueParameter, sprintf('%s%%', $value));
219217

220218
case self::STRATEGY_END:
221219
return $queryBuilder
222220
->andWhere(sprintf('%s.%s LIKE :%s', $alias, $field, $valueParameter))
223-
->setParameter($valueParameter, sprintf('%%%s', $value))
224-
;
221+
->setParameter($valueParameter, sprintf('%%%s', $value));
225222

226223
case self::STRATEGY_WORD_START:
227224
return $queryBuilder
228225
->andWhere(sprintf('%1$s.%2$s LIKE :%3$s_1 OR %1$s.%2$s LIKE :%3$s_2', $alias, $field, $valueParameter))
229226
->setParameter(sprintf('%s_1', $valueParameter), sprintf('%s%%', $value))
230-
->setParameter(sprintf('%s_2', $valueParameter), sprintf('%% %s%%', $value))
231-
;
227+
->setParameter(sprintf('%s_2', $valueParameter), sprintf('%% %s%%', $value));
232228
}
233229

234230
throw new InvalidArgumentException(sprintf('strategy %s does not exist.', $strategy));

Tests/Behat/TestBundle/Entity/Dummy.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,19 @@ public function getJsonData()
165165
{
166166
return $this->jsonData;
167167
}
168+
169+
public function getRelatedDummy()
170+
{
171+
return $this->relatedDummy;
172+
}
173+
174+
public function setRelatedDummy(RelatedDummy $relatedDummy)
175+
{
176+
$this->relatedDummy = $relatedDummy;
177+
}
178+
179+
public function addRelatedDummy(RelatedDummy $relatedDummy)
180+
{
181+
$this->relatedDummies->add($relatedDummy);
182+
}
168183
}

Tests/Behat/TestBundle/Entity/RelatedDummy.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Dunglas\ApiBundle\Annotation\Iri;
1515
use Doctrine\ORM\Mapping as ORM;
1616
use Symfony\Component\Serializer\Annotation\Groups;
17+
use Symfony\Component\Validator\Constraints as Assert;
1718

1819
/**
1920
* Related dummy.
@@ -31,11 +32,24 @@ class RelatedDummy extends ParentDummy
3132
* @ORM\GeneratedValue(strategy="AUTO")
3233
*/
3334
private $id;
35+
/**
36+
* @var string A name.
37+
*
38+
* @ORM\Column(nullable=true)
39+
*/
40+
public $name;
3441
/**
3542
* @ORM\Column
3643
* @Groups({"barcelona", "chicago"})
3744
*/
3845
protected $symfony = 'symfony';
46+
/**
47+
* @var \DateTime A dummy date.
48+
*
49+
* @ORM\Column(type="datetime", nullable=true)
50+
* @Assert\DateTime
51+
*/
52+
public $dummyDate;
3953
/**
4054
* @ORM\ManyToOne(targetEntity="ThirdLevel", cascade={"persist"})
4155
* @Groups({"barcelona", "chicago"})
@@ -56,6 +70,16 @@ public function getId()
5670
return $this->id;
5771
}
5872

73+
public function setName($name)
74+
{
75+
$this->name = $name;
76+
}
77+
78+
public function getName()
79+
{
80+
return $this->name;
81+
}
82+
5983
public function getSymfony()
6084
{
6185
return $this->symfony;
@@ -65,4 +89,14 @@ public function setSymfony($symfony)
6589
{
6690
$this->symfony = $symfony;
6791
}
92+
93+
public function setDummyDate(\DateTime $dummyDate)
94+
{
95+
$this->dummyDate = $dummyDate;
96+
}
97+
98+
public function getDummyDate()
99+
{
100+
return $this->dummyDate;
101+
}
68102
}

0 commit comments

Comments
 (0)