Commit a0290ec6 authored by alexpott's avatar alexpott

Issue #2284413 by damiankloip: Use addMethodCall() instead of...

Issue #2284413 by damiankloip: Use addMethodCall() instead of replaceArgument() in RegisterEntityResolversCompilerPass.
parent 2a806cc6
......@@ -34,7 +34,6 @@ services:
- { name: encoder, format: xml }
serializer.entity_resolver:
class: Drupal\serialization\EntityResolver\ChainEntityResolver
arguments: [{ }]
serializer.entity_resolver.uuid:
class: Drupal\serialization\EntityResolver\UuidResolver
tags:
......
......@@ -12,14 +12,14 @@
/**
* Resolver delegating the entity resolution to a chain of resolvers.
*/
class ChainEntityResolver implements EntityResolverInterface {
class ChainEntityResolver implements ChainEntityResolverInterface {
/**
* The concrete resolvers.
*
* @var \Drupal\serialization\EntityResolver\EntityResolverInterface[]
*/
protected $resolvers;
protected $resolvers = array();
/**
* Constructs a ChainEntityResolver object.
......@@ -31,6 +31,13 @@ public function __construct(array $resolvers = array()) {
$this->resolvers = $resolvers;
}
/**
* {@inheritdoc}
*/
public function addResolver(EntityResolverInterface $resolver) {
$this->resolvers[] = $resolver;
}
/**
* {@inheritdoc}
*/
......
<?php
/**
* @file
* Contains \Drupal\serialization\EntityResolver\ChainEntityResolverInterface
*/
namespace Drupal\serialization\EntityResolver;
/**
* An interface for delegating a entity resolution to a chain of resolvers.
*/
interface ChainEntityResolverInterface extends EntityResolverInterface {
/**
* Adds an entity resolver.
*
* @param \Drupal\serialization\EntityResolver\EntityResolverInterface $resolver
* The entity resolver to add.
*/
public function addResolver(EntityResolverInterface $resolver);
}
......@@ -24,6 +24,7 @@ class RegisterEntityResolversCompilerPass implements CompilerPassInterface {
*/
public function process(ContainerBuilder $container) {
$definition = $container->getDefinition('serializer.entity_resolver');
$resolvers = array();
// Retrieve registered Normalizers and Encoders from the container.
foreach ($container->findTaggedServiceIds('entity_resolver') as $id => $attributes) {
......@@ -32,8 +33,8 @@ public function process(ContainerBuilder $container) {
}
// Add the registered concrete EntityResolvers to the ChainEntityResolver.
if (!empty($resolvers)) {
$definition->replaceArgument(0, $this->sort($resolvers));
foreach ($this->sort($resolvers) as $resolver) {
$definition->addMethodCall('addResolver', array($resolver));
}
}
......
......@@ -47,6 +47,9 @@ public function setUp() {
/**
* Test the resolve method with no matching resolvers.
*
* @covers ::__construct
* @covers ::resolve
*/
public function testResolverWithNoneResolved() {
$resolvers = array(
......@@ -59,8 +62,25 @@ public function testResolverWithNoneResolved() {
$this->assertNull($resolver->resolve($this->testNormalizer, $this->testData, $this->testEntityType));
}
/**
* 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));
}
/**
* Test the resolve method with a matching resolver first.
*
* @covers ::__construct
* @covers ::resolve
*/
public function testResolverWithFirstResolved() {
$resolvers = array(
......@@ -75,6 +95,9 @@ public function testResolverWithFirstResolved() {
/**
* Test the resolve method with a matching resolver last.
*
* @covers ::__construct
* @covers ::resolve
*/
public function testResolverWithLastResolved() {
$resolvers = array(
......@@ -97,7 +120,7 @@ public function testResolverWithLastResolved() {
* Whether or not the resolve method is expected to be called.
*
* @return \Drupal\serialization\EntityResolver\EntityResolverInterface|\PHPUnit_Framework_MockObject_MockObject
* The mocked entity ressolver.
* The mocked entity resolver.
*/
protected function createEntityResolverMock($return = NULL, $called = TRUE) {
$mock = $this->getMock('Drupal\serialization\EntityResolver\EntityResolverInterface');
......
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