class JsonApiDocumentTopLevelNormalizerTest

Same name in this branch
  1. 10 core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest
Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest
  2. 9 core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest
  3. 8.9.x core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest
  4. 8.9.x core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest
  5. 11.x core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest
  6. 11.x core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest

@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer
@group jsonapi

@internal

Hierarchy

Expanded class hierarchy of JsonApiDocumentTopLevelNormalizerTest

File

core/modules/jsonapi/tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 27

Namespace

Drupal\Tests\jsonapi\Unit\Normalizer
View source
class JsonApiDocumentTopLevelNormalizerTest extends UnitTestCase {
  
  /**
   * The normalizer under test.
   *
   * @var \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer
   */
  protected $normalizer;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $resource_type_repository = $this->prophesize(ResourceTypeRepository::class);
    $resource_type_repository->getByTypeName(Argument::any())
      ->willReturn(new ResourceType('node', 'article', NULL));
    $entity_storage = $this->prophesize(EntityStorageInterface::class);
    $self = $this;
    $uuid_to_id = [
      '76dd5c18-ea1b-4150-9e75-b21958a2b836' => 1,
      'fcce1b61-258e-4054-ae36-244d25a9e04c' => 2,
    ];
    $entity_storage->loadByProperties(Argument::type('array'))
      ->will(function ($args) use ($self, $uuid_to_id) {
      $result = [];
      foreach ($args[0]['uuid'] as $uuid) {
        $entity = $self->prophesize(EntityInterface::class);
        $entity->uuid()
          ->willReturn($uuid);
        $entity->id()
          ->willReturn($uuid_to_id[$uuid]);
        $result[$uuid] = $entity->reveal();
      }
      return $result;
    });
    $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
    $entity_type_manager->getStorage('node')
      ->willReturn($entity_storage->reveal());
    $entity_type = $this->prophesize(EntityTypeInterface::class);
    $entity_type->getKey('uuid')
      ->willReturn('uuid');
    $entity_type_manager->getDefinition('node')
      ->willReturn($entity_type->reveal());
    $this->normalizer = new JsonApiDocumentTopLevelNormalizer($entity_type_manager->reveal(), $resource_type_repository->reveal());
    $serializer = $this->prophesize(DenormalizerInterface::class);
    $serializer->willImplement(SerializerInterface::class);
    $serializer->denormalize(Argument::type('array'), Argument::type('string'), Argument::type('string'), Argument::type('array'))
      ->willReturnArgument(0);
    $this->normalizer
      ->setSerializer($serializer->reveal());
  }
  
  /**
   * @covers ::denormalize
   * @dataProvider denormalizeProvider
   */
  public function testDenormalize($input, $expected) : void {
    $resource_type = new ResourceType('node', 'article', FieldableEntityInterface::class);
    $resource_type->setRelatableResourceTypes([]);
    $context = [
      'resource_type' => $resource_type,
    ];
    $denormalized = $this->normalizer
      ->denormalize($input, NULL, 'api_json', $context);
    $this->assertSame($expected, $denormalized);
  }
  
  /**
   * Data provider for the denormalize test.
   *
   * @return array
   *   The data for the test method.
   */
  public static function denormalizeProvider() {
    return [
      [
        [
          'data' => [
            'type' => 'lorem',
            'id' => 'e1a613f6-f2b9-4e17-9d33-727eb6509d8b',
            'attributes' => [
              'title' => 'dummy_title',
            ],
          ],
        ],
        [
          'title' => 'dummy_title',
          'uuid' => 'e1a613f6-f2b9-4e17-9d33-727eb6509d8b',
        ],
      ],
      [
        [
          'data' => [
            'type' => 'lorem',
            'id' => '0676d1bf-55b3-4bbc-9fbc-3df10f4599d5',
            'relationships' => [
              'field_dummy' => [
                'data' => [
                  'type' => 'node',
                  'id' => '76dd5c18-ea1b-4150-9e75-b21958a2b836',
                ],
              ],
            ],
          ],
        ],
        [
          'uuid' => '0676d1bf-55b3-4bbc-9fbc-3df10f4599d5',
          'field_dummy' => [
            [
              'target_id' => 1,
            ],
          ],
        ],
      ],
      [
        [
          'data' => [
            'type' => 'lorem',
            'id' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
            'relationships' => [
              'field_dummy' => [
                'data' => [
                  [
                    'type' => 'node',
                    'id' => '76dd5c18-ea1b-4150-9e75-b21958a2b836',
                  ],
                  [
                    'type' => 'node',
                    'id' => 'fcce1b61-258e-4054-ae36-244d25a9e04c',
                  ],
                ],
              ],
            ],
          ],
        ],
        [
          'uuid' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
          'field_dummy' => [
            [
              'target_id' => 1,
            ],
            [
              'target_id' => 2,
            ],
          ],
        ],
      ],
      [
        [
          'data' => [
            'type' => 'lorem',
            'id' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
            'relationships' => [
              'field_dummy' => [
                'data' => [
                  [
                    'type' => 'node',
                    'id' => '76dd5c18-ea1b-4150-9e75-b21958a2b836',
                    'meta' => [
                      'foo' => 'bar',
                    ],
                  ],
                  [
                    'type' => 'node',
                    'id' => 'fcce1b61-258e-4054-ae36-244d25a9e04c',
                  ],
                ],
              ],
            ],
          ],
        ],
        [
          'uuid' => '535ba297-8d79-4fc1-b0d6-dc2f047765a1',
          'field_dummy' => [
            [
              'target_id' => 1,
              'foo' => 'bar',
            ],
            [
              'target_id' => 2,
            ],
          ],
        ],
      ],
    ];
  }
  
  /**
   * Ensures only valid UUIDs can be specified.
   *
   * @param string $id
   *   The input UUID. May be invalid.
   * @param bool $expect_exception
   *   Whether to expect an exception.
   *
   * @covers ::denormalize
   * @dataProvider denormalizeUuidProvider
   */
  public function testDenormalizeUuid($id, $expect_exception) : void {
    $data['data'] = isset($id) ? [
      'type' => 'node--article',
      'id' => $id,
    ] : [
      'type' => 'node--article',
    ];
    if ($expect_exception) {
      $this->expectException(UnprocessableEntityHttpException::class);
      $this->expectExceptionMessage('IDs should be properly generated and formatted UUIDs as described in RFC 4122.');
    }
    $denormalized = $this->normalizer
      ->denormalize($data, NULL, 'api_json', [
      'resource_type' => new ResourceType('node', 'article', FieldableEntityInterface::class),
    ]);
    if (isset($id)) {
      $this->assertSame($id, $denormalized['uuid']);
    }
    else {
      $this->assertArrayNotHasKey('uuid', $denormalized);
    }
  }
  
  /**
   * Provides test cases for testDenormalizeUuid.
   */
  public static function denormalizeUuidProvider() {
    return [
      'valid' => [
        '76dd5c18-ea1b-4150-9e75-b21958a2b836',
        FALSE,
      ],
      'missing' => [
        NULL,
        FALSE,
      ],
      'invalid_empty' => [
        '',
        TRUE,
      ],
      'invalid_alpha' => [
        'invalid',
        TRUE,
      ],
      'invalid_numeric' => [
        1234,
        TRUE,
      ],
      'invalid_alphanumeric' => [
        'abc123',
        TRUE,
      ],
    ];
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
JsonApiDocumentTopLevelNormalizerTest::$normalizer protected property The normalizer under test.
JsonApiDocumentTopLevelNormalizerTest::denormalizeProvider public static function Data provider for the denormalize test.
JsonApiDocumentTopLevelNormalizerTest::denormalizeUuidProvider public static function Provides test cases for testDenormalizeUuid.
JsonApiDocumentTopLevelNormalizerTest::setUp protected function Overrides UnitTestCase::setUp
JsonApiDocumentTopLevelNormalizerTest::testDenormalize public function @covers ::denormalize[[api-linebreak]]
@dataProvider denormalizeProvider
JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid public function Ensures only valid UUIDs can be specified.
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.
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.