Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / http-kernel / Tests / Controller / ArgumentResolver / ServiceValueResolverTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ServiceLocator;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver;
18 use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
19
20 class ServiceValueResolverTest extends TestCase
21 {
22     public function testDoNotSupportWhenControllerDoNotExists()
23     {
24         $resolver = new ServiceValueResolver(new ServiceLocator(array()));
25         $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
26         $request = $this->requestWithAttributes(array('_controller' => 'my_controller'));
27
28         $this->assertFalse($resolver->supports($request, $argument));
29     }
30
31     public function testExistingController()
32     {
33         $resolver = new ServiceValueResolver(new ServiceLocator(array(
34             'App\\Controller\\Mine::method' => function () {
35                 return new ServiceLocator(array(
36                     'dummy' => function () {
37                         return new DummyService();
38                     },
39                 ));
40             },
41         )));
42
43         $request = $this->requestWithAttributes(array('_controller' => 'App\\Controller\\Mine::method'));
44         $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
45
46         $this->assertTrue($resolver->supports($request, $argument));
47         $this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
48     }
49
50     public function testExistingControllerWithATrailingBackSlash()
51     {
52         $resolver = new ServiceValueResolver(new ServiceLocator(array(
53             'App\\Controller\\Mine::method' => function () {
54                 return new ServiceLocator(array(
55                     'dummy' => function () {
56                         return new DummyService();
57                     },
58                 ));
59             },
60         )));
61
62         $request = $this->requestWithAttributes(array('_controller' => '\\App\\Controller\\Mine::method'));
63         $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
64
65         $this->assertTrue($resolver->supports($request, $argument));
66         $this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
67     }
68
69     public function testControllerNameIsAnArray()
70     {
71         $resolver = new ServiceValueResolver(new ServiceLocator(array(
72             'App\\Controller\\Mine::method' => function () {
73                 return new ServiceLocator(array(
74                     'dummy' => function () {
75                         return new DummyService();
76                     },
77                 ));
78             },
79         )));
80
81         $request = $this->requestWithAttributes(array('_controller' => array('App\\Controller\\Mine', 'method')));
82         $argument = new ArgumentMetadata('dummy', DummyService::class, false, false, null);
83
84         $this->assertTrue($resolver->supports($request, $argument));
85         $this->assertYieldEquals(array(new DummyService()), $resolver->resolve($request, $argument));
86     }
87
88     private function requestWithAttributes(array $attributes)
89     {
90         $request = Request::create('/');
91
92         foreach ($attributes as $name => $value) {
93             $request->attributes->set($name, $value);
94         }
95
96         return $request;
97     }
98
99     private function assertYieldEquals(array $expected, \Generator $generator)
100     {
101         $args = array();
102         foreach ($generator as $arg) {
103             $args[] = $arg;
104         }
105
106         $this->assertEquals($expected, $args);
107     }
108 }
109
110 class DummyService
111 {
112 }