Skip to content

Commit 90a61ab

Browse files
authored
Merge pull request #1791 from meyerbaptiste/merge_2.2
Merge 2.2
2 parents c11f03a + 4ab4f48 commit 90a61ab

File tree

18 files changed

+1158
-97
lines changed

18 files changed

+1158
-97
lines changed

behat.yml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ default:
99
- 'SwaggerContext'
1010
- 'HttpCacheContext'
1111
- 'JsonApiContext': { doctrine: '@doctrine', jsonApiSchemaFile: 'tests/Fixtures/JsonSchema/jsonapi.json' }
12+
- 'JsonHalContext': { schemaFile: 'tests/Fixtures/JsonHal/jsonhal.json' }
1213
- 'Behat\MinkExtension\Context\MinkContext'
1314
- 'Behatch\Context\RestContext'
1415
filters:
@@ -22,6 +23,7 @@ default:
2223
- 'SwaggerContext'
2324
- 'HttpCacheContext'
2425
- 'JsonApiContext': { doctrine: '@doctrine', jsonApiSchemaFile: 'tests/Fixtures/JsonSchema/jsonapi.json' }
26+
- 'JsonHalContext': { schemaFile: 'tests/Fixtures/JsonHal/jsonhal.json' }
2527
- 'Behat\MinkExtension\Context\MinkContext'
2628
- 'Behatch\Context\RestContext'
2729
filters:
@@ -51,6 +53,7 @@ coverage:
5153
- 'SwaggerContext'
5254
- 'HttpCacheContext'
5355
- 'JsonApiContext': { doctrine: '@doctrine', jsonApiSchemaFile: 'tests/Fixtures/JsonSchema/jsonapi.json' }
56+
- 'JsonHalContext': { schemaFile: 'tests/Fixtures/JsonHal/jsonhal.json' }
5457
- 'CoverageContext'
5558
- 'Behat\MinkExtension\Context\MinkContext'
5659
- 'Behatch\Context\RestContext'

features/bootstrap/JsonApiContext.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
2020
use Behatch\Context\RestContext;
2121
use Behatch\Json\Json;
22+
use Behatch\Json\JsonInspector;
2223
use Doctrine\Common\Persistence\ManagerRegistry;
2324
use JsonSchema\Validator;
2425
use PHPUnit\Framework\ExpectationFailedException;
@@ -30,6 +31,7 @@ final class JsonApiContext implements Context
3031
*/
3132
private $restContext;
3233
private $validator;
34+
private $inspector;
3335
private $jsonApiSchemaFile;
3436
private $manager;
3537

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

4244
$this->validator = new Validator();
45+
$this->inspector = new JsonInspector('javascript');
4346
$this->jsonApiSchemaFile = $jsonApiSchemaFile;
4447
$this->manager = $doctrine->getManager();
4548
}
@@ -61,8 +64,12 @@ public function gatherContexts(BeforeScenarioScope $scope)
6164
*/
6265
public function theJsonShouldBeValidAccordingToTheJsonApiSchema()
6366
{
64-
$json = $this->getJson();
67+
$json = $this->getJson()->getContent();
6568
$this->validator->validate($json, (object) ['$ref' => 'file://'.__DIR__.'/../../'.$this->jsonApiSchemaFile]);
69+
70+
if (!$this->validator->isValid()) {
71+
throw new ExpectationFailedException(sprintf('The JSON is not valid according to the JSON API schema.'));
72+
}
6673
}
6774

