ChainEntityResolverTest.php 4.19 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\Tests\serialization\Unit\EntityResolver\ChainEntityResolverTest.
6
7
 */

8
namespace Drupal\Tests\serialization\Unit\EntityResolver;
9
10
11
12
13

use Drupal\Tests\UnitTestCase;
use Drupal\serialization\EntityResolver\ChainEntityResolver;

/**
14
15
 * @coversDefaultClass \Drupal\serialization\EntityResolver\ChainEntityResolver
 * @group serialization
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 */
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}
   */
43
  protected function setUp() {
44
45
46
47
48
49
    $this->testNormalizer = $this->getMock('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
    $this->testData = new \stdClass();
  }

  /**
   * Test the resolve method with no matching resolvers.
50
51
52
   *
   * @covers ::__construct
   * @covers ::resolve
53
54
55
56
57
58
59
60
61
62
63
64
   */
  public function testResolverWithNoneResolved() {
    $resolvers = array(
      $this->createEntityResolverMock(),
      $this->createEntityResolverMock(),
    );

    $resolver = new ChainEntityResolver($resolvers);

    $this->assertNull($resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
  }

65
66
67
68
69
70
71
72
73
74
75
76
77
78
  /**
   * Test the resolve method with no matching resolvers, using addResolver.
   *
   * @covers ::addResolver
   * @covers ::resolve
   */
  public function testResolverWithNoneResolvedUsingAddResolver() {
    $resolver = new ChainEntityResolver();
    $resolver->addResolver($this->createEntityResolverMock());
    $resolver->addResolver($this->createEntityResolverMock());

    $this->assertNull($resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
  }

79
80
  /**
   * Test the resolve method with a matching resolver first.
81
82
83
   *
   * @covers ::__construct
   * @covers ::resolve
84
85
86
87
88
89
90
91
92
93
94
95
96
97
   */
  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.
98
99
100
   *
   * @covers ::__construct
   * @covers ::resolve
101
102
103
104
105
106
107
108
109
110
111
112
   */
  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));
  }

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  /**
   * Test the resolve method where one resolver returns 0.
   *
   * @covers ::__construct
   * @covers ::resolve
   */
  public function testResolverWithResolvedToZero() {
    $resolvers = array(
      $this->createEntityResolverMock(0),
      $this->createEntityResolverMock(NULL, FALSE),
    );

    $resolver = new ChainEntityResolver($resolvers);

    $this->assertSame(0, $resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
  }

130
131
132
133
134
135
136
137
138
139
  /**
   * 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
140
   *   The mocked entity resolver.
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
   */
  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;
  }

}