Commit cfbe530a authored by catch's avatar catch

Issue #1904404 by damiankloip, linclark: Fixed Update EntitySerializationTest...

Issue #1904404 by damiankloip, linclark: Fixed Update EntitySerializationTest to reflect actual Serializer setup.
parent 2bfaff6b
...@@ -10,20 +10,20 @@ ...@@ -10,20 +10,20 @@
use Drupal\serialization\Encoder\JsonEncoder; use Drupal\serialization\Encoder\JsonEncoder;
use Drupal\serialization\Normalizer\ComplexDataNormalizer; use Drupal\serialization\Normalizer\ComplexDataNormalizer;
use Drupal\serialization\Normalizer\TypedDataNormalizer; use Drupal\serialization\Normalizer\TypedDataNormalizer;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\DrupalUnitTestBase;
use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Serializer;
/** /**
* Tests entity normalization and serialization of supported core formats. * Tests entity normalization and serialization of supported core formats.
*/ */
class EntitySerializationTest extends WebTestBase { class EntitySerializationTest extends DrupalUnitTestBase {
/** /**
* Modules to enable. * Modules to enable.
* *
* @var array * @var array
*/ */
public static $modules = array('entity_test', 'serialization'); public static $modules = array('serialization');
/** /**
* The test values. * The test values.
...@@ -39,6 +39,13 @@ class EntitySerializationTest extends WebTestBase { ...@@ -39,6 +39,13 @@ class EntitySerializationTest extends WebTestBase {
*/ */
protected $entity; protected $entity;
/**
* The serializer serivce.
*
* @var \Symfony\Component\Serializer\Serializer.
*/
protected $serializer;
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => 'Entity serialization tests', 'name' => 'Entity serialization tests',
...@@ -50,6 +57,8 @@ public static function getInfo() { ...@@ -50,6 +57,8 @@ public static function getInfo() {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->enableModules(array('system', 'field', 'field_sql_storage', 'text', 'entity_test'));
// Create a test entity to serialize. // Create a test entity to serialize.
$this->values = array( $this->values = array(
'name' => $this->randomName(), 'name' => $this->randomName(),
...@@ -61,17 +70,14 @@ protected function setUp() { ...@@ -61,17 +70,14 @@ protected function setUp() {
); );
$this->entity = entity_create('entity_test_mulrev', $this->values); $this->entity = entity_create('entity_test_mulrev', $this->values);
$this->entity->save(); $this->entity->save();
$this->serializer = $this->container->get('serializer');
} }
/** /**
* Test the normalize function. * Test the normalize function.
*/ */
public function testNormalize() { public function testNormalize() {
// Set up the serializer.
$encoders = array(new JsonEncoder());
$normalizers = array(new ComplexDataNormalizer(), new TypedDataNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$expected = array( $expected = array(
'id' => array( 'id' => array(
array('value' => 1), array('value' => 1),
...@@ -85,7 +91,9 @@ public function testNormalize() { ...@@ -85,7 +91,9 @@ public function testNormalize() {
'langcode' => array( 'langcode' => array(
array('value' => LANGUAGE_NOT_SPECIFIED), array('value' => LANGUAGE_NOT_SPECIFIED),
), ),
'default_langcode' => array(NULL), 'default_langcode' => array(
array('value' => NULL),
),
'name' => array( 'name' => array(
array('value' => $this->values['name']), array('value' => $this->values['name']),
), ),
...@@ -100,7 +108,7 @@ public function testNormalize() { ...@@ -100,7 +108,7 @@ public function testNormalize() {
), ),
); );
$normalized = $serializer->normalize($this->entity); $normalized = $this->serializer->normalize($this->entity);
foreach (array_keys($expected) as $fieldName) { foreach (array_keys($expected) as $fieldName) {
$this->assertEqual($expected[$fieldName], $normalized[$fieldName], "ComplexDataNormalizer produces expected array for $fieldName."); $this->assertEqual($expected[$fieldName], $normalized[$fieldName], "ComplexDataNormalizer produces expected array for $fieldName.");
...@@ -112,21 +120,20 @@ public function testNormalize() { ...@@ -112,21 +120,20 @@ public function testNormalize() {
* Test registered Serializer's entity serialization for core's formats. * Test registered Serializer's entity serialization for core's formats.
*/ */
public function testSerialize() { public function testSerialize() {
$serializer = drupal_container()->get('serializer');
// Test that Serializer responds using the ComplexDataNormalizer and // Test that Serializer responds using the ComplexDataNormalizer and
// JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested // JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested
// elsewhere, so we can just assume that it works properly here. // elsewhere, so we can just assume that it works properly here.
$normalized = $serializer->normalize($this->entity, 'json'); $normalized = $this->serializer->normalize($this->entity, 'json');
$expected = json_encode($normalized); $expected = json_encode($normalized);
// Test 'json'. // Test 'json'.
$actual = $serializer->serialize($this->entity, 'json'); $actual = $this->serializer->serialize($this->entity, 'json');
$this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "json" is requested.'); $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "json" is requested.');
$actual = $serializer->serialize($normalized, 'json'); $actual = $this->serializer->serialize($normalized, 'json');
$this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "json" is requested'); $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "json" is requested');
// Test 'ajax'. // Test 'ajax'.
$actual = $serializer->serialize($this->entity, 'ajax'); $actual = $this->serializer->serialize($this->entity, 'ajax');
$this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "ajax" is requested.'); $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "ajax" is requested.');
$actual = $serializer->serialize($normalized, 'ajax'); $actual = $this->serializer->serialize($normalized, 'ajax');
$this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "ajax" is requested'); $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "ajax" is requested');
// Generate the expected xml in a way that allows changes to entity property // Generate the expected xml in a way that allows changes to entity property
...@@ -149,9 +156,9 @@ public function testSerialize() { ...@@ -149,9 +156,9 @@ public function testSerialize() {
// Reduced the array to a string. // Reduced the array to a string.
$expected = implode('', $expected); $expected = implode('', $expected);
// Test 'xml'. The output should match that of Symfony's XmlEncoder. // Test 'xml'. The output should match that of Symfony's XmlEncoder.
$actual = $serializer->serialize($this->entity, 'xml'); $actual = $this->serializer->serialize($this->entity, 'xml');
$this->assertIdentical($actual, $expected); $this->assertIdentical($actual, $expected);
$actual = $serializer->serialize($normalized, 'xml'); $actual = $this->serializer->serialize($normalized, 'xml');
$this->assertIdentical($actual, $expected); $this->assertIdentical($actual, $expected);
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment