Skip to content

Use database type conversion in Filters (test postgres+mysql) #1480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cache:
env:
global:
- SYMFONY_DEPRECATIONS_HELPER=weak_vendors
- APP_ENV=test

matrix:
include:
Expand All @@ -18,11 +19,23 @@ matrix:
- php: '7.2'
env: lint=1
- php: '7.2'
env: deps='low'
env: deps=low
- php: '7.2'
env: deps='beta'
env: deps=beta
- php: '7.2'
env: SYMFONY_DEPRECATIONS_HELPER=0
- php: '7.2'
services:
- postgresql
before_script:
- psql -c 'create database api_platform_test;' -U postgres
env: APP_ENV=postgres
- php: '7.2'
services:
- mysql
before_script:
- mysql -e 'CREATE DATABASE api_platform_test;'
env: APP_ENV=mysql
allow_failures:
env: SYMFONY_DEPRECATIONS_HELPER=0

Expand All @@ -41,8 +54,10 @@ install:

script:
- vendor/bin/phpunit
- if [[ $deps = 'beta' ]]; then vendor/bin/behat --format=progress --tags='~@nobeta'; fi
- if [[ $deps != 'beta' ]]; then vendor/bin/behat --format=progress; fi
- if [[ $APP_ENV = 'test' && $deps = 'beta' ]]; then vendor/bin/behat --format=progress --tags='~@nobeta&&~@postgres'; fi
- if [[ $APP_ENV = 'test' && $deps != 'beta' ]]; then vendor/bin/behat --format=progress --tags='~@postgres'; fi
- if [[ $APP_ENV = 'postgres' ]]; then vendor/bin/behat --tags='~@sqlite' --format=progress; fi
- if [[ $APP_ENV = 'mysql' ]]; then vendor/bin/behat --tags='~@postgres' --format=progress; fi
- tests/Fixtures/app/console api:swagger:export > swagger.json && swagger-cli validate swagger.json && rm swagger.json
- if [[ $lint = 1 ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi
- if [[ $lint = 1 ]]; then phpstan analyse -c phpstan.neon -l5 --ansi src tests; fi
5 changes: 4 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ cache:
init:
- SET PATH=c:\tools\php71;%PATH%

environment:
APP_ENV: 'test'

install:
- ps: Set-Service wuauserv -StartupType Manual
- cinst -y php
Expand All @@ -26,5 +29,5 @@ install:

test_script:
- cd %APPVEYOR_BUILD_FOLDER%
- php vendor\behat\behat\bin\behat --format=progress
- php vendor\behat\behat\bin\behat --format=progress --tags='~@postgres'
- php vendor\phpunit\phpunit\phpunit
6 changes: 3 additions & 3 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ default:
default:
contexts:
- 'FeatureContext': { doctrine: '@doctrine' }
- 'JsonContext'
- 'HydraContext'
- 'SwaggerContext'
- 'HttpCacheContext'
- 'Behat\MinkExtension\Context\MinkContext'
- 'Behatch\Context\RestContext'
- 'Behatch\Context\JsonContext'
extensions:
'Behat\Symfony2Extension':
kernel:
env: 'test'
env: '%env(APP_ENV)%'
debug: 'true'
path: 'tests/Fixtures/app/AppKernel.php'
bootstrap: 'tests/Fixtures/app/bootstrap.php'
Expand All @@ -28,10 +28,10 @@ coverage:
default:
contexts:
- 'FeatureContext': { doctrine: '@doctrine' }
- 'JsonContext'
- 'HydraContext'
- 'SwaggerContext'
- 'HttpCacheContext'
- 'CoverageContext'
- 'Behat\MinkExtension\Context\MinkContext'
- 'Behatch\Context\RestContext'
- 'Behatch\Context\JsonContext'
20 changes: 20 additions & 0 deletions features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyAggregateOffer;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCarColor;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDate;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyFriend;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyGroup;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyOffer;
Expand Down Expand Up @@ -746,4 +747,23 @@ public function createPeopleWithPets()

$this->manager->flush();
}

/**
* @Given there is :nb dummydate objects with dummyDate
*/
public function thereIsDummyDateObjectsWithDummyDate(int $nb)
{
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];

for ($i = 1; $i <= $nb; ++$i) {
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));

$dummy = new DummyDate();
$dummy->dummyDate = $date;

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

$this->manager->flush();
}
}
68 changes: 68 additions & 0 deletions features/bootstrap/JsonContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

use Behat\Gherkin\Node\PyStringNode;
use Behatch\Context\JsonContext as BaseJsonContext;
use Behatch\HttpCall\HttpCallResultPool;
use Behatch\Json\Json;

