You are here

class JsonApiDocumentTopLevelNormalizerTest in JSON:API 8

Same name in this branch
  1. 8 tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest
  2. 8 tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest
Same name and namespace in other branches
  1. 8.2 tests/src/Unit/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Unit\Normalizer\JsonApiDocumentTopLevelNormalizerTest

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

@internal

Hierarchy

Expanded class hierarchy of JsonApiDocumentTopLevelNormalizerTest

File

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}
   */
  public function setUp() {
    $link_manager = $this
      ->prophesize(LinkManager::class);
    $resource_type_repository = $this
      ->prophesize(ResourceTypeRepository::class);
    $field_resolver = $this
      ->prophesize(FieldResolver::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($link_manager
      ->reveal(), $entity_type_manager
      ->reveal(), $resource_type_repository
      ->reveal(), $field_resolver
      ->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) {
    $context = [
      'resource_type' => new ResourceType($this
        ->randomMachineName(), $this
        ->randomMachineName(), FieldableEntityInterface::class),
    ];
    $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 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) {
    $data['data'] = isset($id) ? [
      'type' => 'node--article',
      'id' => $id,
    ] : [
      'type' => 'node--article',
    ];
    if ($expect_exception) {
      $this
        ->setExpectedException(UnprocessableEntityHttpException::class, '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($this
        ->randomMachineName(), $this
        ->randomMachineName(), FieldableEntityInterface::class),
    ]);
    if (isset($id)) {
      $this
        ->assertSame($id, $denormalized['uuid']);
    }
    else {
      $this
        ->assertArrayNotHasKey('uuid', $denormalized);
    }
  }

  /**
   * Provides test cases for testDenormalizeUuid.
   */
  public 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

Namesort descending Modifiers Type Description Overrides
JsonApiDocumentTopLevelNormalizerTest::$normalizer protected property The normalizer under test.
JsonApiDocumentTopLevelNormalizerTest::denormalizeProvider public function Data provider for the denormalize test.
JsonApiDocumentTopLevelNormalizerTest::denormalizeUuidProvider public function Provides test cases for testDenormalizeUuid.
JsonApiDocumentTopLevelNormalizerTest::setUp public function Overrides UnitTestCase::setUp
JsonApiDocumentTopLevelNormalizerTest::testDenormalize public function @covers ::denormalize @dataProvider denormalizeProvider
JsonApiDocumentTopLevelNormalizerTest::testDenormalizeUuid public function Ensures only valid UUIDs can be specified.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 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::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.