Skip to content

Commit 869a2a9

Browse files
committed
Merge remote-tracking branch 'origin/2.0'
2 parents de0bdc3 + 8c27b49 commit 869a2a9

File tree

4 files changed

+105
-18
lines changed

4 files changed

+105
-18
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ cache:
1010
matrix:
1111
include:
1212
- php: '7.0'
13-
- php: '7.1'
13+
- php: '7.1.0' # Prevent phpdbg's segfaults
1414
env: coverage=1 lint=1
1515
- php: '7.1'
1616
env: deps='low'

features/bootstrap/FeatureContext.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,22 @@ public function thereIsDummyObjectsWithDummyPrice($nb)
235235
*/
236236
public function thereIsDummyObjectsWithDummyBoolean($nb, $bool)
237237
{
238+
if (in_array($bool, ['true', '1', 1], true)) {
239+
$bool = true;
240+
} elseif (in_array($bool, ['false', '0', 0], true)) {
241+
$bool = false;
242+
} else {
243+
$expected = ['true', 'false', '1', '0'];
244+
throw new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
245+
}
238246
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];
239247

240248
for ($i = 1; $i <= $nb; ++$i) {
241249
$dummy = new Dummy();
242250
$dummy->setName('Dummy #'.$i);
243251
$dummy->setAlias('Alias #'.($nb - $i));
244252
$dummy->setDescription($descriptions[($i - 1) % 2]);
245-
$dummy->setDummyBoolean((bool) $bool);
253+
$dummy->setDummyBoolean($bool);
246254

247255
$this->manager->persist($dummy);
248256
}

features/doctrine/boolean_filter.feature

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ Feature: Boolean filter on collections
44
I need to retrieve collections with boolean value
55

66
@createSchema
7-
Scenario: Get collection by dummyBoolean false
7+
Scenario: Get collection by dummyBoolean true
88
Given there is "15" dummy objects with dummyBoolean true
9+
And there is "10" dummy objects with dummyBoolean false
910
When I send a "GET" request to "/dummies?dummyBoolean=true"
1011
Then the response status code should be 200
1112
And the response should be in JSON
@@ -34,12 +35,16 @@ Feature: Boolean filter on collections
3435
}
3536
},
3637
"hydra:view": {
37-
"@id": {"pattern": "^/dummies\\?dummyBoolean=0"},
38-
"@type": {"pattern": "^hydra:PartialCollectionView$"}
38+
"type": "object",
39+
"properties": {
40+
"@id": {"pattern": "^/dummies\\?dummyBoolean=true"},
41+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
42+
}
3943
}
4044
}
4145
}
4246
"""
47+
And the JSON node "hydra:totalItems" should be equal to 15
4348

4449
Scenario: Get collection by dummyBoolean true
4550
When I send a "GET" request to "/dummies?dummyBoolean=1"
@@ -70,14 +75,18 @@ Feature: Boolean filter on collections
7075
}
7176
},
7277
"hydra:view": {
73-
"@id": {"pattern": "^/dummies\\?dummyBoolean=1"},
74-
"@type": {"pattern": "^hydra:PartialCollectionView$"}
78+
"type": "object",
79+
"properties": {
80+
"@id": {"pattern": "^/dummies\\?dummyBoolean=1"},
81+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
82+
}
7583
}
7684
}
7785
}
7886
"""
87+
And the JSON node "hydra:totalItems" should be equal to 15
88+
7989
Scenario: Get collection by dummyBoolean false
80-
Given there is "15" dummy objects with dummyBoolean false
8190
When I send a "GET" request to "/dummies?dummyBoolean=false"
8291
Then the response status code should be 200
8392
And the response should be in JSON
@@ -97,22 +106,65 @@ Feature: Boolean filter on collections
97106
"properties": {
98107
"@id": {
99108
"oneOf": [
100-
{"pattern": "^/dummies/1$"},
101-
{"pattern": "^/dummies/2$"},
102-
{"pattern": "^/dummies/3$"}
109+
{"pattern": "^/dummies/16$"},
110+
{"pattern": "^/dummies/17$"},
111+
{"pattern": "^/dummies/18$"}
103112
]
104113
}
105114
}
106115
}
107116
},
108117
"hydra:view": {
109-
"@id": {"pattern": "^/dummies\\?dummyBoolean=false"},
110-
"@type": {"pattern": "^hydra:PartialCollectionView$"}
118+
"type": "object",
119+
"properties": {
120+
"@id": {"pattern": "^/dummies\\?dummyBoolean=false"},
121+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
122+
}
111123
}
112124
}
113125
}
114126
"""
127+
And the JSON node "hydra:totalItems" should be equal to 10
115128

129+
Scenario: Get collection by dummyBoolean false
130+
When I send a "GET" request to "/dummies?dummyBoolean=0"
131+
Then the response status code should be 200
132+
And the response should be in JSON
133+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
134+
And the JSON should be valid according to this schema:
135+
"""
136+
{
137+
"type": "object",
138+
"properties": {
139+
"@context": {"pattern": "^/contexts/Dummy$"},
140+
"@id": {"pattern": "^/dummies$"},
141+
"@type": {"pattern": "^hydra:Collection$"},
142+
"hydra:member": {
143+
"type": "array",
144+
"items": {
145+
"type": "object",
146+
"properties": {
147+
"@id": {
148+
"oneOf": [
149+
{"pattern": "^/dummies/16$"},
150+
{"pattern": "^/dummies/17$"},
151+
{"pattern": "^/dummies/18$"}
152+
]
153+
}
154+
}
155+
}
156+
},
157+
"hydra:view": {
158+
"type": "object",
159+
"properties": {
160+
"@id": {"pattern": "^/dummies\\?dummyBoolean=0"},
161+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
162+
}
163+
}
164+
}
165+
}
166+
"""
167+
And the JSON node "hydra:totalItems" should be equal to 10
116168

117169
@dropSchema
118170
Scenario: Get collection ordered by a non valid properties
@@ -144,12 +196,16 @@ Feature: Boolean filter on collections
144196
}
145197
},
146198
"hydra:view": {
147-
"@id": {"pattern": "^/dummies\\?unknown=0"},
148-
"@type": {"pattern": "^hydra:PartialCollectionView$"}
199+
"type": "object",
200+
"properties": {
201+
"@id": {"pattern": "^/dummies\\?unknown=0"},
202+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
203+
}
149204
}
150205
}
151206
}
152207
"""
208+
And the JSON node "hydra:totalItems" should be equal to 25
153209

