You are here

public function SerializationTest::testSerializerComponentRegistration in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/serialization/src/Tests/SerializationTest.php \Drupal\serialization\Tests\SerializationTest::testSerializerComponentRegistration()

Confirms that modules can register normalizers and encoders.

File

core/modules/serialization/src/Tests/SerializationTest.php, line 42
Contains \Drupal\serialization\Tests\SerializationTest.

Class

SerializationTest
Functional tests for serialization system.

Namespace

Drupal\serialization\Tests

Code

public function testSerializerComponentRegistration() {
  $object = new \stdClass();
  $format = 'serialization_test';
  $expected = 'Normalized by SerializationTestNormalizer, Encoded by SerializationTestEncoder';

  // Ensure the serialization invokes the expected normalizer and encoder.
  $this
    ->assertIdentical($this->serializer
    ->serialize($object, $format), $expected);

  // Ensure the serialization fails for an unsupported format.
  try {
    $this->serializer
      ->serialize($object, 'unsupported_format');
    $this
      ->fail('The serializer was expected to throw an exception for an unsupported format, but did not.');
  } catch (UnexpectedValueException $e) {
    $this
      ->pass('The serializer threw an exception for an unsupported format.');
  }
}