6875
/**
@@ -141,8 +148,7 @@ public function thereIsACircularReference()
141148

142149
private function getValueOfNode($node)
143150
{
144-
$json = $this->getJson();
145-
$this->validator->validate($json, $node);
151+
return $this->inspector->evaluate($this->getJson(), $node);
146152
}
147153

148154
private function getJson()

features/bootstrap/JsonHalContext.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\Behat\Context\Context;
15+
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
16+
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
17+
use Behatch\Context\RestContext;
18+
use Behatch\Json\Json;
19+
use JsonSchema\Validator;
20+
use PHPUnit\Framework\ExpectationFailedException;
21+
22+
final class JsonHalContext implements Context
23+
{
24+
/**
25+
* @var RestContext
26+
*/
27+
private $restContext;
28+
private $validator;
29+
private $schemaFile;
30+
31+
public function __construct(string $schemaFile)
32+
{
33+
if (!is_file($schemaFile)) {
34+
throw new \InvalidArgumentException('The JSON HAL schema doesn\'t exist.');
35+
}
36+
37+
$this->validator = new Validator();
38+
$this->schemaFile = $schemaFile;
39+
}
40+
41+
/**
42+
* Gives access to the Behatch context.
43+
*
44+
* @BeforeScenario
45+
*/
46+
public function gatherContexts(BeforeScenarioScope $scope)
47+
{
48+
/** @var InitializedContextEnvironment $environment */
49+
$environment = $scope->getEnvironment();
50+
$this->restContext = $environment->getContext(RestContext::class);
51+
}
52+
53+
/**
54+
* @Then the JSON should be valid according to the JSON HAL schema
55+
*/
56+
public function theJsonShouldBeValidAccordingToTheJsonHALSchema()
57+
{
58+
$json = $this->getJson()->getContent();
59+
$this->validator->validate($json, (object) ['$ref' => 'file://'.__DIR__.'/../../'.$this->schemaFile]);
60+
61+
if (!$this->validator->isValid()) {
62+
throw new ExpectationFailedException(sprintf('The JSON is not valid according to the HAL+JSON schema.'));
63+
}
64+
}
65+
66+
private function getJson()
67+
{
68+
return new Json($this->getContent());
69+
}
70+
71+
private function getContent()
72+
{
73+
return $this->restContext->getMink()->getSession()->getDriver()->getContent();
74+
}
75+
}

features/graphql/mutation.feature

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Feature: GraphQL mutation support
122122
And the JSON node "data.deleteFoo.id" should be equal to "/foos/1"
123123
And the JSON node "data.deleteFoo.clientMutationId" should be equal to "anotherId"
124124

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

143143
@createSchema
144144
Scenario: Modify an item through a mutation
145-
Given there are 1 foo objects with fake names
145+
Given there are 1 dummy objects
146146
When I send the following GraphQL request:
147147
"""
148148
mutation {
149-
updateFoo(input: {id: "/foos/1", bar: "Modified description.", clientMutationId: "myId"}) {
149+
updateDummy(input: {id: "/dummies/1", description: "Modified description.", dummyDate: "2018-06-05", clientMutationId: "myId"}) {
150150
id
151151
name
152-
bar
152+
description
153+
dummyDate
153154
clientMutationId
154155
}
155156
}
156157
"""
157158
Then the response status code should be 200
158159
And the response should be in JSON
159160
And the header "Content-Type" should be equal to "application/json"
160-
And the JSON node "data.updateFoo.id" should be equal to "/foos/1"
161-
And the JSON node "data.updateFoo.name" should be equal to "Hawsepipe"
162-
And the JSON node "data.updateFoo.bar" should be equal to "Modified description."
163-
And the JSON node "data.updateFoo.clientMutationId" should be equal to "myId"
161+
And the JSON node "data.updateDummy.id" should be equal to "/dummies/1"
162+
And the JSON node "data.updateDummy.name" should be equal to "Dummy #1"
163+
And the JSON node "data.updateDummy.description" should be equal to "Modified description."
164+
And the JSON node "data.updateDummy.dummyDate" should be equal to "2018-06-05T00:00:00+00:00"
165+
And the JSON node "data.updateDummy.clientMutationId" should be equal to "myId"
164166

165-
@createSchema
166167
Scenario: Modify an item with composite identifiers through a mutation
167168
Given there are Composite identifier objects
168169
When I send the following GraphQL request:

0 commit comments

Comments
 (0)