1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
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 WBW\Bundle\CoreBundle\Tests; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
20
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
22
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionBagInterface; |
24
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
25
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
26
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
27
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
28
|
|
|
use Symfony\Component\Routing\RouterInterface; |
29
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
30
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
31
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
|
|
|
|
32
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
33
|
|
|
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface; |
34
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
35
|
|
|
use Twig\Environment; |
36
|
|
|
use Twig\Loader\LoaderInterface; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Abstract test case. |
40
|
|
|
* |
41
|
|
|
* @author webeweb <https://github.com/webeweb> |
42
|
|
|
* @package WBW\Bundle\CoreBundle\Tests |
43
|
|
|
* @abstract |
44
|
|
|
*/ |
45
|
|
|
abstract class AbstractTestCase extends TestCase { |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Container builder. |
49
|
|
|
* |
50
|
|
|
* @var ContainerBuilder |
51
|
|
|
*/ |
52
|
|
|
protected $containerBuilder; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* CSRF token manager. |
56
|
|
|
* |
57
|
|
|
* @var CsrfTokenManagerInterface |
58
|
|
|
*/ |
59
|
|
|
protected $csrfTokenManager; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Encoder factory. |
63
|
|
|
* |
64
|
|
|
* @var EncoderFactoryInterface |
65
|
|
|
*/ |
66
|
|
|
protected $encoderFactory; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Entity manager. |
70
|
|
|
* |
71
|
|
|
* @var EntityManagerInterface; |
72
|
|
|
*/ |
73
|
|
|
protected $entityManager; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Event dispatcher. |
77
|
|
|
* |
78
|
|
|
* @var EventDispatcherInterface |
79
|
|
|
*/ |
80
|
|
|
protected $eventDispatcher; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Flash bag. |
84
|
|
|
* |
85
|
|
|
* @var FlashBagInterface |
86
|
|
|
*/ |
87
|
|
|
protected $flashBag; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Form factory. |
91
|
|
|
* |
92
|
|
|
* @var FormFactoryInterface |
93
|
|
|
*/ |
94
|
|
|
protected $formFactory; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Kernel. |
98
|
|
|
* |
99
|
|
|
* @var KernelInterface |
100
|
|
|
*/ |
101
|
|
|
protected $kernel; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Logger. |
105
|
|
|
* |
106
|
|
|
* @var LoggerInterface |
107
|
|
|
*/ |
108
|
|
|
protected $logger; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Mailer. |
112
|
|
|
* |
113
|
|
|
* @var MailerInterface |
114
|
|
|
*/ |
115
|
|
|
protected $mailer; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Request stack. |
119
|
|
|
* |
120
|
|
|
* @var RequestStack |
121
|
|
|
*/ |
122
|
|
|
protected $requestStack; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Router. |
126
|
|
|
* |
127
|
|
|
* @var RouterInterface |
128
|
|
|
*/ |
129
|
|
|
protected $router; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Session. |
133
|
|
|
* |
134
|
|
|
* @var SessionInterface |
135
|
|
|
*/ |
136
|
|
|
protected $session; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Session bag. |
140
|
|
|
* |
141
|
|
|
* @var SessionBagInterface |
142
|
|
|
*/ |
143
|
|
|
protected $sessionBag; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Token |
147
|
|
|
* |
148
|
|
|
* @var TokenInterface |
149
|
|
|
*/ |
150
|
|
|
protected $token; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Token storage. |
154
|
|
|
* |
155
|
|
|
* @var TokenStorageInterface |
156
|
|
|
*/ |
157
|
|
|
protected $tokenStorage; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Translator. |
161
|
|
|
* |
162
|
|
|
* @var TranslatorInterface |
163
|
|
|
*/ |
164
|
|
|
protected $translator; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Twig environment. |
168
|
|
|
* |
169
|
|
|
* @var Environment |
170
|
|
|
*/ |
171
|
|
|
protected $twigEnvironment; |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Twig globals. |
175
|
|
|
* |
176
|
|
|
* @var array |
177
|
|
|
*/ |
178
|
|
|
private $twigGlobals = []; |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Twig loader. |
182
|
|
|
* |
183
|
|
|
* @var LoaderInterface |
184
|
|
|
*/ |
185
|
|
|
protected $twigLoader; |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* User. |
189
|
|
|
* |
190
|
|
|
* @var UserInterface |
191
|
|
|
*/ |
192
|
|
|
protected $user; |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the dispatch() method for an EventDispatcher. |
196
|
|
|
* |
197
|
|
|
* @return callable Returns the dispatch() method for an EventDispatcher. |
198
|
|
|
*/ |
199
|
|
|
public static function getEventDispatcherDispatchFunction(): callable { |
200
|
|
|
return TestCaseHelper::getEventDispatcherDispatchFunction(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritDoc} |
205
|
|
|
*/ |
206
|
|
|
protected function setUp(): void { |
207
|
|
|
parent::setUp(); |
208
|
|
|
|
209
|
|
|
// Set a CSRF token manager. |
210
|
|
|
$this->csrfTokenManager = $this->getMockBuilder(CsrfTokenManagerInterface::class)->getMock(); |
|
|
|
|
211
|
|
|
|
212
|
|
|
// Set an Encoder factory mock. |
213
|
|
|
$this->encoderFactory = $this->getMockBuilder(EncoderFactoryInterface::class)->getMock(); |
|
|
|
|
214
|
|
|
|
215
|
|
|
// Set an Entity manager mock. |
216
|
|
|
$this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->getMock(); |
|
|
|
|
217
|
|
|
|
218
|
|
|
// Set an Event dispatcher mock. |
219
|
|
|
$this->eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); |
|
|
|
|
220
|
|
|
|
221
|
|
|
// Set a Flash bag mock. |
222
|
|
|
$this->flashBag = $this->getMockBuilder(FlashBagInterface::class)->getMock(); |
|
|
|
|
223
|
|
|
|
224
|
|
|
// Set a Form factory mock. |
225
|
|
|
$this->formFactory = $this->getMockBuilder(FormFactoryInterface::class)->getMock(); |
|
|
|
|
226
|
|
|
|
227
|
|
|
// Set a Kernel mock. |
228
|
|
|
$this->kernel = $this->getMockBuilder(KernelInterface::class)->getMock(); |
|
|
|
|
229
|
|
|
|
230
|
|
|
// Set a Logger mock. |
231
|
|
|
$this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock(); |
|
|
|
|
232
|
|
|
|
233
|
|
|
// Set a Mailer mock. |
234
|
|
|
$this->mailer = $this->getMockBuilder(MailerInterface::class)->getMock(); |
|
|
|
|
235
|
|
|
|
236
|
|
|
// Set a Request stack. |
237
|
|
|
$this->requestStack = $this->getMockBuilder(RequestStack::class)->disableOriginalConstructor()->getMock(); |
|
|
|
|
238
|
|
|
|
239
|
|
|
// Set a Router mock. |
240
|
|
|
$this->router = $this->getMockBuilder(RouterInterface::class)->getMock(); |
|
|
|
|
241
|
|
|
|
242
|
|
|
// Set a Session mock. |
243
|
|
|
$this->session = $this->getMockBuilder(SessionInterface::class)->getMock(); |
|
|
|
|
244
|
|
|
|
245
|
|
|
// TODO: Remove when dropping support for Symfony 5.2 |
246
|
|
|
if (50300 <= Kernel::VERSION_ID) { |
247
|
|
|
$this->requestStack->expects($this->any())->method("getSession")->willReturn($this->session); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// Set a Session bag mock. |
251
|
|
|
$this->sessionBag = $this->getMockBuilder(SessionBagInterface::class)->getMock(); |
|
|
|
|
252
|
|
|
|
253
|
|
|
// Set a trans() callback. |
254
|
|
|
$trans = TestCaseHelper::getTranslatorTransFunction(); |
255
|
|
|
|
256
|
|
|
// Set a Translator mock. |
257
|
|
|
$this->translator = $this->getMockBuilder(TranslatorInterface::class)->getMock(); |
|
|
|
|
258
|
|
|
$this->translator->expects($this->any())->method("trans")->willReturnCallback($trans); |
259
|
|
|
|
260
|
|
|
// Set getUser() callback. |
261
|
|
|
$getUser = function(): ?UserInterface { |
262
|
|
|
return $this->user; |
263
|
|
|
}; |
264
|
|
|
|
265
|
|
|
// Set a Token mock. |
266
|
|
|
$this->token = $this->getMockBuilder(TokenInterface::class)->getMock(); |
|
|
|
|
267
|
|
|
$this->token->expects($this->any())->method("getUser")->willReturnCallback($getUser); |
268
|
|
|
|
269
|
|
|
// Set a Token storage mock. |
270
|
|
|
$this->tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock(); |
|
|
|
|
271
|
|
|
$this->tokenStorage->expects($this->any())->method("getToken")->willReturn($this->token); |
272
|
|
|
|
273
|
|
|
// Set a Twig loader mock. |
274
|
|
|
$this->twigLoader = $this->getMockBuilder(LoaderInterface::class)->getMock(); |
|
|
|
|
275
|
|
|
|
276
|
|
|
// Set addGlobal() callback. |
277
|
|
|
$addGlobal = function(string $name, $value): void { |
278
|
|
|
$this->twigGlobals[$name] = $value; |
279
|
|
|
}; |
280
|
|
|
|
281
|
|
|
// Set getGlobals() callback. |
282
|
|
|
$getGlobals = function(): array { |
283
|
|
|
return $this->twigGlobals; |
284
|
|
|
}; |
285
|
|
|
|
286
|
|
|
// Set a Twig environment mock. |
287
|
|
|
$this->twigEnvironment = $this->getMockBuilder(Environment::class)->setConstructorArgs([$this->twigLoader, []])->getMock(); |
|
|
|
|
288
|
|
|
$this->twigEnvironment->expects($this->any())->method("addGlobal")->willReturnCallback($addGlobal); |
289
|
|
|
$this->twigEnvironment->expects($this->any())->method("getGlobals")->willReturnCallback($getGlobals); |
290
|
|
|
|
291
|
|
|
// Set a Parameter bag mock. |
292
|
|
|
$parameterBag = new ParameterBag([ |
293
|
|
|
"kernel.environment" => "test", |
294
|
|
|
"kernel.project_dir" => realpath(__DIR__ . "/Fixtures/app"), |
295
|
|
|
]); |
296
|
|
|
|
297
|
|
|
// Set a Container builder with only the necessary. |
298
|
|
|
$this->containerBuilder = new ContainerBuilder($parameterBag); |
299
|
|
|
$this->containerBuilder->set("doctrine.orm.entity_manager", $this->entityManager); |
300
|
|
|
$this->containerBuilder->set("event_dispatcher", $this->eventDispatcher); |
301
|
|
|
$this->containerBuilder->set("form.factory", $this->formFactory); |
302
|
|
|
$this->containerBuilder->set("kernel", $this->kernel); |
303
|
|
|
$this->containerBuilder->set("logger", $this->logger); |
304
|
|
|
$this->containerBuilder->set("mailer", $this->mailer); |
305
|
|
|
$this->containerBuilder->set("router", $this->router); |
306
|
|
|
$this->containerBuilder->set("request_stack", $this->requestStack); |
307
|
|
|
$this->containerBuilder->set("session", $this->session); |
308
|
|
|
$this->containerBuilder->set("security.csrf.token_manager", $this->csrfTokenManager); |
309
|
|
|
$this->containerBuilder->set("security.encoder_factory", $this->encoderFactory); |
310
|
|
|
$this->containerBuilder->set("security.token_storage", $this->tokenStorage); |
311
|
|
|
$this->containerBuilder->set("swiftmailer.mailer", $this->mailer); |
312
|
|
|
$this->containerBuilder->set("translator", $this->translator); |
313
|
|
|
$this->containerBuilder->set("twig", $this->twigEnvironment); |
314
|
|
|
|
315
|
|
|
$this->containerBuilder->set("Psr\\Container\\ContainerInterface", $this->containerBuilder); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths