5 * Contains \Drupal\Tests\Core\ProxyBuilder\ProxyBuilderTest.
8 namespace Drupal\Tests\Core\ProxyBuilder;
9 use Drupal\Core\ProxyBuilder\ProxyBuilder;
10 use Drupal\Tests\UnitTestCase;
13 * @coversDefaultClass \Drupal\Core\ProxyBuilder\ProxyBuilder
14 * @group proxy_builder
16 class ProxyBuilderTest extends UnitTestCase {
19 * The tested proxy builder.
21 * @var \Drupal\Core\ProxyBuilder\ProxyBuilder
23 protected $proxyBuilder;
28 protected function setUp() {
31 $this->proxyBuilder = new ProxyBuilder();
35 * @covers ::buildMethod
36 * @covers ::buildParameter
37 * @covers ::buildMethodBody
39 public function testBuildComplexMethod() {
40 $class = 'Drupal\Tests\Core\ProxyBuilder\TestServiceComplexMethod';
42 $result = $this->proxyBuilder->build($class);
44 // @todo Solve the silly linebreak for array()
45 $method_body = <<<'EOS'
50 public function complexMethod($parameter, callable $function, \Drupal\Tests\Core\ProxyBuilder\TestServiceNoMethod $test_service = NULL, array &$elements = array (
53 return $this->lazyLoadItself()->complexMethod($parameter, $function, $test_service, $elements);
58 $this->assertEquals($this->buildExpectedClass($class, $method_body), $result);
62 * Constructs the expected class output.
64 * @param string $expected_methods_body
65 * The expected body of decorated methods.
68 * The code of the entire proxy.
70 protected function buildExpectedClass($class, $expected_methods_body, $interface_string = '') {
71 $reflection = new \ReflectionClass($class);
72 $namespace = ProxyBuilder::buildProxyNamespace($class);
73 $proxy_class = $reflection->getShortName();
74 $expected_string = <<<'EOS'
76 namespace {{ namespace }} {
79 * Provides a proxy class for \{{ class }}.
81 * @see \Drupal\Component\ProxyBuilder
83 class {{ proxy_class }}{{ interface_string }}
86 use \Drupal\Core\DependencyInjection\DependencySerializationTrait;
89 * The id of the original proxied service.
93 protected $drupalProxyOriginalServiceId;
96 * The real proxied service, after it was lazy loaded.
103 * The service container.
105 * @var \Symfony\Component\DependencyInjection\ContainerInterface
107 protected $container;
110 * Constructs a ProxyClass Drupal proxy object.
112 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
114 * @param string $drupal_proxy_original_service_id
115 * The service ID of the original service.
117 public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $drupal_proxy_original_service_id)
119 $this->container = $container;
120 $this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id;
124 * Lazy loads the real service from the container.
127 * Returns the constructed real service.
129 protected function lazyLoadItself()
131 if (!isset($this->service)) {
132 $this->service = $this->container->get($this->drupalProxyOriginalServiceId);
135 return $this->service;
137 {{ expected_methods_body }}
144 $expected_methods_body = implode("\n", array_map(function ($value) {
149 }, explode("\n", $expected_methods_body)));
151 $expected_string = str_replace('{{ proxy_class }}', $proxy_class, $expected_string);
152 $expected_string = str_replace('{{ namespace }}', $namespace, $expected_string);
153 $expected_string = str_replace('{{ class }}', $class, $expected_string);
154 $expected_string = str_replace('{{ expected_methods_body }}', $expected_methods_body, $expected_string);
155 $expected_string = str_replace('{{ interface_string }}', $interface_string, $expected_string);
157 return $expected_string;
162 class TestServiceNoMethod {
166 class TestServiceComplexMethod {
168 public function complexMethod($parameter, callable $function, TestServiceNoMethod $test_service = NULL, array &$elements = []) {