Skip to content

Commit 2a5eeaa

Browse files
OskarStarkchalasr
authored andcommitted
Use createMock() instead of a getter
1 parent 6cde986 commit 2a5eeaa

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

Tests/EventListener/WebDebugToolbarListenerTest.php

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testHtmlRedirectionIsIntercepted($statusCode, $hasSession)
6565
{
6666
$response = new Response('Some content', $statusCode);
6767
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
68-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'html', $hasSession), HttpKernelInterface::MASTER_REQUEST, $response);
68+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(false, 'html', $hasSession), HttpKernelInterface::MASTER_REQUEST, $response);
6969

7070
$listener = new WebDebugToolbarListener($this->getTwigMock('Redirection'), true);
7171
$listener->onKernelResponse($event);
@@ -78,7 +78,7 @@ public function testNonHtmlRedirectionIsNotIntercepted()
7878
{
7979
$response = new Response('Some content', '301');
8080
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
81-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'json', true), HttpKernelInterface::MASTER_REQUEST, $response);
81+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(false, 'json', true), HttpKernelInterface::MASTER_REQUEST, $response);
8282

8383
$listener = new WebDebugToolbarListener($this->getTwigMock('Redirection'), true);
8484
$listener->onKernelResponse($event);
@@ -92,7 +92,7 @@ public function testToolbarIsInjected()
9292
$response = new Response('<html><head></head><body></body></html>');
9393
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
9494

95-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
95+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
9696

9797
$listener = new WebDebugToolbarListener($this->getTwigMock());
9898
$listener->onKernelResponse($event);
@@ -108,7 +108,7 @@ public function testToolbarIsNotInjectedOnNonHtmlContentType()
108108
$response = new Response('<html><head></head><body></body></html>');
109109
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
110110
$response->headers->set('Content-Type', 'text/xml');
111-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
111+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
112112

113113
$listener = new WebDebugToolbarListener($this->getTwigMock());
114114
$listener->onKernelResponse($event);
@@ -124,7 +124,7 @@ public function testToolbarIsNotInjectedOnContentDispositionAttachment()
124124
$response = new Response('<html><head></head><body></body></html>');
125125
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
126126
$response->headers->set('Content-Disposition', 'attachment; filename=test.html');
127-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'html'), HttpKernelInterface::MASTER_REQUEST, $response);
127+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(false, 'html'), HttpKernelInterface::MASTER_REQUEST, $response);
128128

129129
$listener = new WebDebugToolbarListener($this->getTwigMock());
130130
$listener->onKernelResponse($event);
@@ -140,7 +140,7 @@ public function testToolbarIsNotInjectedOnRedirection($statusCode, $hasSession)
140140
{
141141
$response = new Response('<html><head></head><body></body></html>', $statusCode);
142142
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
143-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'html', $hasSession), HttpKernelInterface::MASTER_REQUEST, $response);
143+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(false, 'html', $hasSession), HttpKernelInterface::MASTER_REQUEST, $response);
144144

145145
$listener = new WebDebugToolbarListener($this->getTwigMock());
146146
$listener->onKernelResponse($event);
@@ -165,7 +165,7 @@ public function testToolbarIsNotInjectedWhenThereIsNoNoXDebugTokenResponseHeader
165165
{
166166
$response = new Response('<html><head></head><body></body></html>');
167167

168-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
168+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
169169

170170
$listener = new WebDebugToolbarListener($this->getTwigMock());
171171
$listener->onKernelResponse($event);
@@ -181,7 +181,7 @@ public function testToolbarIsNotInjectedWhenOnSubRequest()
181181
$response = new Response('<html><head></head><body></body></html>');
182182
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
183183

184-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::SUB_REQUEST, $response);
184+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::SUB_REQUEST, $response);
185185

186186
$listener = new WebDebugToolbarListener($this->getTwigMock());
187187
$listener->onKernelResponse($event);
@@ -197,7 +197,7 @@ public function testToolbarIsNotInjectedOnIncompleteHtmlResponses()
197197
$response = new Response('<div>Some content</div>');
198198
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
199199

200-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
200+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
201201

202202
$listener = new WebDebugToolbarListener($this->getTwigMock());
203203
$listener->onKernelResponse($event);
@@ -213,7 +213,7 @@ public function testToolbarIsNotInjectedOnXmlHttpRequests()
213213
$response = new Response('<html><head></head><body></body></html>');
214214
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
215215

216-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(true), HttpKernelInterface::MASTER_REQUEST, $response);
216+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(true), HttpKernelInterface::MASTER_REQUEST, $response);
217217

218218
$listener = new WebDebugToolbarListener($this->getTwigMock());
219219
$listener->onKernelResponse($event);
@@ -229,7 +229,7 @@ public function testToolbarIsNotInjectedOnNonHtmlRequests()
229229
$response = new Response('<html><head></head><body></body></html>');
230230
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
231231

