You are here

public function DenormalizeTest::testTypeHandling in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/hal/src/Tests/DenormalizeTest.php \Drupal\hal\Tests\DenormalizeTest::testTypeHandling()

Tests that the type link relation in incoming data is handled correctly.

File

core/modules/hal/src/Tests/DenormalizeTest.php, line 24
Contains \Drupal\hal\Tests\DenormalizeTest.

Class

DenormalizeTest
Tests that entities can be denormalized from HAL.

Namespace

Drupal\hal\Tests

Code

public function testTypeHandling() {

  // Valid type.
  $data_with_valid_type = array(
    '_links' => array(
      'type' => array(
        'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array(
          'absolute' => TRUE,
        ))
          ->toString(),
      ),
    ),
  );
  $denormalized = $this->serializer
    ->denormalize($data_with_valid_type, $this->entityClass, $this->format);
  $this
    ->assertEqual(get_class($denormalized), $this->entityClass, 'Request with valid type results in creation of correct bundle.');

  // Multiple types.
  $data_with_multiple_types = array(
    '_links' => array(
      'type' => array(
        array(
          'href' => Url::fromUri('base:rest/types/foo', array(
            'absolute' => TRUE,
          ))
            ->toString(),
        ),
        array(
          'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array(
            'absolute' => TRUE,
          ))
            ->toString(),
        ),
      ),
    ),
  );
  $denormalized = $this->serializer
    ->denormalize($data_with_multiple_types, $this->entityClass, $this->format);
  $this
    ->assertEqual(get_class($denormalized), $this->entityClass, 'Request with multiple types results in creation of correct bundle.');

  // Invalid type.
  $data_with_invalid_type = array(
    '_links' => array(
      'type' => array(
        'href' => Url::fromUri('base:rest/types/foo', array(
          'absolute' => TRUE,
        ))
          ->toString(),
      ),
    ),
  );
  try {
    $this->serializer
      ->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
    $this
      ->fail('Exception should be thrown when type is invalid.');
  } catch (UnexpectedValueException $e) {
    $this
      ->pass('Exception thrown when type is invalid.');
  }

  // No type.
  $data_with_no_type = array(
    '_links' => array(),
  );
  try {
    $this->serializer
      ->denormalize($data_with_no_type, $this->entityClass, $this->format);
    $this
      ->fail('Exception should be thrown when no type is provided.');
  } catch (UnexpectedValueException $e) {
    $this
      ->pass('Exception thrown when no type is provided.');
  }
}