View source
<?php
namespace Drupal\Tests\serialization\Unit\EntityResolver;
use Drupal\Tests\UnitTestCase;
use Drupal\serialization\EntityResolver\ChainEntityResolver;
class ChainEntityResolverTest extends UnitTestCase {
protected $testNormalizer;
protected $testData;
protected $testEntityType = 'test_type';
protected function setUp() : void {
$this->testNormalizer = $this
->createMock('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface');
$this->testData = new \stdClass();
}
public function testResolverWithNoneResolved() {
$resolvers = [
$this
->createEntityResolverMock(),
$this
->createEntityResolverMock(),
];
$resolver = new ChainEntityResolver($resolvers);
$this
->assertNull($resolver
->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
}
public function testResolverWithNoneResolvedUsingAddResolver() {
$resolver = new ChainEntityResolver();
$resolver
->addResolver($this
->createEntityResolverMock());
$resolver
->addResolver($this
->createEntityResolverMock());
$this
->assertNull($resolver
->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
}
public function testResolverWithFirstResolved() {
$resolvers = [
$this
->createEntityResolverMock(10),
$this
->createEntityResolverMock(NULL, FALSE),
];
$resolver = new ChainEntityResolver($resolvers);
$this
->assertSame(10, $resolver
->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
}
public function testResolverWithLastResolved() {
$resolvers = [
$this
->createEntityResolverMock(),
$this
->createEntityResolverMock(10),
];
$resolver = new ChainEntityResolver($resolvers);
$this
->assertSame(10, $resolver
->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
}
public function testResolverWithResolvedToZero() {
$resolvers = [
$this
->createEntityResolverMock(0),
$this
->createEntityResolverMock(NULL, FALSE),
];
$resolver = new ChainEntityResolver($resolvers);
$this
->assertSame(0, $resolver
->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
}
protected function createEntityResolverMock($return = NULL, $called = TRUE) {
$mock = $this
->createMock('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;
}
}