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