10000 minor #42047 clean up remaining event mocks (xabbuh) · symfony/symfony@fa182e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa182e6

Browse files
minor #42047 clean up remaining event mocks (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- clean up remaining event mocks | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | Commits ------- f09bd17 clean up remaining event mocks
2 parents 120f3d6 + f09bd17 commit fa182e6

File tree

5 files changed

+21
-89
lines changed

5 files changed

+21
-89
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TimedPhpEngineTest.php

Lines changed: 6 additions & 20 deletions
$globalVariables = $this->getGlobalVariables();
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1717
use Symfony\Component\DependencyInjection\Container;
1818
use Symfony\Component\Stopwatch\Stopwatch;
19-
use Symfony\Component\Stopwatch\StopwatchEvent;
2019
use Symfony\Component\Templating\Loader\Loader;
2120
use Symfony\Component\Templating\Storage\StringStorage;
2221
use Symfony\Component\Templating\TemplateNameParserInterface;
@@ -34,18 +33,15 @@ public function testThatRenderLogsTime()
3433
3534
$loader = $this->getLoader($this->getStorage());
3635

37-
$stopwatch = $this->getStopwatch();
38-
$stopwatchEvent = $this->getStopwatchEvent();
39-
40-
$stopwatch->expects($this->once())
41-
->method('start')
42-
->with('template.php (index.php)', 'template')
43-
->willReturn($stopwatchEvent);
44-
45-
$stopwatchEvent->expects($this->once())->method('stop');
36+
$stopwatch = new Stopwatch();
4637

4738
$engine = new TimedPhpEngine($templateNameParser, $container, $loader, $stopwatch, $globalVariables);
4839
$engine->render('index.php');
40+
41+
$sections = $stopwatch->getSections();
42+
43+
$this->assertCount(1, $sections);
44+
$this->assertCount(1, reset($sections)->getEvents());
4945
}
5046

5147
private function getTemplateNameParser(): TemplateNameParserInterface
@@ -83,14 +79,4 @@ private function getLoader($storage): Loader
8379

8480
return $loader;
8581
}
86-
87-
private function getStopwatchEvent(): StopwatchEvent
88-
{
89-
return $this->createMock(StopwatchEvent::class);
90-
}
91-
92-
private function getStopwatch(): Stopwatch
93-
{
94-
return $this->createMock(Stopwatch::class);
95-
}
9682
}

src/Symfony/Component/Security/Guard/Tests/Firewall/GuardAuthenticationListenerTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpFoundation\Request;
1717
use Symfony\Component\HttpFoundation\Response;
1818
use Symfony\Component\HttpKernel\Event\RequestEvent;
19+
use Symfony\Component\HttpKernel\HttpKernelInterface;
1920
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
2021
use Symfony\Component\Security\Core\Exception\AuthenticationException;
2122
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
@@ -322,14 +323,7 @@ protected function setUp(): void
322323
$this->guardAuthenticatorHandler = $this->createMock(GuardAuthenticatorHandler::class);
323324
$this->request = new Request([], [], [], [], [], []);
324325

325-
$this->event = $this->getMockBuilder(RequestEvent::class)
326-
->disableOriginalConstructor()
327-
->setMethods(['getRequest'])
328-
->getMock();
329-
$this->event
330-
->expects($this->any())
331-
->method('getRequest')
332-
->willReturn($this->request);
326+
$this->event = new RequestEvent($this->createMock(HttpKernelInterface::class), $this->request, HttpKernelInterface::MASTER_REQUEST);
333327

334328
$this->logger = $this->createMock(LoggerInterface::class);
335329
$this->rememberMeServices = $this->createMock(RememberMeServicesInterface::class);

src/Symfony/Component/Security/Http/Tests/Firewall/RememberMeListenerTest.php

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testOnCoreSecurityDoesNotTryToPopulateNonEmptyTokenStorage()
4545
->method('setToken')
4646
;
4747

48-
$this->assertNull($listener($this->getGetResponseEvent()));
48+
$this->assertNull($listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST)));
4949
}
5050

5151
public function testOnCoreSecurityDoesNothingWhenNoCookieIsSet()
@@ -64,9 +64,7 @@ public function testOnCoreSecurityDoesNothingWhenNoCookieIsSet()
6464
->willReturn(null)
6565
;
6666

67-
$event = $this->getGetResponseEvent();
68-
69-
$this->assertNull($listener($event));
67+
$this->assertNull($listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST)));
7068
}
7169

7270
public function testOnCoreSecurityIgnoresAuthenticationExceptionThrownByAuthenticationManagerImplementation()
@@ -99,9 +97,7 @@ public function testOnCoreSecurityIgnoresAuthenticationExceptionThrownByAuthenti
9997
->willThrowException($exception)
10098
;
10199

102-
$event = $this->getGetResponseEvent($request);
103-
104-
$listener($event);
100+
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
105101
}
106102

