function ResourceResponseValidatorTest::validateResponseProvider

Provides test cases for testValidateResponse.

Return value

array An array of test cases.

File

core/modules/jsonapi/tests/src/Unit/EventSubscriber/ResourceResponseValidatorTest.php, line 75

Class

ResourceResponseValidatorTest
@coversDefaultClass \Drupal\jsonapi\EventSubscriber\ResourceResponseValidator[[api-linebreak]] @group jsonapi

Namespace

Drupal\Tests\jsonapi\Unit\EventSubscriber

Code

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.