5 * Contains \Drupal\Tests\Core\ProxyBuilder\ProxyBuilderTest.
8 namespace Drupal\Tests\Core\ProxyBuilder;
10 use Drupal\Core\ProxyBuilder\ProxyBuilder;
11 use Drupal\Tests\UnitTestCase;
14 * @coversDefaultClass \Drupal\Core\ProxyBuilder\ProxyBuilder
15 * @group proxy_builder
17 class ProxyBuilderTest extends UnitTestCase {
20 * The tested proxy builder.
22 * @var \Drupal\Core\ProxyBuilder\ProxyBuilder
24 protected $proxyBuilder;
29 protected function setUp() {
32 $this->proxyBuilder = new ProxyBuilder();
36 * @covers ::buildMethod
37 * @covers ::buildParameter
38 * @covers ::buildMethodBody
40 public function testBuildComplexMethod() {
41 $class = 'Drupal\Tests\Core\ProxyBuilder\TestServiceComplexMethod';
43 $result = $this->proxyBuilder->build($class);
45 // @todo Solve the silly linebreak for array()
46 $method_body = <<<'EOS'
51 public function complexMethod($parameter, callable $function, \Drupal\Tests\Core\ProxyBuilder\TestServiceNoMethod $test_service = NULL, array &$elements = array (
54 return $this->lazyLoadItself()->complexMethod($parameter, $function, $test_service, $elements);
59 $this->assertEquals($this->buildExpectedClass($class, $method_body), $result);
63 * Constructs the expected class output.
65 * @param string $expected_methods_body
66 * The expected body of decorated methods.
69 * The code of the entire proxy.
71 protected function buildExpectedClass($class, $expected_methods_body, $interface_string = '') {
72 $reflection = new \ReflectionClass($class);
73 $namespace = ProxyBuilder::buildProxyNamespace($class);
74 $proxy_class = $reflection->getShortName();
75 $expected_string = <<<'EOS'
77 namespace {{ namespace }} {
80 * Provides a proxy class for \{{ class }}.
82 * @see \Drupal\Component\ProxyBuilder
84 class {{ proxy_class }}{{ interface_string }}
87 use \Drupal\Core\DependencyInjection\DependencySerializationTrait;
90 * The id of the original proxied service.
94 protected $drupalProxyOriginalServiceId;
97 * The real proxied service, after it was lazy loaded.
104 * The service container.
106 * @var \Symfony\Component\DependencyInjection\ContainerInterface
108 protected $container;
111 * Constructs a ProxyClass Drupal proxy object.
113 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
115 * @param string $drupal_proxy_original_service_id
116 * The service ID of the original service.
118 public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $drupal_proxy_original_service_id)
120 $this->container = $container;
121 $this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id;
125 * Lazy loads the real service from the container.
128 * Returns the constructed real service.
130 protected function lazyLoadItself()
132 if (!isset($this->service)) {
133 $this->service = $this->container->get($this->drupalProxyOriginalServiceId);
136 return $this->service;
138 {{ expected_methods_body }}
145 $expected_methods_body = implode("\n", array_map(function ($value) {
150 }, explode("\n", $expected_methods_body)));
152 $expected_string = str_replace('{{ proxy_class }}', $proxy_class, $expected_string);
153 $expected_string = str_replace('{{ namespace }}', $namespace, $expected_string);
154 $expected_string = str_replace('{{ class }}', $class, $expected_string);
155 $expected_string = str_replace('{{ expected_methods_body }}', $expected_methods_body, $expected_string);
156 $expected_string = str_replace('{{ interface_string }}', $interface_string, $expected_string);
158 return $expected_string;
163 class TestServiceNoMethod {
167 class TestServiceComplexMethod {
169 public function complexMethod($parameter, callable $function, TestServiceNoMethod $test_service = NULL, array &$elements = []) {