Skip to content

Commit c2e5b77

Browse files
bendaviesdunglas
authored andcommitted
hal collection links should have a href property (#1775)
* hal collection links should have a href property * flesh out hal collection feature * validate hal responses against the hal+json schema * fix schema validation
1 parent be06b0c commit c2e5b77

File tree

9 files changed

+1022
-71
lines changed

9 files changed

+1022
-71
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,12 @@ public function gatherContexts(BeforeScenarioScope $scope)
6161
*/
6262
public function theJsonShouldBeValidAccordingToTheJsonApiSchema()
6363
{
64-
$json = $this->getJson();
64+
$json = $this->getJson()->getContent();
6565
$this->validator->validate($json, (object) ['$ref' => 'file://'.__DIR__.'/../../'.$this->jsonApiSchemaFile]);
66+
67+
if (!$this->validator->isValid()) {
68+
throw new ExpectationFailedException(sprintf('The JSON is not valid according to the JSON API schema.'));
69+
}
6670
}
6771

6872
/**

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+
}

0 commit comments

Comments
 (0)