107103
public function testOnCoreSecurityIgnoresAuthenticationOptionallyRethrowsExceptionThrownAuthenticationManagerImplementation()
@@ -134,9 +130,7 @@ public function testOnCoreSecurityIgnoresAuthenticationOptionallyRethrowsExcepti
134130
->willThrowException($exception)
135131
;
136132

137-
$event = $this->getGetResponseEvent();
138-
139-
$listener($event);
133+
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST));
140134
}
141135

142136
public function testOnCoreSecurityAuthenticationExceptionDuringAutoLoginTriggersLoginFail()
@@ -166,9 +160,7 @@ public function testOnCoreSecurityAuthenticationExceptionDuringAutoLoginTriggers
166160
->method('authenticate')
167161
;
168162

169-
$event = $this->getGetResponseEvent();
170-
171-
$listener($event);
163+
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST));
172164
}
173165

174166
public function testOnCoreSecurity()
@@ -200,9 +192,7 @@ public function testOnCoreSecurity()
200192
->willReturn($token)
201193
;
202194

203-
$event = $this->getGetResponseEvent();
204-
205-
$listener($event);
195+
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST));
206196
}
207197

208198
public function testSessionStrategy()
@@ -244,15 +234,13 @@ public function testSessionStrategy()
244234
$request = new Request();
245235
$request->setSession($session);
246236

247-
$event = $this->getGetResponseEvent($request);
248-
249237
$sessionStrategy
250238
->expects($this->once())
251239
->method('onAuthentication')
252240
->willReturn(null)
253241
;
254242

255-
$listener($event);
243+
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
256244
}
257245

258246
public function testSessionIsMigratedByDefault()
@@ -298,9 +286,7 @@ public function testSessionIsMigratedByDefault()
298286
$request = new Request();
299287
$request->setSession($session);
300288

301-
$event = $this->getGetResponseEvent($request);
302-
303-
$listener($event);
289+
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST));
304290
}
305291

306292
public function testOnCoreSecurityInteractiveLoginEventIsDispatchedIfDispatcherIsPresent()
@@ -332,8 +318,6 @@ public function testOnCoreSecurityInteractiveLoginEventIsDispatchedIfDispatcherI
332318
->willReturn($token)
333319
;
334320

335-
$event = $this->getGetResponseEvent();
336-
337321
$dispatcher
338322
->expects($this->once())
339323
->method('dispatch')
@@ -343,23 +327,7 @@ public function testOnCoreSecurityInteractiveLoginEventIsDispatchedIfDispatcherI
343327
)
344328
;
345329

346-
$listener($event);
347-
}
348-
349-
protected function getGetResponseEvent(Request $request = null): RequestEvent
350-
{
351-
$request = $request ?? new Request();
352-
353-
$event = $this->getMockBuilder(RequestEvent::class)
354-
->setConstructorArgs([$this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST])
355-
->getMock();
356-
$event
357-
->expects($this->any())
358-
->method('getRequest')
359-
->willReturn($request)
360-
;
361-
362-
return $event;
330+
$listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST));
363331
}
364332

365333
protected function getListener($withDispatcher = false, $catchExceptions = true, $withSessionStrategy = false)

src/Symfony/Component/Security/Http/Tests/FirewallTest.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1616
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpKernel\Event\RequestEvent;
1819
use Symfony\Component\HttpKernel\HttpKernelInterface;
1920
use Symfony\Component\Security\Http\Firewall;
@@ -68,20 +69,8 @@ public function testOnKernelRequestStopsWhenThereIsAResponse()
6869
->willReturn([[$first, $second], null, null])
6970
;
7071

71-
$event = $this->getMockBuilder(RequestEvent::class)
72-
->setMethods(['hasResponse'])
73-
->setConstructorArgs([
74-
$this->createMock(HttpKernelInterface::class),
75-
$this->createMock(Request::class),
76-
HttpKernelInterface::MASTER_REQUEST,
77-
])
78-
->getMock()
79-
;
80-
$event
81-
->expects($this->once())
82-
->method('hasResponse')
83-
->willReturn(true)
84-
;
72+
$event = new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST);
73+
$event->setResponse(new Response());
8574

8675
$firewall = new Firewall($map, $this->createMock(EventDispatcherInterface::class));
8776
$firewall->onKernelRequest($event);

src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ public function testIsNotStartedEvent()
7171
$events = new \ReflectionProperty(Section::class, 'events');
7272
$events->setAccessible(true);
7373

74-
$stopwatchMockEvent = $this->getMockBuilder(StopwatchEvent::class)
75-
->setConstructorArgs([microtime(true) * 1000])
76-
->getMock()
77-
;
78-
79-
$events->setValue(end($section), ['foo' => $stopwatchMockEvent]);
74+
$events->setValue(end($section), ['foo' => new StopwatchEvent(microtime(true) * 1000)]);
8075

8176
$this->assertFalse($stopwatch->isStarted('foo'));
8277
}

0 commit comments

Comments
 (0)
0