154210
When I send a "GET" request to "/dummies?unknown=1"
155211
Then the response status code should be 200
@@ -179,9 +235,13 @@ Feature: Boolean filter on collections
179235
}
180236
},
181237
"hydra:view": {
182-
"@id": {"pattern": "^/dummies\\?unknown=1$"},
183-
"@type": {"pattern": "^hydra:PartialCollectionView$"}
238+
"type": "object",
239+
"properties": {
240+
"@id": {"pattern": "^/dummies\\?unknown=1"},
241+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
242+
}
184243
}
185244
}
186245
}
187246
"""
247+
And the JSON node "hydra:totalItems" should be equal to 25

tests/Bridge/Doctrine/Orm/Filter/SearchFilterTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
use ApiPlatform\Core\Api\IriConverterInterface;
1515
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
1616
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
17+
use ApiPlatform\Core\Exception\InvalidArgumentException;
1718
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
19+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
1820
use Doctrine\Common\Persistence\ManagerRegistry;
1921
use Doctrine\ORM\EntityRepository;
22+
use Prophecy\Argument;
2023
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
2124
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
2225
use Symfony\Component\HttpFoundation\Request;
@@ -62,7 +65,23 @@ protected function setUp()
6265
self::bootKernel();
6366
$manager = DoctrineTestHelper::createTestEntityManager();
6467
$this->managerRegistry = self::$kernel->getContainer()->get('doctrine');
65-
$this->iriConverter = self::$kernel->getContainer()->get('api_platform.iri_converter');
68+
69+
$relatedDummyProphecy = $this->prophesize(RelatedDummy::class);
70+
71+
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
72+
73+
$iriConverterProphecy->getItemFromIri(Argument::type('string'), ['fetch_data' => false])->will(function ($args) use ($relatedDummyProphecy) {
74+
if (false !== strpos($args[0], '/related_dummies')) {
75+
$relatedDummyProphecy->getId()->shouldBeCalled()->willReturn(1);
76+
77+
return $relatedDummyProphecy->reveal();
78+
}
79+
80+
throw new InvalidArgumentException();
81+
});
82+
83+
$this->iriConverter = $iriConverterProphecy->reveal();
84+
6685
$this->propertyAccessor = self::$kernel->getContainer()->get('property_accessor');
6786
$this->repository = $manager->getRepository(Dummy::class);
6887
$this->resourceClass = Dummy::class;

0 commit comments

Comments
 (0)