class ResourceResponseValidatorTest
Same name in other branches
- 9 core/modules/jsonapi/tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php \Drupal\Tests\jsonapi\Unit\EventSubscriber\ResourceResponseValidatorTest
- 8.9.x core/modules/jsonapi/tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php \Drupal\Tests\jsonapi\Unit\EventSubscriber\ResourceResponseValidatorTest
- 11.x core/modules/jsonapi/tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php \Drupal\Tests\jsonapi\Unit\EventSubscriber\ResourceResponseValidatorTest
@coversDefaultClass \Drupal\jsonapi\EventSubscriber\ResourceResponseValidator @group jsonapi
@internal
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\jsonapi\Unit\EventSubscriber\ResourceResponseValidatorTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of ResourceResponseValidatorTest
File
-
core/
modules/ jsonapi/ tests/ src/ Unit/ EventSubscriber/ ResourceResponseValidatorTest.php, line 24
Namespace
Drupal\Tests\jsonapi\Unit\EventSubscriberView source
class ResourceResponseValidatorTest extends UnitTestCase {
/**
* The subscriber under test.
*
* @var \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber
*/
protected $subscriber;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Check that the validation class is available.
if (!class_exists("\\JsonSchema\\Validator")) {
$this->fail('The JSON Schema validator is missing. You can install it with `composer require justinrainbow/json-schema`.');
}
$module_handler = $this->prophesize(ModuleHandlerInterface::class);
$module = $this->prophesize(Extension::class);
$module_path = dirname(__DIR__, 4);
$module->getPath()
->willReturn($module_path);
$module_handler->getModule('jsonapi')
->willReturn($module->reveal());
$subscriber = new ResourceResponseValidator($this->prophesize(LoggerInterface::class)
->reveal(), $module_handler->reveal(), '');
$subscriber->setValidator();
$this->subscriber = $subscriber;
}
/**
* @covers ::validateResponse
* @dataProvider validateResponseProvider
*/
public function testValidateResponse($request, $response, $expected, $description) : void {
// Expose protected ResourceResponseSubscriber::validateResponse() method.
$object = new \ReflectionObject($this->subscriber);
$method = $object->getMethod('validateResponse');
$this->assertSame($expected, $method->invoke($this->subscriber, $response, $request), $description);
}
/**
* Provides test cases for testValidateResponse.
*
* @return array
* An array of test cases.
*/
public static function validateResponseProvider() {
$defaults = [
'route_name' => 'jsonapi.node--article.individual',
'resource_type' => new ResourceType('node', 'article', NULL),
];
$test_data = [
// Test validation success.
[
'json' => <<<'EOD'
{
"data": {
"type": "node--article",
"id": "4f342419-e668-4b76-9f87-7ce20c436169",
"attributes": {
"nid": "1",
"uuid": "4f342419-e668-4b76-9f87-7ce20c436169"
}
}
}
EOD
,
'expected' => TRUE,
'description' => 'Response validation flagged a valid response.',
],
// Test validation failure: no "type" in "data".
[
'json' => <<<'EOD'
{
"data": {
"id": "4f342419-e668-4b76-9f87-7ce20c436169",
"attributes": {
"nid": "1",
"uuid": "4f342419-e668-4b76-9f87-7ce20c436169"
}
}
}
EOD
,
'expected' => FALSE,
'description' => 'Response validation failed to flag an invalid response.',
],
// Test validation failure: "errors" at the root level.
[
'json' => <<<'EOD'
{
"data": {
"type": "node--article",
"id": "4f342419-e668-4b76-9f87-7ce20c436169",
"attributes": {
"nid": "1",
"uuid": "4f342419-e668-4b76-9f87-7ce20c436169"
}
},
"errors": [{}]
}
EOD
,
'expected' => FALSE,
'description' => 'Response validation failed to flag an invalid response.',
],
// Test validation of an empty response passes.
[
'json' => NULL,
'expected' => TRUE,
'description' => 'Response validation flagged a valid empty response.',
],
// Test validation fails on empty object.
[
'json' => '{}',
'expected' => FALSE,
'description' => 'Response validation flags empty array as invalid.',
],
];
$test_cases = array_map(function ($input) use ($defaults) {
[
$json,
$expected,
$description,
$route_name,
$resource_type,
] = array_values($input + $defaults);
return [
static::createRequest($route_name, $resource_type),
static::createResponse($json),
$expected,
$description,
];
}, $test_data);
return $test_cases;
}
/**
* Helper method to create a request object.
*
* @param string $route_name
* The route name with which to construct a request.
* @param \Drupal\jsonapi\ResourceType\ResourceType $resource_type
* The resource type for the requested route.
*
* @return \Symfony\Component\HttpFoundation\Request
* The mock request object.
*/
protected static function createRequest(string $route_name, ResourceType $resource_type) : Request {
$request = new Request();
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, $route_name);
$request->attributes
->set(Routes::RESOURCE_TYPE_KEY, $resource_type);
return $request;
}
/**
* Helper method to create a resource response from arbitrary JSON.
*
* @param string|null $json
* The JSON with which to create a mock response.
*
* @return \Drupal\rest\ResourceResponse
* The mock response object.
*/
protected static function createResponse(?string $json = NULL) : ResourceResponse {
$response = new ResourceResponse();
if ($json) {
$response->setContent($json);
}
return $response;
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
ResourceResponseValidatorTest::$subscriber | protected | property | The subscriber under test. | |||
ResourceResponseValidatorTest::createRequest | protected static | function | Helper method to create a request object. | |||
ResourceResponseValidatorTest::createResponse | protected static | function | Helper method to create a resource response from arbitrary JSON. | |||
ResourceResponseValidatorTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
ResourceResponseValidatorTest::testValidateResponse | public | function | @covers ::validateResponse @dataProvider validateResponseProvider |
|||
ResourceResponseValidatorTest::validateResponseProvider | public static | function | Provides test cases for testValidateResponse. | |||
UnitTestCase::$root | protected | property | The app root. | 1 | ||
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.