Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / ProxyBuilder / ProxyBuilderTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\ProxyBuilder\ProxyBuilderTest.
6  */
7
8 namespace Drupal\Tests\Core\ProxyBuilder;
9 use Drupal\Core\ProxyBuilder\ProxyBuilder;
10 use Drupal\Tests\UnitTestCase;
11
12 /**
13  * @coversDefaultClass \Drupal\Core\ProxyBuilder\ProxyBuilder
14  * @group proxy_builder
15  */
16 class ProxyBuilderTest extends UnitTestCase {
17
18   /**
19    * The tested proxy builder.
20    *
21    * @var \Drupal\Core\ProxyBuilder\ProxyBuilder
22    */
23   protected $proxyBuilder;
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30
31     $this->proxyBuilder = new ProxyBuilder();
32   }
33
34   /**
35    * @covers ::buildMethod
36    * @covers ::buildParameter
37    * @covers ::buildMethodBody
38    */
39   public function testBuildComplexMethod() {
40     $class = 'Drupal\Tests\Core\ProxyBuilder\TestServiceComplexMethod';
41
42     $result = $this->proxyBuilder->build($class);
43
44     // @todo Solve the silly linebreak for array()
45     $method_body = <<<'EOS'
46
47 /**
48  * {@inheritdoc}
49  */
50 public function complexMethod($parameter, callable $function, \Drupal\Tests\Core\ProxyBuilder\TestServiceNoMethod $test_service = NULL, array &$elements = array (
51 ))
52 {
53     return $this->lazyLoadItself()->complexMethod($parameter, $function, $test_service, $elements);
54 }
55
56 EOS;
57
58     $this->assertEquals($this->buildExpectedClass($class, $method_body), $result);
59   }
60
61   /**
62    * Constructs the expected class output.
63    *
64    * @param string $expected_methods_body
65    *   The expected body of decorated methods.
66    *
67    * @return string
68    *   The code of the entire proxy.
69    */
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'
75
76 namespace {{ namespace }} {
77
78     /**
79      * Provides a proxy class for \{{ class }}.
80      *
81      * @see \Drupal\Component\ProxyBuilder
82      */
83     class {{ proxy_class }}{{ interface_string }}
84     {
85
86         use \Drupal\Core\DependencyInjection\DependencySerializationTrait;
87
88         /**
89          * The id of the original proxied service.
90          *
91          * @var string
92          */
93         protected $drupalProxyOriginalServiceId;
94
95         /**
96          * The real proxied service, after it was lazy loaded.
97          *
98          * @var \{{ class }}
99          */
100         protected $service;
101
102         /**
103          * The service container.
104          *
105          * @var \Symfony\Component\DependencyInjection\ContainerInterface
106          */
107         protected $container;
108
109         /**
110          * Constructs a ProxyClass Drupal proxy object.
111          *
112          * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
113          *   The container.
114          * @param string $drupal_proxy_original_service_id
115          *   The service ID of the original service.
116          */
117         public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface $container, $drupal_proxy_original_service_id)
118         {
119             $this->container = $container;
120             $this->drupalProxyOriginalServiceId = $drupal_proxy_original_service_id;
121         }
122
123         /**
124          * Lazy loads the real service from the container.
125          *
126          * @return object
127          *   Returns the constructed real service.
128          */
129         protected function lazyLoadItself()
130         {
131             if (!isset($this->service)) {
132                 $this->service = $this->container->get($this->drupalProxyOriginalServiceId);
133             }
134
135             return $this->service;
136         }
137 {{ expected_methods_body }}
138     }
139
140 }
141
142 EOS;
143
144     $expected_methods_body = implode("\n", array_map(function ($value) {
145       if ($value === '') {
146         return $value;
147       }
148       return "        $value";
149     }, explode("\n", $expected_methods_body)));
150
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);
156
157     return $expected_string;
158   }
159
160 }
161
162 class TestServiceNoMethod {
163
164 }
165
166 class TestServiceComplexMethod {
167
168   public function complexMethod($parameter, callable $function, TestServiceNoMethod $test_service = NULL, array &$elements = []) {
169
170   }
171
172 }