diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php index 95b5858..a85617f 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityNormalizer.php @@ -136,6 +136,8 @@ protected function getEntityUri($entity) { * * @return array * The typed data IDs. + * + * @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException */ protected function getTypedDataIds($types) { // The 'type' can potentially contain an array of type objects. By default, diff --git a/core/modules/hal/lib/Drupal/hal/Tests/DenormalizeTest.php b/core/modules/hal/lib/Drupal/hal/Tests/DenormalizeTest.php index c4164f8..e4674ef 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/DenormalizeTest.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/DenormalizeTest.php @@ -34,7 +34,7 @@ public function testTypeHandling() { ), ), ); - $denormalized = $this->container->get('serializer')->denormalize($data_with_valid_type, $this->entityClass, $this->format); + $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. @@ -50,7 +50,7 @@ public function testTypeHandling() { ), ), ); - $denormalized = $this->container->get('serializer')->denormalize($data_with_multiple_types, $this->entityClass, $this->format); + $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. @@ -62,8 +62,8 @@ public function testTypeHandling() { ), ); try { - $this->container->get('serializer')->denormalize($data_with_invalid_type, $this->entityClass, $this->format); - $this->fail('Exception thrown when type is invalid.'); + $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.'); @@ -75,8 +75,8 @@ public function testTypeHandling() { ), ); try { - $this->container->get('serializer')->denormalize($data_with_no_type, $this->entityClass, $this->format); - $this->fail('Exception thrown when no type is provided.'); + $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.'); @@ -94,7 +94,7 @@ public function testMarkFieldForDeletion() { ), ), ); - $no_field_denormalized = $this->container->get('serializer')->denormalize($no_field_data, $this->entityClass, $this->format); + $no_field_denormalized = $this->serializer->denormalize($no_field_data, $this->entityClass, $this->format); $no_field_value = $no_field_denormalized->field_test_text->getValue(); $empty_field_data = array( @@ -105,7 +105,7 @@ public function testMarkFieldForDeletion() { ), 'field_test_text' => array(), ); - $empty_field_denormalized = $this->container->get('serializer')->denormalize($empty_field_data, $this->entityClass, $this->format); + $empty_field_denormalized = $this->serializer->denormalize($empty_field_data, $this->entityClass, $this->format); $empty_field_value = $empty_field_denormalized->field_test_text->getValue(); $this->assertTrue(!empty($no_field_value) && empty($empty_field_value), 'A field set to an empty array in the data is structured differently than an empty field.'); @@ -176,7 +176,7 @@ public function testBasicFieldDenormalization() { 'format' => 'full_html', ), ); - $denormalized = $this->container->get('serializer')->denormalize($data, $this->entityClass, $this->format); + $denormalized = $this->serializer->denormalize($data, $this->entityClass, $this->format); $this->assertEqual($data['uuid'], $denormalized->get('uuid')->getValue(), 'A preset value (e.g. UUID) is overridden by incoming data.'); $this->assertEqual($data['field_test_text'], $denormalized->get('field_test_text')->getValue(), 'A basic text field is denormalized.'); $this->assertEqual($expected_value_en, $denormalized->get('field_test_translatable_text')->getValue(), 'Values in the default language are properly handled for a translatable field.'); diff --git a/core/modules/hal/lib/Drupal/hal/Tests/NormalizeTest.php b/core/modules/hal/lib/Drupal/hal/Tests/NormalizeTest.php index 848b19a..eb577e5 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/NormalizeTest.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/NormalizeTest.php @@ -150,7 +150,7 @@ public function testNormalize() { ), ); - $normalized = $this->container->get('serializer')->normalize($entity, $this->format); + $normalized = $this->serializer->normalize($entity, $this->format); $this->assertEqual($normalized['_links']['self'], $expected_array['_links']['self'], 'self link placed correctly.'); // @todo Test curies. // @todo Test type. diff --git a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php index 9cf7019..cbab209 100644 --- a/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php +++ b/core/modules/hal/lib/Drupal/hal/Tests/NormalizerTestBase.php @@ -7,8 +7,18 @@ namespace Drupal\hal\Tests; +use Drupal\Core\Cache\MemoryBackend; use Drupal\Core\Language\Language; +use Drupal\hal\Encoder\JsonEncoder; +use Drupal\hal\Normalizer\EntityNormalizer; +use Drupal\hal\Normalizer\EntityReferenceItemNormalizer; +use Drupal\hal\Normalizer\FieldItemNormalizer; +use Drupal\hal\Normalizer\FieldNormalizer; +use Drupal\rest\LinkManager\LinkManager; +use Drupal\rest\LinkManager\RelationLinkManager; +use Drupal\rest\LinkManager\TypeLinkManager; use Drupal\simpletest\DrupalUnitTestBase; +use Symfony\Component\Serializer\Serializer; /** * Test the HAL normalizer. @@ -23,6 +33,13 @@ public static $modules = array('entity_test', 'entity_reference', 'field', 'field_sql_storage', 'hal', 'language', 'rest', 'serialization', 'system', 'text', 'user'); /** + * The mock serializer. + * + * @var \Symfony\Component\Serializer\Serializer + */ + protected $serializer; + + /** * The format being tested. * * @var string @@ -104,6 +121,22 @@ function setUp() { 'bundle' => 'entity_test', ); field_create_instance($instance); + + // Set up the mock serializer. + $normalizers = array( + new EntityNormalizer(), + new EntityReferenceItemNormalizer(), + new FieldItemNormalizer(), + new FieldNormalizer(), + ); + $link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('cache')), new RelationLinkManager()); + foreach ($normalizers as $normalizer) { + $normalizer->setLinkManager($link_manager); + } + $encoders = array( + new JsonEncoder(), + ); + $this->serializer = new Serializer($normalizers, $encoders); } }