Skip to content

Commit dba1cea

Browse files
author
abluchet
committed
Use type conversion in DateFilter
Add postgres Fix bad identifier behavior in search filter
1 parent d683d00 commit dba1cea

28 files changed

+1174
-578
lines changed

.travis.yml

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,53 @@ matrix:
1616
- php: '7.0'
1717
- php: '7.1'
1818
- php: '7.2'
19-
env: coverage=1 lint=1
19+
env: coverage=1
20+
before_install:
21+
- mkdir -p build/logs build/cov
22+
- wget https://phar.phpunit.de/phpcov.phar
23+
- wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
24+
- wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.7.1/php-cs-fixer.phar
25+
- composer global require --dev 'phpstan/phpstan:^0.8'
26+
script:
27+
- phpdbg -qrr vendor/bin/phpunit --coverage-php build/cov/coverage-phpunit.cov
28+
- for f in $(find features -name '*.feature'); do FEATURE=${f//\//_} echo "Feature $f"; phpdbg -qrr vendor/bin/behat --tags ~@postgres --format=progress --profile coverage $f || exit $?; done;
29+
- phpdbg -qrr phpcov.phar merge --clover build/logs/clover.xml build/cov
30+
- php php-cs-fixer.phar fix --dry-run --diff --no-ansi
31+
- phpstan analyse -c phpstan.neon -l5 --ansi src tests
32+
- tests/Fixtures/app/console api:swagger:export > swagger.json && swagger-cli validate swagger.json && rm swagger.json
33+
after_success:
34+
- travis_retry php coveralls.phar
2035
- php: '7.2'
2136
env: deps='low'
2237
- php: '7.2'
2338
env: SYMFONY_DEPRECATIONS_HELPER=0
39+
- php: '7.2'
40+
services:
41+
- postgresql
42+
before_install:
43+
- psql -c 'create database api_platform_test;' -U postgres
44+
- cp tests/Fixtures/app/config/parameters.postgres.yml tests/Fixtures/app/config/parameters.yml
45+
env: postgres=1
46+
- php: '7.2'
47+
services:
48+
- mysql
49+
before_install:
50+
- mysql -e 'CREATE DATABASE api_platform_test;'
51+
- cp tests/Fixtures/app/config/parameters.mysql.yml tests/Fixtures/app/config/parameters.yml
52+
env: mysql=1
2453
allow_failures:
2554
env: SYMFONY_DEPRECATIONS_HELPER=0
2655

27-
before_install:
56+
install:
2857
- phpenv config-rm xdebug.ini || echo "xdebug not available"
2958
- echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
3059
- npm install -g swagger-cli
3160
- export PATH="$PATH:$HOME/.composer/vendor/bin"
32-
- if [[ $coverage = 1 ]]; then mkdir -p build/logs build/cov; fi
33-
- if [[ $coverage = 1 ]]; then wget https://phar.phpunit.de/phpcov.phar; fi
34-
- if [[ $coverage = 1 ]]; then wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar; fi
35-
- if [[ $lint = 1 ]]; then wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.7.1/php-cs-fixer.phar; fi
36-
- if [[ $lint = 1 ]]; then composer global require --dev 'phpstan/phpstan:^0.8'; fi
37-
38-
install:
3961
- if [[ $coverage = 1 ]]; then composer require --dev --no-update 'phpunit/php-code-coverage:^5.2.2'; fi
4062
- if [[ ! $deps ]]; then composer update --prefer-dist --no-progress --no-suggest --ansi; fi
4163
- if [[ $deps = 'low' ]]; then composer update --prefer-dist --no-progress --no-suggest --prefer-stable --prefer-lowest --ansi; fi
4264

4365
script:
44-
- if [[ $coverage = 1 ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-php build/cov/coverage-phpunit.cov; else vendor/bin/phpunit; fi
45-
- 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
46-
- if [[ $coverage = 1 ]]; then phpdbg -qrr phpcov.phar merge --clover build/logs/clover.xml build/cov; fi
66+
- vendor/bin/phpunit
67+
- if [[ $postgres = 1 ]]; then vendor/bin/behat --tags ~@sqlite --stop-on-failure; elif [[ $mysql = 1 ]]; then vendor/bin/behat --tags ~@postgres --stop-on-failure; else vendor/bin/behat --tags ~@postgres --format=progress; fi
4768
- tests/Fixtures/app/console api:swagger:export > swagger.json && swagger-cli validate swagger.json && rm swagger.json
48-
- if [[ $lint = 1 ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi
49-
- if [[ $lint = 1 ]]; then phpstan analyse -c phpstan.neon -l5 --ansi src tests; fi
50-
51-
after_success:
52-
- if [[ $coverage = 1 ]]; then travis_retry php coveralls.phar; fi

behat.yml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ default:
44
contexts:
55
- 'FeatureContext': { doctrine: '@doctrine' }
66
- 'GraphqlContext'
7+
- 'JsonContext'
78
- 'HydraContext'
89
- 'SwaggerContext'
910
- 'HttpCacheContext'
1011
- 'JsonApiContext': { doctrine: '@doctrine', jsonApiSchemaFile: 'tests/Fixtures/JsonSchema/jsonapi.json' }
1112
- 'Behat\MinkExtension\Context\MinkContext'
1213
- 'Behatch\Context\RestContext'
13-
- 'Behatch\Context\JsonContext'
1414
extensions:
1515
'Behat\Symfony2Extension':
1616
kernel:
@@ -31,11 +31,11 @@ coverage:
3131
contexts:
3232
- 'FeatureContext': { doctrine: '@doctrine' }
3333
- 'GraphqlContext'
34+
- 'JsonContext'
3435
- 'HydraContext'
3536
- 'SwaggerContext'
3637
- 'HttpCacheContext'
3738
- 'JsonApiContext': { doctrine: '@doctrine', jsonApiSchemaFile: 'tests/Fixtures/JsonSchema/jsonapi.json' }
3839
- 'CoverageContext'
3940
- 'Behat\MinkExtension\Context\MinkContext'
4041
- 'Behatch\Context\RestContext'
41-
- 'Behatch\Context\JsonContext'

features/bootstrap/FeatureContext.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyAggregateOffer;
2222
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
2323
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCarColor;
24+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDate;
2425
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyFriend;
2526
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyGroup;
2627
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyOffer;
@@ -780,4 +781,23 @@ public function createPeopleWithPets()
780781

781782
$this->manager->flush();
782783
}
784+
785+
/**
786+
* @Given there is :nb dummydate objects with dummyDate
787+
*/
788+
public function thereIsDummyDateObjectsWithDummyDate(int $nb)
789+
{
790+
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];
791+
792+
for ($i = 1; $i <= $nb; ++$i) {
793+
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
794+
795+
$dummy = new DummyDate();
796+
$dummy->dummyDate = $date;
797+
798+
$this->manager->persist($dummy);
799+
}
800+
801+
$this->manager->flush();
802+
}
783803
}

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
@@ -660,6 +660,15 @@ Feature: Date filter on collections
660660
}
661661
"""
662662

663+
@dropSchema
664+
@createSchema
665+
Scenario: Get collection filtered by date that is not a datetime
666+
Given there is "30" dummydate objects with dummyDate
667+
When I send a "GET" request to "/dummy_dates?dummyDate[after]=2015-04-28"
668+
Then the response status code should be 200
669+
And the response should be in JSON
670+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
671+
663672
@dropSchema
664673
@createSchema
665674
Scenario: Get collection filtered by embedded date

features/doctrine/numeric_filter.feature

Lines changed: 20 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,12 @@
1-
Feature: Numeric filter on collections
1+
Feature: Numeric search filter on collections
22
In order to retrieve ordered large collections of resources
33
As a client software developer
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)