From ab0bb61f1477979fde915d2d6a5c4b5ca64938aa Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Tue, 7 Jan 2014 23:48:53 -0800
Subject: [PATCH] Issue #2159755 by damiankloip: Unit test the
 Drupal\serialization\EntityResolver\ChainEntityResolver class.

---
 .../EntityResolver/ChainEntityResolver.php    |   9 +-
 .../ChainEntityResolverTest.php               | 134 ++++++++++++++++++
 2 files changed, 138 insertions(+), 5 deletions(-)
 create mode 100644 core/modules/serialization/tests/Drupal/serialization/Tests/EntityResolver/ChainEntityResolverTest.php

diff --git a/core/modules/serialization/lib/Drupal/serialization/EntityResolver/ChainEntityResolver.php b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/ChainEntityResolver.php
index ea7c5d5e46f5..8127c7931e8a 100644
--- a/core/modules/serialization/lib/Drupal/serialization/EntityResolver/ChainEntityResolver.php
+++ b/core/modules/serialization/lib/Drupal/serialization/EntityResolver/ChainEntityResolver.php
@@ -17,14 +17,14 @@ class ChainEntityResolver implements EntityResolverInterface {
   /**
    * The concrete resolvers.
    *
-   * @var array
+   * @var \Drupal\serialization\EntityResolver\EntityResolverInterface[]
    */
   protected $resolvers;
 
   /**
-   * Constructor.
+   * Constructs a ChainEntityResolver object.
    *
-   * @param array $resolvers
+   * @param \Drupal\serialization\EntityResolver\EntityResolverInterface[] $resolvers
    *   The array of concrete resolvers.
    */
   public function __construct(array $resolvers = array()) {
@@ -32,7 +32,7 @@ public function __construct(array $resolvers = array()) {
   }
 
   /**
-   * Implements \Drupal\serialization\EntityResolver\EntityResolverInterface::resolve().
+   * {@inheritdoc}
    */
   public function resolve(NormalizerInterface $normalizer, $data, $entity_type) {
     foreach ($this->resolvers as $resolver) {
@@ -40,7 +40,6 @@ public function resolve(NormalizerInterface $normalizer, $data, $entity_type) {
         return $resolved;
       }
     }
-    return NULL;
   }
 
 }
diff --git a/core/modules/serialization/tests/Drupal/serialization/Tests/EntityResolver/ChainEntityResolverTest.php b/core/modules/serialization/tests/Drupal/serialization/Tests/EntityResolver/ChainEntityResolverTest.php
new file mode 100644
index 000000000000..5deda2f4d236
--- /dev/null
+++ b/core/modules/serialization/tests/Drupal/serialization/Tests/EntityResolver/ChainEntityResolverTest.php
@@ -0,0 +1,134 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\serialization\Tests\EntityResolver\ChainEntityResolverTest.
+ */
+
+namespace Drupal\serialization\Tests\EntityResolver;
+
+use Drupal\Tests\UnitTestCase;
+use Drupal\serialization\EntityResolver\ChainEntityResolver;
+
+/**
+ * Tests the ChainEntityResolver class.
+ *
+ * @see \Drupal\serialization\EntityResolver\ChainEntityResolver
+ *
+ * @group Drupal
+ * @group Serialization
+ */
+class ChainEntityResolverTest extends UnitTestCase {
+
+  /**
+   * A mocked normalizer.
+   *
+   * @var \Symfony\Component\Serializer\Normalizer\NormalizerInterface|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $testNormalizer;
+
+  /**
+   * Test data passed to the resolve method.
+   *
+   * @var \stdClass
+   */
+  protected $testData;
+
+  /**
+   * A test entity type.
+   *
+   * @var string
+   */
+  protected $testEntityType = 'test_type';
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'ChainEntityResolver',
+      'description' => 'Tests the Drupal\serialization\EntityResolver\ChainEntityResolver class.',
+      'group' => 'Serialization',
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setUp() {
+    $this->testNormalizer = $this->getMock('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
+    $this->testData = new \stdClass();
+  }
+
+  /**
+   * Test the resolve method with no matching resolvers.
+   */
+  public function testResolverWithNoneResolved() {
+    $resolvers = array(
+      $this->createEntityResolverMock(),
+      $this->createEntityResolverMock(),
+    );
+
+    $resolver = new ChainEntityResolver($resolvers);
+
+    $this->assertNull($resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
+  }
+
+  /**
+   * Test the resolve method with a matching resolver first.
+   */
+  public function testResolverWithFirstResolved() {
+    $resolvers = array(
+      $this->createEntityResolverMock(10),
+      $this->createEntityResolverMock(NULL, FALSE),
+    );
+
+    $resolver = new ChainEntityResolver($resolvers);
+
+    $this->assertSame(10, $resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
+  }
+
+  /**
+   * Test the resolve method with a matching resolver last.
+   */
+  public function testResolverWithLastResolved() {
+    $resolvers = array(
+      $this->createEntityResolverMock(),
+      $this->createEntityResolverMock(10),
+    );
+
+    $resolver = new ChainEntityResolver($resolvers);
+
+    $this->assertSame(10, $resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
+  }
+
+  /**
+   * Creates a mock entity resolver.
+   *
+   * @param null|int $return
+   *   Whether the mocked resolve method should return TRUE or FALSE.
+   *
+   * @param bool $called
+   *   Whether or not the resolve method is expected to be called.
+   *
+   * @return \Drupal\serialization\EntityResolver\EntityResolverInterface|\PHPUnit_Framework_MockObject_MockObject
+   *   The mocked entity ressolver.
+   */
+  protected function createEntityResolverMock($return = NULL, $called = TRUE) {
+    $mock = $this->getMock('Drupal\serialization\EntityResolver\EntityResolverInterface');
+
+    if ($called) {
+      $mock->expects($this->once())
+        ->method('resolve')
+        ->with($this->testNormalizer, $this->testData, $this->testEntityType)
+        ->will($this->returnValue($return));
+    }
+    else {
+      $mock->expects($this->never())
+        ->method('resolve');
+    }
+
+    return $mock;
+  }
+
+}
-- 
GitLab