232-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(false, 'json'), HttpKernelInterface::MASTER_REQUEST, $response);
232+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(false, 'json'), HttpKernelInterface::MASTER_REQUEST, $response);
233233

234234
$listener = new WebDebugToolbarListener($this->getTwigMock());
235235
$listener->onKernelResponse($event);
@@ -242,15 +242,15 @@ public function testXDebugUrlHeader()
242242
$response = new Response();
243243
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
244244

245-
$urlGenerator = $this->getUrlGeneratorMock();
245+
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
246246
$urlGenerator
247247
->expects($this->once())
248248
->method('generate')
249249
->with('_profiler', ['token' => 'xxxxxxxx'], UrlGeneratorInterface::ABSOLUTE_URL)
250250
->willReturn('http://mydomain.com/_profiler/xxxxxxxx')
251251
;
252252

253-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
253+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
254254

255255
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, $urlGenerator);
256256
$listener->onKernelResponse($event);
@@ -263,15 +263,15 @@ public function testThrowingUrlGenerator()
263263
$response = new Response();
264264
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
265265

266-
$urlGenerator = $this->getUrlGeneratorMock();
266+
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
267267
$urlGenerator
268268
->expects($this->once())
269269
->method('generate')
270270
->with('_profiler', ['token' => 'xxxxxxxx'])
271271
->willThrowException(new \Exception('foo'))
272272
;
273273

274-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
274+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
275275

276276
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, $urlGenerator);
277277
$listener->onKernelResponse($event);
@@ -284,15 +284,15 @@ public function testThrowingErrorCleanup()
284284
$response = new Response();
285285
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
286286

287-
$urlGenerator = $this->getUrlGeneratorMock();
287+
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
288288
$urlGenerator
289289
->expects($this->once())
290290
->method('generate')
291291
->with('_profiler', ['token' => 'xxxxxxxx'])
292292
->willThrowException(new \Exception("This\nmultiline\r\ntabbed text should\tcome out\r on\n \ta single plain\r\nline"))
293293
;
294294

295-
$event = new ResponseEvent($this->getKernelMock(), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
295+
$event = new ResponseEvent($this->createMock(Kernel::class), $this->getRequestMock(), HttpKernelInterface::MASTER_REQUEST, $response);
296296

297297
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, $urlGenerator);
298298
$listener->onKernelResponse($event);
@@ -331,14 +331,4 @@ protected function getTwigMock($render = 'WDT')
331331

332332
return $templating;
333333
}
334-
335-
protected function getUrlGeneratorMock()
336-
{
337-
return $this->createMock(UrlGeneratorInterface::class);
338-
}
339-
340-
protected function getKernelMock()
341-
{
342-
return $this->createMock(Kernel::class);
343-
}
344334
}

Tests/Profiler/TemplateManagerTest.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
use Twig\Loader\SourceContextLoaderInterface;
2222

2323
/**
24-
* Test for TemplateManager class.
25-
*
2624
* @author Artur Wielogórski <wodor@wodor.net>
2725
*/
2826
class TemplateManagerTest extends TestCase
@@ -38,23 +36,23 @@ class TemplateManagerTest extends TestCase
3836
protected $profiler;
3937

4038
/**
41-
* @var \Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager
39+
* @var TemplateManager
4240
*/
4341
protected $templateManager;
4442

4543
protected function setUp(): void
4644
{
4745
parent::setUp();
4846

49-
$profiler = $this->mockProfiler();
47+
$this->profiler = $this->createMock(Profiler::class);
5048
$twigEnvironment = $this->mockTwigEnvironment();
5149
$templates = [
5250
'data_collector.foo' => ['foo', 'FooBundle:Collector:foo'],
5351
'data_collector.bar' => ['bar', 'FooBundle:Collector:bar'],
5452
'data_collector.baz' => ['baz', 'FooBundle:Collector:baz'],
5553
];
5654

57-
$this->templateManager = new TemplateManager($profiler, $twigEnvironment, $templates);
55+
$this->templateManager = new TemplateManager($this->profiler, $twigEnvironment, $templates);
5856
}
5957

6058
public function testGetNameOfInvalidTemplate()
@@ -98,11 +96,6 @@ public function profileHasCollectorCallback($panel)
9896
}
9997
}
10098

101-
protected function mockProfile()
102-
{
103-
return $this->createMock(Profile::class);
104-
}
105-
10699
protected function mockTwigEnvironment()
107100
{
108101
$this->twigEnvironment = $this->createMock(Environment::class);
@@ -121,13 +114,6 @@ protected function mockTwigEnvironment()
121114

122115
return $this->twigEnvironment;
123116
}
124-
125-
protected function mockProfiler()
126-
{
127-
$this->profiler = $this->createMock(Profiler::class);
128-
129-
return $this->profiler;
130-
}
131117
}
132118

133119
class ProfileDummy extends Profile

0 commit comments

Comments
 (0)