Skip to content

Commit 9528880

Browse files
author
abluchet
committed
debug
1 parent 32c69f2 commit 9528880

File tree

11 files changed

+120
-51
lines changed

11 files changed

+120
-51
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ install:
4949

5050
script:
5151
- if [[ $coverage = 1 ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-php build/cov/coverage-phpunit.cov; else vendor/bin/phpunit; fi
52-
- if [[ $coverage = 1 ]]; then for f in $(find features -name '*.feature'); do FEATURE=${f//\//_} phpdbg -qrr vendor/bin/behat --format=progress --profile coverage $f || exit $?; done; else vendor/bin/behat --format=progress; fi
52+
- if [[ $coverage = 1 ]]; then for f in $(find features -name '*.feature'); do FEATURE=${f//\//_} phpdbg -qrr vendor/bin/behat --format=progress --profile coverage $f || exit $?; done; elif [[ $postgres = 1 ]]; then vendor/bin/behat --stop-on-failure; else vendor/bin/behat --format=progress; fi
5353
- if [[ $coverage = 1 ]]; then phpdbg -qrr phpcov.phar merge --clover build/logs/clover.xml build/cov; fi
5454
- tests/Fixtures/app/console api:swagger:export > swagger.json && swagger validate swagger.json && rm swagger.json
5555
- if [[ $lint = 1 ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi

behat.yml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ default:
33
default:
44
contexts:
55
- 'FeatureContext': { doctrine: '@doctrine' }
6+
- 'JsonContext'
67
- 'HydraContext'
78
- 'SwaggerContext'
89
- 'HttpCacheContext'
910
- 'Behat\MinkExtension\Context\MinkContext'
1011
- 'Behatch\Context\RestContext'
11-
- 'Behatch\Context\JsonContext'
1212
extensions:
1313
'Behat\Symfony2Extension':
1414
kernel:

features/bootstrap/JsonContext.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
use Behat\Gherkin\Node\PyStringNode;
15+
use Behatch\Context\JsonContext as BaseJsonContext;
16+
use Behatch\HttpCall\HttpCallResultPool;
17+
use Behatch\Json\Json;
18+
19+
final class JsonContext extends BaseJsonContext
20+
{
21+
public function __construct(HttpCallResultPool $httpCallResultPool)
22+
{
23+
parent::__construct($httpCallResultPool);
24+
}
25+
26+
private function sortArrays(&$obj)
27+
{
28+
foreach ($obj as $key => &$value) {
29+
if (is_scalar($value)) {
30+
continue;
31+
}
32+
33+
if (is_array($value)) {
34+
sort($value);
35+
}
36+
37+
$this->sortArrays($value);
38+
}
39+
}
40+
41+
/**
42+
* @Then /^the JSON should be deep equal to:$/
43+
*/
44+
public function theJsonShouldBeDeepEqualTo(PyStringNode $content)
45+
{
46+
$actual = $this->getJson();
47+
try {
48+
$expected = new Json($content);
49+
} catch (\Exception $e) {
50+
throw new \Exception('The expected JSON is not a valid');
51+
}
52+
53+
$this->sortArrays($actual->getContent());
54+
$this->sortArrays($expected->getContent());
55+
56+
$this->assertSame(
57+
(string) $expected,
58+
(string) $actual,
59+
"The json is equal to:\n".$actual->encode()
60+
);
61+
}
62+
}

features/doctrine/numeric_filter.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Feature: Numeric filter on collections
77
Scenario: Get collection by id equals 9.99 which is not possible
88
Given there is "30" dummy objects
99
When I send a "GET" request to "/dummies?id=9.99"
10-
Then the response status code should be 200
10+
Then print last JSON response
11+
And the response status code should be 200
1112
And the response should be in JSON
1213
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
1314
And the JSON should be valid according to this schema:

features/doctrine/search_filter.feature

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ Feature: Search filter on collections
1818
Scenario: Test #944
1919
Given there is a DummyCar entity with related colors
2020
When I send a "GET" request to "/dummy_cars?colors.prop=red"
21-
Then the response status code should be 200
22-
And the JSON should be equal to:
21+
And the response status code should be 200
22+
And the JSON should be deep equal to:
2323
"""
2424
{
2525
"@context": "/contexts/DummyCar",
@@ -213,7 +213,8 @@ Feature: Search filter on collections
213213

214214
Scenario: Search collection by description (word_start)
215215
When I send a "GET" request to "/dummies?description=smart"
216-
Then the response status code should be 200
216+
Then print last JSON response
217+
And the response status code should be 200
217218
And the response should be in JSON
218219
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
219220
And the JSON should be valid according to this schema:

features/integration/fos_user.feature

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Feature: FOSUser integration
1515
"plainPassword": "azerty"
1616
}
1717
"""
18-
Then the response status code should be 201
18+
Then print last JSON response
19+
And the response status code should be 201
1920
And the response should be in JSON
2021
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
2122
And the JSON should be equal to:

features/main/composite.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Feature: Retrieve data with Composite identifiers
1111
Then the response status code should be 200
1212
And the response should be in JSON
1313
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
14-
And the JSON should be equal to:
14+
And the JSON should be deep equal to:
1515
"""
1616
{
1717
"@context": "/contexts/CompositeItem",

features/main/uuid.feature

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ Feature: Using uuid identifier on resource
1010
"""
1111
{
1212
"name": "My Dummy",
13-
"uuid": "41B29566-144B-11E6-A148-3E1D05DEFE78"
13+
"uuid": "41b29566-144b-11e6-a148-3e1d05defe78"
1414
}
1515
"""
1616
Then the response status code should be 201
1717
And the response should be in JSON
1818
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
1919

2020
Scenario: Get a resource
21-
When I send a "GET" request to "/uuid_identifier_dummies/41B29566-144B-11E6-A148-3E1D05DEFE78"
21+
When I send a "GET" request to "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78"
2222
Then the response status code should be 200
2323
And the response should be in JSON
2424
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
2525
And the JSON should be equal to:
2626
"""
2727
{
2828
"@context": "/contexts/UuidIdentifierDummy",
29-
"@id": "/uuid_identifier_dummies/41B29566-144B-11E6-A148-3E1D05DEFE78",
29+
"@id": "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78",
3030
"@type": "UuidIdentifierDummy",
31-
"uuid": "41B29566-144B-11E6-A148-3E1D05DEFE78",
31+
"uuid": "41b29566-144b-11e6-a148-3e1d05defe78",
3232
"name": "My Dummy"
3333
}
3434
"""
@@ -46,9 +46,9 @@ Feature: Using uuid identifier on resource
4646
"@type": "hydra:Collection",
4747
"hydra:member": [
4848
{
49-
"@id": "/uuid_identifier_dummies/41B29566-144B-11E6-A148-3E1D05DEFE78",
49+
"@id": "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78",
5050
"@type": "UuidIdentifierDummy",
51-
"uuid": "41B29566-144B-11E6-A148-3E1D05DEFE78",
51+
"uuid": "41b29566-144b-11e6-a148-3e1d05defe78",
5252
"name": "My Dummy"
5353
}
5454
],
@@ -58,7 +58,7 @@ Feature: Using uuid identifier on resource
5858

5959
Scenario: Update a resource
6060
When I add "Content-Type" header equal to "application/ld+json"
61-
And I send a "PUT" request to "/uuid_identifier_dummies/41B29566-144B-11E6-A148-3E1D05DEFE78" with body:
61+
And I send a "PUT" request to "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78" with body:
6262
"""
6363
{
6464
"name": "My Dummy modified"
@@ -71,9 +71,9 @@ Feature: Using uuid identifier on resource
7171
"""
7272
{
7373
"@context": "/contexts/UuidIdentifierDummy",
74-
"@id": "/uuid_identifier_dummies/41B29566-144B-11E6-A148-3E1D05DEFE78",
74+
"@id": "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78",
7575
"@type": "UuidIdentifierDummy",
76-
"uuid": "41B29566-144B-11E6-A148-3E1D05DEFE78",
76+
"uuid": "41b29566-144b-11e6-a148-3e1d05defe78",
7777
"name": "My Dummy modified"
7878
}
7979
"""
@@ -99,6 +99,6 @@ Feature: Using uuid identifier on resource
9999

100100
@dropSchema
101101
Scenario: Delete a resource
102-
When I send a "DELETE" request to "/uuid_identifier_dummies/41B29566-144B-11E6-A148-3E1D05DEFE78"
102+
When I send a "DELETE" request to "/uuid_identifier_dummies/41b29566-144b-11e6-a148-3e1d05defe78"
103103
Then the response status code should be 204
104104
And the response should be empty

src/Bridge/Doctrine/Orm/Filter/AbstractFilter.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use ApiPlatform\Core\Util\RequestParser;
2020
use Doctrine\Common\Persistence\ManagerRegistry;
2121
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
22+
use Doctrine\DBAL\Types\Type;
2223
use Doctrine\ORM\Query\Expr\Join;
2324
use Doctrine\ORM\QueryBuilder;
2425
use Psr\Log\LoggerInterface;
@@ -399,4 +400,29 @@ protected function addJoinOnce(QueryBuilder $queryBuilder, QueryNameGeneratorInt
399400

400401
return $associationAlias;
401402
}
403+
404+
/**
405+
* Returns the correct value for a given property according to the database platform.
406+
*
407+
* @see Type::convertToDatabaseValue
408+
*
409+
* @param $phpValue the current value to be converted to a database valid one
410+
*
411+
* @return the database compatible value
412+
*/
413+
protected function convertToDatabaseValue(string $property, string $resourceClass, QueryBuilder $queryBuilder, $phpValue)
414+
{
415+
$propertyParts = $this->splitPropertyParts($property, $resourceClass);
416+
$metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
417+
418+
return Type::getType(
419+
$metadata->getTypeOfField($propertyParts['field'])
420+
)->convertToDatabaseValue(
421+
$phpValue,
422+
$queryBuilder
423+
->getEntityManager()
424+
->getConnection()
425+
->getDatabasePlatform()
426+
);
427+
}
402428
}

src/Bridge/Doctrine/Orm/Filter/DateFilter.php

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter;
1515

1616
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
17-
use Doctrine\DBAL\Types\Type;
1817
use Doctrine\ORM\QueryBuilder;
1918

2019
/**
@@ -86,7 +85,6 @@ protected function filterProperty(string $property, $values, QueryBuilder $query
8685

8786
$alias = 'o';
8887
$field = $property;
89-
$type = $this->getDateType($property, $resourceClass);
9088

9189
if ($this->isPropertyNested($property, $resourceClass)) {
9290
list($alias, $field) = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass);
@@ -105,9 +103,8 @@ protected function filterProperty(string $property, $values, QueryBuilder $query
105103
$alias,
106104
$field,
107105
self::PARAMETER_BEFORE,
108-
$values[self::PARAMETER_BEFORE],
109-
$nullManagement,
110-
$type
106+
$this->convertToDatabaseValue($property, $resourceClass, $queryBuilder, new \DateTime($values[self::PARAMETER_BEFORE])),
107+
$nullManagement
111108
);
112109
}
113110

@@ -118,9 +115,8 @@ protected function filterProperty(string $property, $values, QueryBuilder $query
118115
$alias,
119116
$field,
120117
self::PARAMETER_STRICTLY_BEFORE,
121-
$values[self::PARAMETER_STRICTLY_BEFORE],
122-
$nullManagement,
123-
$type
118+
$this->convertToDatabaseValue($property, $resourceClass, $queryBuilder, new \DateTime($values[self::PARAMETER_STRICTLY_BEFORE])),
119+
$nullManagement
124120
);
125121
}
126122

@@ -131,9 +127,8 @@ protected function filterProperty(string $property, $values, QueryBuilder $query
131127
$alias,
132128
$field,
133129
self::PARAMETER_AFTER,
134-
$values[self::PARAMETER_AFTER],
135-
$nullManagement,
136-
$type
130+
$this->convertToDatabaseValue($property, $resourceClass, $queryBuilder, new \DateTime($values[self::PARAMETER_AFTER])),
131+
$nullManagement
137132
);
138133
}
139134

@@ -144,9 +139,8 @@ protected function filterProperty(string $property, $values, QueryBuilder $query
144139
$alias,
145140
$field,
146141
self::PARAMETER_STRICTLY_AFTER,
147-
$values[self::PARAMETER_STRICTLY_AFTER],
148-
$nullManagement,
149-
$type
142+
$this->convertToDatabaseValue($property, $resourceClass, $queryBuilder, new \DateTime($values[self::PARAMETER_STRICTLY_AFTER])),
143+
$nullManagement
150144
);
151145
}
152146
}
@@ -162,7 +156,7 @@ protected function filterProperty(string $property, $values, QueryBuilder $query
162156
* @param string $value
163157
* @param string|null $nullManagement
164158
*/
165-
protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, string $operator, string $value, string $nullManagement = null, Type $type = null)
159+
protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, string $operator, string $value, string $nullManagement = null)
166160
{
167161
$valueParameter = $queryNameGenerator->generateParameterName($field);
168162

@@ -191,7 +185,7 @@ protected function addWhere(QueryBuilder $queryBuilder, QueryNameGeneratorInterf
191185
));
192186
}
193187

194-
$queryBuilder->setParameter($valueParameter, $type->convertToDatabaseValue(new \DateTime($value), $queryBuilder->getEntityManager()->getConnection()->getDatabasePlatform()));
188+
$queryBuilder->setParameter($valueParameter, $value);
195189
}
196190

197191
/**
@@ -210,22 +204,6 @@ protected function isDateField(string $property, string $resourceClass): bool
210204
return isset(self::DOCTRINE_DATE_TYPES[$metadata->getTypeOfField($propertyParts['field'])]);
211205
}
212206

213-
/**
214-
* Returns the doctrine type for a given property.
215-
*
216-
* @param string $property
217-
* @param string $resourceClass
218-
*
219-
* @return Type
220-
*/
221-
protected function getDateType(string $property, string $resourceClass): Type
222-
{
223-
$propertyParts = $this->splitPropertyParts($property, $resourceClass);
224-
$metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
225-
226-
return Type::getType($metadata->getTypeOfField($propertyParts['field']));
227-
}
228-
229207
/**
230208
* Gets filter description.
231209
*

src/Bridge/Doctrine/Orm/Filter/NumericFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
124124

125125
$queryBuilder
126126
->andWhere(sprintf('%s.%s = :%s', $alias, $field, $valueParameter))
127-
->setParameter($valueParameter, $value);
127+
->setParameter($valueParameter, $this->convertToDatabaseValue($property, $resourceClass, $queryBuilder, $value));
128128
}
129129

130130
/**

0 commit comments

Comments
 (0)