From f09bd17ba198c1eefbf0fddcf161ed4c981018e6 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sat, 10 Jul 2021 10:24:26 +0200 Subject: [PATCH] clean up remaining event mocks --- .../Tests/Templating/TimedPhpEngineTest.php | 26 +++------- .../GuardAuthenticationListenerTest.php | 10 +--- .../Tests/Firewall/RememberMeListenerTest.php | 50 ++++--------------- .../Security/Http/Tests/FirewallTest.php | 17 ++----- .../Stopwatch/Tests/StopwatchTest.php | 7 +-- 5 files changed, 21 insertions(+), 89 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TimedPhpEngineTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TimedPhpEngineTest.php index 72e3fb0f78..4951b46c45 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TimedPhpEngineTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TimedPhpEngineTest.php @@ -16,7 +16,6 @@ use Symfony\Bundle\FrameworkBundle\Templating\TimedPhpEngine; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\Stopwatch\Stopwatch; -use Symfony\Component\Stopwatch\StopwatchEvent; use Symfony\Component\Templating\Loader\Loader; use Symfony\Component\Templating\Storage\StringStorage; use Symfony\Component\Templating\TemplateNameParserInterface; @@ -34,18 +33,15 @@ class TimedPhpEngineTest extends TestCase $globalVariables = $this->getGlobalVariables(); $loader = $this->getLoader($this->getStorage()); - $stopwatch = $this->getStopwatch(); - $stopwatchEvent = $this->getStopwatchEvent(); - - $stopwatch->expects($this->once()) - ->method('start') - ->with('template.php (index.php)', 'template') - ->willReturn($stopwatchEvent); - - $stopwatchEvent->expects($this->once())->method('stop'); + $stopwatch = new Stopwatch(); $engine = new TimedPhpEngine($templateNameParser, $container, $loader, $stopwatch, $globalVariables); $engine->render('index.php'); + + $sections = $stopwatch->getSections(); + + $this->assertCount(1, $sections); + $this->assertCount(1, reset($sections)->getEvents()); } private function getTemplateNameParser(): TemplateNameParserInterface @@ -83,14 +79,4 @@ class TimedPhpEngineTest extends TestCase return $loader; } - - private function getStopwatchEvent(): StopwatchEvent - { - return $this->createMock(StopwatchEvent::class); - } - - private function getStopwatch(): Stopwatch - { - return $this->createMock(Stopwatch::class); - } } diff --git a/src/Symfony/Component/Security/Guard/Tests/Firewall/GuardAuthenticationListenerTest.php b/src/Symfony/Component/Security/Guard/Tests/Firewall/GuardAuthenticationListenerTest.php index b4dc224544..1ef0e20b46 100644 --- a/src/Symfony/Component/Security/Guard/Tests/Firewall/GuardAuthenticationListenerTest.php +++ b/src/Symfony/Component/Security/Guard/Tests/Firewall/GuardAuthenticationListenerTest.php @@ -16,6 +16,7 @@ use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\RequestEvent; +use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager; use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\BadCredentialsException; @@ -322,14 +323,7 @@ class GuardAuthenticationListenerTest extends TestCase $this->guardAuthenticatorHandler = $this->createMock(GuardAuthenticatorHandler::class); $this->request = new Request([], [], [], [], [], []); - $this->event = $this->getMockBuilder(RequestEvent::class) - ->disableOriginalConstructor() - ->setMethods(['getRequest']) - ->getMock(); - $this->event - ->expects($this->any()) - ->method('getRequest') - ->willReturn($this->request); + $this->event = new RequestEvent($this->createMock(HttpKernelInterface::class), $this->request, HttpKernelInterface::MASTER_REQUEST); $this->logger = $this->createMock(LoggerInterface::class); $this->rememberMeServices = $this->createMock(RememberMeServicesInterface::class); diff --git a/src/Symfony/Component/Security/Http/Tests/Firewall/RememberMeListenerTest.php b/src/Symfony/Component/Security/Http/Tests/Firewall/RememberMeListenerTest.php index c6da58ce1f..81cf3ab353 100644 --- a/src/Symfony/Component/Security/Http/Tests/Firewall/RememberMeListenerTest.php +++ b/src/Symfony/Component/Security/Http/Tests/Firewall/RememberMeListenerTest.php @@ -45,7 +45,7 @@ class RememberMeListenerTest extends TestCase ->method('setToken') ; - $this->assertNull($listener($this->getGetResponseEvent())); + $this->assertNull($listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST))); } public function testOnCoreSecurityDoesNothingWhenNoCookieIsSet() @@ -64,9 +64,7 @@ class RememberMeListenerTest extends TestCase ->willReturn(null) ; - $event = $this->getGetResponseEvent(); - - $this->assertNull($listener($event)); + $this->assertNull($listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST))); } public function testOnCoreSecurityIgnoresAuthenticationExceptionThrownByAuthenticationManagerImplementation() @@ -99,9 +97,7 @@ class RememberMeListenerTest extends TestCase ->willThrowException($exception) ; - $event = $this->getGetResponseEvent($request); - - $listener($event); + $listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST)); } public function testOnCoreSecurityIgnoresAuthenticationOptionallyRethrowsExceptionThrownAuthenticationManagerImplementation() @@ -134,9 +130,7 @@ class RememberMeListenerTest extends TestCase ->willThrowException($exception) ; - $event = $this->getGetResponseEvent(); - - $listener($event); + $listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST)); } public function testOnCoreSecurityAuthenticationExceptionDuringAutoLoginTriggersLoginFail() @@ -166,9 +160,7 @@ class RememberMeListenerTest extends TestCase ->method('authenticate') ; - $event = $this->getGetResponseEvent(); - - $listener($event); + $listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST)); } public function testOnCoreSecurity() @@ -200,9 +192,7 @@ class RememberMeListenerTest extends TestCase ->willReturn($token) ; - $event = $this->getGetResponseEvent(); - - $listener($event); + $listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST)); } public function testSessionStrategy() @@ -244,15 +234,13 @@ class RememberMeListenerTest extends TestCase $request = new Request(); $request->setSession($session); - $event = $this->getGetResponseEvent($request); - $sessionStrategy ->expects($this->once()) ->method('onAuthentication') ->willReturn(null) ; - $listener($event); + $listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST)); } public function testSessionIsMigratedByDefault() @@ -298,9 +286,7 @@ class RememberMeListenerTest extends TestCase $request = new Request(); $request->setSession($session); - $event = $this->getGetResponseEvent($request); - - $listener($event); + $listener(new RequestEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST)); } public function testOnCoreSecurityInteractiveLoginEventIsDispatchedIfDispatcherIsPresent() @@ -332,8 +318,6 @@ class RememberMeListenerTest extends TestCase ->willReturn($token) ; - $event = $this->getGetResponseEvent(); - $dispatcher ->expects($this->once()) ->method('dispatch') @@ -343,23 +327,7 @@ class RememberMeListenerTest extends TestCase ) ; - $listener($event); - } - - protected function getGetResponseEvent(Request $request = null): RequestEvent - { - $request = $request ?? new Request(); - - $event = $this->getMockBuilder(RequestEvent::class) - ->setConstructorArgs([$this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MASTER_REQUEST]) - ->getMock(); - $event - ->expects($this->any()) - ->method('getRequest') - ->willReturn($request) - ; - - return $event; + $listener(new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST)); } protected function getListener($withDispatcher = false, $catchExceptions = true, $withSessionStrategy = false) diff --git a/src/Symfony/Component/Security/Http/Tests/FirewallTest.php b/src/Symfony/Component/Security/Http/Tests/FirewallTest.php index 9f14c6de2e..1db12f4551 100644 --- a/src/Symfony/Component/Security/Http/Tests/FirewallTest.php +++ b/src/Symfony/Component/Security/Http/Tests/FirewallTest.php @@ -14,6 +14,7 @@ namespace Symfony\Component\Security\Http\Tests; use PHPUnit\Framework\TestCase; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\Security\Http\Firewall; @@ -68,20 +69,8 @@ class FirewallTest extends TestCase ->willReturn([[$first, $second], null, null]) ; - $event = $this->getMockBuilder(RequestEvent::class) - ->setMethods(['hasResponse']) - ->setConstructorArgs([ - $this->createMock(HttpKernelInterface::class), - $this->createMock(Request::class), - HttpKernelInterface::MASTER_REQUEST, - ]) - ->getMock() - ; - $event - ->expects($this->once()) - ->method('hasResponse') - ->willReturn(true) - ; + $event = new RequestEvent($this->createMock(HttpKernelInterface::class), new Request(), HttpKernelInterface::MASTER_REQUEST); + $event->setResponse(new Response()); $firewall = new Firewall($map, $this->createMock(EventDispatcherInterface::class)); $firewall->onKernelRequest($event); diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php index 75d3bb5d5b..d0bc1b0a7c 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php @@ -71,12 +71,7 @@ class StopwatchTest extends TestCase $events = new \ReflectionProperty(Section::class, 'events'); $events->setAccessible(true); - $stopwatchMockEvent = $this->getMockBuilder(StopwatchEvent::class) - ->setConstructorArgs([microtime(true) * 1000]) - ->getMock() - ; - - $events->setValue(end($section), ['foo' => $stopwatchMockEvent]); + $events->setValue(end($section), ['foo' => new StopwatchEvent(microtime(true) * 1000)]); $this->assertFalse($stopwatch->isStarted('foo')); }