You are here

public function ResourceResponseValidatorTest::validateResponseProvider in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php \Drupal\Tests\jsonapi\Unit\EventSubscriber\ResourceResponseValidatorTest::validateResponseProvider()

Provides test cases for testValidateResponse.

Return value

array An array of test cases.

File

tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php, line 190

Class

ResourceResponseValidatorTest
@coversDefaultClass \Drupal\jsonapi\EventSubscriber\ResourceResponseValidator @group jsonapi

Namespace

Drupal\Tests\jsonapi\Unit\EventSubscriber

Code

public 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) {
    list($json, $expected, $description, $route_name, $resource_type) = array_values($input + $defaults);
    return [
      $this
        ->createRequest($route_name, $resource_type),
      $this
        ->createResponse($json),
      $expected,
      $description,
    ];
  }, $test_data);
  return $test_cases;
}