|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Yaroslav Honcharuk <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Yarhon\RouteGuardBundle\Tests\Controller\ArgumentResolver; |
|
12
|
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use Psr\Container\ContainerInterface; |
|
15
|
|
|
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException as ContainerRuntimeException; |
|
17
|
|
|
use Yarhon\RouteGuardBundle\Controller\ArgumentResolver\ServiceValueResolver; |
|
18
|
|
|
use Yarhon\RouteGuardBundle\Controller\ArgumentResolver\ArgumentResolverContext; |
|
19
|
|
|
use Yarhon\RouteGuardBundle\Exception\RuntimeException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class ServiceValueResolverTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
private $container; |
|
27
|
|
|
|
|
28
|
|
|
private $context; |
|
29
|
|
|
|
|
30
|
|
|
private $argument; |
|
31
|
|
|
|
|
32
|
|
|
private $resolver; |
|
33
|
|
|
|
|
34
|
|
|
public function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
37
|
|
|
|
|
38
|
|
|
$this->context = $this->createMock(ArgumentResolverContext::class); |
|
39
|
|
|
|
|
40
|
|
|
$this->argument = $this->createMock(ArgumentMetadata::class); |
|
41
|
|
|
|
|
42
|
|
|
$this->argument->method('getName') |
|
|
|
|
|
|
43
|
|
|
->willReturn('arg'); |
|
44
|
|
|
|
|
45
|
|
|
$this->resolver = new ServiceValueResolver($this->container); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @dataProvider supportsDataProvider |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testSupports($controllerName, $serviceLocator, $expected) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->context->method('getControllerName') |
|
54
|
|
|
->willReturn($controllerName); |
|
55
|
|
|
|
|
56
|
|
|
$this->container->method('has') |
|
57
|
|
|
->willReturn((bool) $serviceLocator); |
|
58
|
|
|
|
|
59
|
|
|
$this->container->method('get') |
|
60
|
|
|
->willReturn($serviceLocator); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertSame($expected, $this->resolver->supports($this->context, $this->argument)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function supportsDataProvider() |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
|
|
// no service locator |
|
69
|
|
|
[ |
|
70
|
|
|
'a::b', |
|
71
|
|
|
null, |
|
72
|
|
|
false, |
|
73
|
|
|
], |
|
74
|
|
|
// no service in service locator |
|
75
|
|
|
[ |
|
76
|
|
|
'a::b', |
|
77
|
|
|
$this->createServiceLocator(), |
|
78
|
|
|
false, |
|
79
|
|
|
], |
|
80
|
|
|
// has service in service locator |
|
81
|
|
|
[ |
|
82
|
|
|
'a::b', |
|
83
|
|
|
$this->createServiceLocator('arg'), |
|
84
|
|
|
true, |
|
85
|
|
|
], |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testSupportsTriesOldServiceNaming() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->context->method('getControllerName') |
|
92
|
|
|
->willReturn('a::b'); |
|
93
|
|
|
|
|
94
|
|
|
$this->container->expects($this->at(0)) |
|
95
|
|
|
->method('has') |
|
96
|
|
|
->with('a::b') |
|
97
|
|
|
->willReturn(false); |
|
98
|
|
|
|
|
99
|
|
|
$this->container->expects($this->at(1)) |
|
100
|
|
|
->method('has') |
|
101
|
|
|
->with('a:b') |
|
102
|
|
|
->willReturn(true); |
|
103
|
|
|
|
|
104
|
|
|
$this->container->expects($this->once()) |
|
105
|
|
|
->method('get') |
|
106
|
|
|
->with('a:b'); |
|
107
|
|
|
|
|
108
|
|
|
$this->resolver->supports($this->context, $this->argument); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testSupportsTrimsLeadingBackslashes() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->context->method('getControllerName') |
|
114
|
|
|
->willReturn('\\a::b'); |
|
115
|
|
|
|
|
116
|
|
|
$this->container->expects($this->at(0)) |
|
117
|
|
|
->method('has') |
|
118
|
|
|
->with('a::b'); |
|
119
|
|
|
|
|
120
|
|
|
$this->resolver->supports($this->context, $this->argument); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testResolve() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->context->method('getControllerName') |
|
126
|
|
|
->willReturn('a::b'); |
|
127
|
|
|
|
|
128
|
|
|
$value = new \stdClass(); |
|
129
|
|
|
|
|
130
|
|
|
$serviceLocator = $this->createServiceLocator('arg', $value); |
|
131
|
|
|
|
|
132
|
|
|
$this->container->method('has') |
|
133
|
|
|
->with('a::b') |
|
134
|
|
|
->willReturn(true); |
|
135
|
|
|
|
|
136
|
|
|
$this->container->method('get') |
|
137
|
|
|
->with('a::b') |
|
138
|
|
|
->willReturn($serviceLocator); |
|
139
|
|
|
|
|
140
|
|
|
$this->assertSame($value, $this->resolver->resolve($this->context, $this->argument)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testResolveException() |
|
144
|
|
|
{ |
|
145
|
|
|
$this->context->method('getControllerName') |
|
146
|
|
|
->willReturn('a::b'); |
|
147
|
|
|
|
|
148
|
|
|
$serviceLocator = $this->createServiceLocator('arg'); |
|
149
|
|
|
|
|
150
|
|
|
$exception = new ContainerRuntimeException('original text'); |
|
151
|
|
|
|
|
152
|
|
|
$serviceLocator->method('get') |
|
153
|
|
|
->with('arg') |
|
154
|
|
|
->willThrowException($exception); |
|
155
|
|
|
|
|
156
|
|
|
$this->container->method('has') |
|
157
|
|
|
->with('a::b') |
|
158
|
|
|
->willReturn(true); |
|
159
|
|
|
|
|
160
|
|
|
$this->container->method('get') |
|
161
|
|
|
->with('a::b') |
|
162
|
|
|
->willReturn($serviceLocator); |
|
163
|
|
|
|
|
164
|
|
|
$this->expectException(RuntimeException::class); |
|
165
|
|
|
$this->expectExceptionMessage('Cannot resolve argument $arg of "a::b()": original text'); |
|
166
|
|
|
|
|
167
|
|
|
$this->resolver->resolve($this->context, $this->argument); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
private function createServiceLocator($serviceId = null, $serviceValue = null) |
|
171
|
|
|
{ |
|
172
|
|
|
$serviceLocator = $this->createMock(ContainerInterface::class); |
|
173
|
|
|
|
|
174
|
|
|
if (null !== $serviceId) { |
|
175
|
|
|
$serviceLocator->method('has') |
|
176
|
|
|
->with($serviceId) |
|
177
|
|
|
->willReturn(true); |
|
178
|
|
|
} else { |
|
179
|
|
|
$serviceLocator->method('has') |
|
180
|
|
|
->willReturn(false); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if (null !== $serviceValue) { |
|
184
|
|
|
$serviceLocator->method('get') |
|
185
|
|
|
->with($serviceId) |
|
186
|
|
|
->willReturn($serviceValue); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $serviceLocator; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.