Skip to content

Commit acb6ee7

Browse files
authored
Merge pull request #1480 from soyuka/fix/doctrine-type-where
Use database type conversion in Filters (test postgres+mysql)
2 parents 17ab9f5 + c25b81b commit acb6ee7

27 files changed

+588
-127
lines changed

.travis.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ cache:
1010
env:
1111
global:
1212
- SYMFONY_DEPRECATIONS_HELPER=weak_vendors
13+
- APP_ENV=test
1314

1415
matrix:
1516
include:
@@ -18,11 +19,23 @@ matrix:
1819
- php: '7.2'
1920
env: lint=1
2021
- php: '7.2'
21-
env: deps='low'
22+
env: deps=low
2223
- php: '7.2'
23-
env: deps='beta'
24+
env: deps=beta
2425
- php: '7.2'
2526
env: SYMFONY_DEPRECATIONS_HELPER=0
27+
- php: '7.2'
28+
services:
29+
- postgresql
30+
before_script:
31+
- psql -c 'create database api_platform_test;' -U postgres
32+
env: APP_ENV=postgres
33+
- php: '7.2'
34+
services:
35+
- mysql
36+
before_script:
37+
- mysql -e 'CREATE DATABASE api_platform_test;'
38+
env: APP_ENV=mysql
2639
allow_failures:
2740
env: SYMFONY_DEPRECATIONS_HELPER=0
2841

@@ -41,8 +54,10 @@ install:
4154

4255
script:
4356
- vendor/bin/phpunit
44-
- if [[ $deps = 'beta' ]]; then vendor/bin/behat --format=progress --tags='~@nobeta'; fi
45-
- if [[ $deps != 'beta' ]]; then vendor/bin/behat --format=progress; fi
57+
- if [[ $APP_ENV = 'test' && $deps = 'beta' ]]; then vendor/bin/behat --format=progress --tags='~@nobeta&&~@postgres'; fi
58+
- if [[ $APP_ENV = 'test' && $deps != 'beta' ]]; then vendor/bin/behat --format=progress --tags='~@postgres'; fi
59+
- if [[ $APP_ENV = 'postgres' ]]; then vendor/bin/behat --tags='~@sqlite' --format=progress; fi
60+
- if [[ $APP_ENV = 'mysql' ]]; then vendor/bin/behat --tags='~@postgres' --format=progress; fi
4661
- tests/Fixtures/app/console api:swagger:export > swagger.json && swagger-cli validate swagger.json && rm swagger.json
4762
- if [[ $lint = 1 ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi
4863
- if [[ $lint = 1 ]]; then phpstan analyse -c phpstan.neon -l5 --ansi src tests; fi

appveyor.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ cache:
88
init:
99
- SET PATH=c:\tools\php71;%PATH%
1010

11+
environment:
12+
APP_ENV: 'test'
13+
1114
install:
1215
- ps: Set-Service wuauserv -StartupType Manual
1316
- cinst -y php
@@ -26,5 +29,5 @@ install:
2629

2730
test_script:
2831
- cd %APPVEYOR_BUILD_FOLDER%
29-
- php vendor\behat\behat\bin\behat --format=progress
32+
- php vendor\behat\behat\bin\behat --format=progress --tags='~@postgres'
3033
- php vendor\phpunit\phpunit\phpunit

behat.yml.dist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ 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:
15-
env: 'test'
15+
env: '%env(APP_ENV)%'
1616
debug: 'true'
1717
path: 'tests/Fixtures/app/AppKernel.php'
1818
bootstrap: 'tests/Fixtures/app/bootstrap.php'
@@ -28,10 +28,10 @@ coverage:
2828
default:
2929
contexts:
3030
- 'FeatureContext': { doctrine: '@doctrine' }
31+
- 'JsonContext'
3132
- 'HydraContext'
3233
- 'SwaggerContext'
3334
- 'HttpCacheContext'
3435
- 'CoverageContext'
3536
- 'Behat\MinkExtension\Context\MinkContext'
3637
- 'Behatch\Context\RestContext'
37-
- 'Behatch\Context\JsonContext'

features/bootstrap/FeatureContext.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyAggregateOffer;
2121
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
2222
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCarColor;
23+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDate;
2324
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyFriend;
2425
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyGroup;
2526
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyOffer;
@@ -746,4 +747,23 @@ public function createPeopleWithPets()
746747

747748
$this->manager->flush();
748749
}
750+
751+
/**
752+
* @Given there is :nb dummydate objects with dummyDate
753+
*/
754+
public function thereIsDummyDateObjectsWithDummyDate(int $nb)
755+
{
756+
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];
757+
758+
for ($i = 1; $i <= $nb; ++$i) {
759+
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
760+
761+
$dummy = new DummyDate();
762+
$dummy->dummyDate = $date;
763+
764+
$this->manager->persist($dummy);
765+
}
766+
767+
$this->manager->flush();
768+
}
749769
}

features/bootstrap/JsonContext.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
$isObject = is_object($obj);
29+
30+
foreach ($obj as $key => $value) {
31+
if (null === $value || is_scalar($value)) {
32+
continue;
33+
}
34+
35+
if (is_array($value)) {
36+
sort($value);
37+
}
38+
39+
$value = $this->sortArrays($value);
40+
41+
$isObject ? $obj->{$key} = $value : $obj[$key] = $value;
42+
}
43+
44+
return $obj;
45+
}
46+
47+
/**
48+
* @Then /^the JSON should be deep equal to:$/
49+
*/
50+
public function theJsonShouldBeDeepEqualTo(PyStringNode $content)
51+
{
52+
$actual = $this->getJson();
53+
try {
54+
$expected = new Json($content);
55+
} catch (\Exception $e) {
56+
throw new \Exception('The expected JSON is not a valid');
57+
}
58+
59+
$actual = new Json(json_encode($this->sortArrays($actual->getContent())));
60+
$expected = new Json(json_encode($this->sortArrays($expected->getContent())));
61+
62+
$this->assertSame(
63+
(string) $expected,
64+
(string) $actual,
65+
"The json is equal to:\n".$actual->encode()
66+
);
67+
}
68+
}