final class JsonContext extends BaseJsonContext
{
public function __construct(HttpCallResultPool $httpCallResultPool)
{
parent::__construct($httpCallResultPool);
}

private function sortArrays($obj)
{
$isObject = is_object($obj);

foreach ($obj as $key => $value) {
if (null === $value || is_scalar($value)) {
continue;
}

if (is_array($value)) {
sort($value);
}

$value = $this->sortArrays($value);

$isObject ? $obj->{$key} = $value : $obj[$key] = $value;
}

return $obj;
}

/**
* @Then /^the JSON should be deep equal to:$/
*/
public function theJsonShouldBeDeepEqualTo(PyStringNode $content)
{
$actual = $this->getJson();
try {
$expected = new Json($content);
} catch (\Exception $e) {
throw new \Exception('The expected JSON is not a valid');
}

$actual = new Json(json_encode($this->sortArrays($actual->getContent())));
$expected = new Json(json_encode($this->sortArrays($expected->getContent())));

$this->assertSame(
(string) $expected,
(string) $actual,
"The json is equal to:\n".$actual->encode()
);
}
}
9 changes: 9 additions & 0 deletions features/doctrine/date_filter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,15 @@ Feature: Date filter on collections
}
"""

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

@dropSchema
@createSchema
Scenario: Get collection filtered by embedded date
Expand Down
96 changes: 19 additions & 77 deletions features/doctrine/numeric_filter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,9 @@ Feature: Numeric filter on collections
I need to retrieve collections with numerical value

@createSchema
Scenario: Get collection by id equals 9.99 which is not possible
Given there is "30" dummy objects
When I send a "GET" request to "/dummies?id=9.99"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be valid according to this schema:
"""
{
"type": "object",
"properties": {
"@context": {"pattern": "^/contexts/Dummy$"},
"@id": {"pattern": "^/dummies"},
"@type": {"pattern": "^hydra:Collection$"},
"hydra:member": {
"type": "array",
"maxItems": 0
},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/dummies\\?id=9.99$"},
"@type": {"pattern": "^hydra:PartialCollectionView$"}
}
}
}
}
"""

Scenario: Get collection by id 10
When I send a "GET" request to "/dummies?id=10"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
And the JSON should be valid according to this schema:
"""
{
"type": "object",
"properties": {
"@context": {"pattern": "^/contexts/Dummy$"},
"@id": {"pattern": "^/dummies$"},
"@type": {"pattern": "^hydra:Collection$"},
"hydra:member": {
"type": "array",
"items": {
"type": "object",
"properties": {
"@id": {
"oneOf": [
{"pattern": "^/dummies/10$"}
]
}
}
}
},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/dummies\\?id=10"},
"@type": {"pattern": "^hydra:PartialCollectionView$"}
}
}
}
}
"""


@dropSchema
Scenario: Get collection ordered by a non valid properties
When I send a "GET" request to "/dummies?unknown=0"
Scenario: Get collection by dummyPrice=9.99
Given there is "10" dummy objects with dummyPrice
When I send a "GET" request to "/dummies?dummyPrice=9.99"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
Expand All @@ -93,25 +26,31 @@ Feature: Numeric filter on collections
"@id": {
"oneOf": [
{"pattern": "^/dummies/1$"},
{"pattern": "^/dummies/2$"},
{"pattern": "^/dummies/3$"}
{"pattern": "^/dummies/5$"},
{"pattern": "^/dummies/9$"}
]
}
}
}
},
"maxItems": 3,
"uniqueItems": true
},
"hydra:totalItems": {"pattern": "^3$"},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/dummies\\?unknown=0"},
"@id": {"pattern": "^/dummies\\?dummyPrice=9.99"},
"@type": {"pattern": "^hydra:PartialCollectionView$"}
}
}
}
}
"""

When I send a "GET" request to "/dummies?unknown=1"
@dropSchema
Scenario: Get collection by non-numeric dummyPrice=marty
Given there is "10" dummy objects with dummyPrice
When I send a "GET" request to "/dummies?dummyPrice=marty"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
Expand All @@ -136,12 +75,15 @@ Feature: Numeric filter on collections
]
}
}
}
},
"maxItems": 3,
"uniqueItems": true
},
"hydra:totalItems": {"pattern": "^20$"},
"hydra:view": {
"type": "object",
"properties": {
"@id": {"pattern": "^/dummies\\?unknown=1"},
"@id": {"pattern": "^/dummies\\?dummyPrice=marty"},
"@type": {"pattern": "^hydra:PartialCollectionView$"}
}
}
Expand Down
Loading