Skip to content

Commit

Permalink
Merge pull request api-platform#1791 from meyerbaptiste/merge_2.2
Browse files Browse the repository at this point in the history
Merge 2.2
  • Loading branch information
soyuka authored Mar 23, 2018
2 parents c11f03a + 4ab4f48 commit 90a61ab
Show file tree
Hide file tree
Showing 18 changed files with 1,158 additions and 97 deletions.
3 changes: 3 additions & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ default:
- 'SwaggerContext'
- 'HttpCacheContext'
- 'JsonApiContext': { doctrine: '@doctrine', jsonApiSchemaFile: 'tests/Fixtures/JsonSchema/jsonapi.json' }
- 'JsonHalContext': { schemaFile: 'tests/Fixtures/JsonHal/jsonhal.json' }
- 'Behat\MinkExtension\Context\MinkContext'
- 'Behatch\Context\RestContext'
filters:
Expand All @@ -22,6 +23,7 @@ default:
- 'SwaggerContext'
- 'HttpCacheContext'
- 'JsonApiContext': { doctrine: '@doctrine', jsonApiSchemaFile: 'tests/Fixtures/JsonSchema/jsonapi.json' }
- 'JsonHalContext': { schemaFile: 'tests/Fixtures/JsonHal/jsonhal.json' }
- 'Behat\MinkExtension\Context\MinkContext'
- 'Behatch\Context\RestContext'
filters:
Expand Down Expand Up @@ -51,6 +53,7 @@ coverage:
- 'SwaggerContext'
- 'HttpCacheContext'
- 'JsonApiContext': { doctrine: '@doctrine', jsonApiSchemaFile: 'tests/Fixtures/JsonSchema/jsonapi.json' }
- 'JsonHalContext': { schemaFile: 'tests/Fixtures/JsonHal/jsonhal.json' }
- 'CoverageContext'
- 'Behat\MinkExtension\Context\MinkContext'
- 'Behatch\Context\RestContext'
12 changes: 9 additions & 3 deletions features/bootstrap/JsonApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behatch\Context\RestContext;
use Behatch\Json\Json;
use Behatch\Json\JsonInspector;
use Doctrine\Common\Persistence\ManagerRegistry;
use JsonSchema\Validator;
use PHPUnit\Framework\ExpectationFailedException;
Expand All @@ -30,6 +31,7 @@ final class JsonApiContext implements Context
*/
private $restContext;
private $validator;
private $inspector;
private $jsonApiSchemaFile;
private $manager;

Expand All @@ -40,6 +42,7 @@ public function __construct(ManagerRegistry $doctrine, string $jsonApiSchemaFile
}

$this->validator = new Validator();
$this->inspector = new JsonInspector('javascript');
$this->jsonApiSchemaFile = $jsonApiSchemaFile;
$this->manager = $doctrine->getManager();
}
Expand All @@ -61,8 +64,12 @@ public function gatherContexts(BeforeScenarioScope $scope)
*/
public function theJsonShouldBeValidAccordingToTheJsonApiSchema()
{
$json = $this->getJson();
$json = $this->getJson()->getContent();
$this->validator->validate($json, (object) ['$ref' => 'file://'.__DIR__.'/../../'.$this->jsonApiSchemaFile]);

if (!$this->validator->isValid()) {
throw new ExpectationFailedException(sprintf('The JSON is not valid according to the JSON API schema.'));
}
}

/**
Expand Down Expand Up @@ -141,8 +148,7 @@ public function thereIsACircularReference()

private function getValueOfNode($node)
{
$json = $this->getJson();
$this->validator->validate($json, $node);
return $this->inspector->evaluate($this->getJson(), $node);
}

private function getJson()
Expand Down
75 changes: 75 additions & 0 deletions features/bootstrap/JsonHalContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behatch\Context\RestContext;
use Behatch\Json\Json;
use JsonSchema\Validator;
use PHPUnit\Framework\ExpectationFailedException;

final class JsonHalContext implements Context
{
/**
* @var RestContext
*/
private $restContext;
private $validator;
private $schemaFile;

public function __construct(string $schemaFile)
{
if (!is_file($schemaFile)) {
throw new \InvalidArgumentException('The JSON HAL schema doesn\'t exist.');
}

$this->validator = new Validator();
$this->schemaFile = $schemaFile;
}

/**
* Gives access to the Behatch context.
*
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
/** @var InitializedContextEnvironment $environment */
$environment = $scope->getEnvironment();
$this->restContext = $environment->getContext(RestContext::class);
}

/**
* @Then the JSON should be valid according to the JSON HAL schema
*/
public function theJsonShouldBeValidAccordingToTheJsonHALSchema()
{
$json = $this->getJson()->getContent();
$this->validator->validate($json, (object) ['$ref' => 'file://'.__DIR__.'/../../'.$this->schemaFile]);

if (!$this->validator->isValid()) {
throw new ExpectationFailedException(sprintf('The JSON is not valid according to the HAL+JSON schema.'));
}
}

private function getJson()
{
return new Json($this->getContent());
}

private function getContent()
{
return $this->restContext->getMink()->getSession()->getDriver()->getContent();
}
}
19 changes: 10 additions & 9 deletions features/graphql/mutation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Feature: GraphQL mutation support
And the JSON node "data.deleteFoo.id" should be equal to "/foos/1"
And the JSON node "data.deleteFoo.clientMutationId" should be equal to "anotherId"

@createSchema
@dropSchema
Scenario: Delete an item with composite identifiers through a mutation
Given there are Composite identifier objects
When I send the following GraphQL request:
Expand All @@ -142,27 +142,28 @@ Feature: GraphQL mutation support

@createSchema
Scenario: Modify an item through a mutation
Given there are 1 foo objects with fake names
Given there are 1 dummy objects
When I send the following GraphQL request:
"""
mutation {
updateFoo(input: {id: "/foos/1", bar: "Modified description.", clientMutationId: "myId"}) {
updateDummy(input: {id: "/dummies/1", description: "Modified description.", dummyDate: "2018-06-05", clientMutationId: "myId"}) {
id
name
bar
description
dummyDate
clientMutationId
}
}
"""
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/json"
And the JSON node "data.updateFoo.id" should be equal to "/foos/1"
And the JSON node "data.updateFoo.name" should be equal to "Hawsepipe"
And the JSON node "data.updateFoo.bar" should be equal to "Modified description."
And the JSON node "data.updateFoo.clientMutationId" should be equal to "myId"
And the JSON node "data.updateDummy.id" should be equal to "/dummies/1"
And the JSON node "data.updateDummy.name" should be equal to "Dummy #1"
And the JSON node "data.updateDummy.description" should be equal to "Modified description."
And the JSON node "data.updateDummy.dummyDate" should be equal to "2018-06-05T00:00:00+00:00"
And the JSON node "data.updateDummy.clientMutationId" should be equal to "myId"

@createSchema
Scenario: Modify an item with composite identifiers through a mutation
Given there are Composite identifier objects
When I send the following GraphQL request:
Expand Down
Loading

0 comments on commit 90a61ab

Please sign in to comment.