features/doctrine/date_filter.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,15 @@ Feature: Date filter on collections
654654
}
655655
"""
656656

657+
@dropSchema
658+
@createSchema
659+
Scenario: Get collection filtered by date that is not a datetime
660+
Given there is "30" dummydate objects with dummyDate
661+
When I send a "GET" request to "/dummy_dates?dummyDate[after]=2015-04-28"
662+
Then the response status code should be 200
663+
And the response should be in JSON
664+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
665+
657666
@dropSchema
658667
@createSchema
659668
Scenario: Get collection filtered by embedded date

features/doctrine/numeric_filter.feature

Lines changed: 19 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,76 +4,9 @@ Feature: Numeric filter on collections
44
I need to retrieve collections with numerical value
55

66
@createSchema
7-
Scenario: Get collection by id equals 9.99 which is not possible
8-
Given there is "30" dummy objects
9-
When I send a "GET" request to "/dummies?id=9.99"
10-
Then the response status code should be 200
11-
And the response should be in JSON
12-
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
13-
And the JSON should be valid according to this schema:
14-
"""
15-
{
16-
"type": "object",
17-
"properties": {
18-
"@context": {"pattern": "^/contexts/Dummy$"},
19-
"@id": {"pattern": "^/dummies"},
20-
"@type": {"pattern": "^hydra:Collection$"},
21-
"hydra:member": {
22-
"type": "array",
23-
"maxItems": 0
24-
},
25-
"hydra:view": {
26-
"type": "object",
27-
"properties": {
28-
"@id": {"pattern": "^/dummies\\?id=9.99$"},
29-
"@type": {"pattern": "^hydra:PartialCollectionView$"}
30-
}
31-
}
32-
}
33-
}
34-
"""
35-
36-
Scenario: Get collection by id 10
37-
When I send a "GET" request to "/dummies?id=10"
38-
Then the response status code should be 200
39-
And the response should be in JSON
40-
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
41-
And the JSON should be valid according to this schema:
42-
"""
43-
{
44-
"type": "object",
45-
"properties": {
46-
"@context": {"pattern": "^/contexts/Dummy$"},
47-
"@id": {"pattern": "^/dummies$"},
48-
"@type": {"pattern": "^hydra:Collection$"},
49-
"hydra:member": {
50-
"type": "array",
51-
"items": {
52-
"type": "object",
53-
"properties": {
54-
"@id": {
55-
"oneOf": [
56-
{"pattern": "^/dummies/10$"}
57-
]
58-
}
59-
}
60-
}
61-
},
62-
"hydra:view": {
63-
"type": "object",
64-
"properties": {
65-
"@id": {"pattern": "^/dummies\\?id=10"},
66-
"@type": {"pattern": "^hydra:PartialCollectionView$"}
67-
}
68-
}
69-
}
70-
}
71-
"""
72-
73-
74-
@dropSchema
75-
Scenario: Get collection ordered by a non valid properties
76-
When I send a "GET" request to "/dummies?unknown=0"
7+
Scenario: Get collection by dummyPrice=9.99
8+
Given there is "10" dummy objects with dummyPrice
9+
When I send a "GET" request to "/dummies?dummyPrice=9.99"
7710
Then the response status code should be 200
7811
And the response should be in JSON
7912
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
@@ -93,25 +26,31 @@ Feature: Numeric filter on collections
9326
"@id": {
9427
"oneOf": [
9528
{"pattern": "^/dummies/1$"},
96-
{"pattern": "^/dummies/2$"},
97-
{"pattern": "^/dummies/3$"}
29+
{"pattern": "^/dummies/5$"},
30+
{"pattern": "^/dummies/9$"}
9831
]
9932
}
10033
}
101-
}
34+
},
35+
"maxItems": 3,
36+
"uniqueItems": true
10237
},
38+
"hydra:totalItems": {"pattern": "^3$"},
10339
"hydra:view": {
10440
"type": "object",
10541
"properties": {
106-
"@id": {"pattern": "^/dummies\\?unknown=0"},
42+
"@id": {"pattern": "^/dummies\\?dummyPrice=9.99"},
10743
"@type": {"pattern": "^hydra:PartialCollectionView$"}
10844
}
10945
}
11046
}
11147
}
11248
"""
11349

114-
When I send a "GET" request to "/dummies?unknown=1"
50+
@dropSchema
51+
Scenario: Get collection by non-numeric dummyPrice=marty
52+
Given there is "10" dummy objects with dummyPrice
53+
When I send a "GET" request to "/dummies?dummyPrice=marty"
11554
Then the response status code should be 200
11655
And the response should be in JSON
11756
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
@@ -136,12 +75,15 @@ Feature: Numeric filter on collections
13675
]
13776
}
13877
}
139-
}
78+
},
79+
"maxItems": 3,
80+
"uniqueItems": true
14081
},
82+
"hydra:totalItems": {"pattern": "^20$"},
14183
"hydra:view": {
14284
"type": "object",
14385
"properties": {
144-
"@id": {"pattern": "^/dummies\\?unknown=1"},
86+
"@id": {"pattern": "^/dummies\\?dummyPrice=marty"},
14587
"@type": {"pattern": "^hydra:PartialCollectionView$"}
14688
}
14789
}

0 commit comments

Comments
 (0)