From 4265f8bb2ceaf9448d6d62d6ab45faa94ea94f91 Mon Sep 17 00:00:00 2001 From: sanaullahkhan Date: Tue, 30 Jan 2024 13:46:43 +0000 Subject: [PATCH] test(refactor): refactored tests by removing dependency on serviceManager to --- .../SessionTimeoutControllerTest.php | 236 ++++-------------- 1 file changed, 50 insertions(+), 186 deletions(-) diff --git a/test/Olcs/src/Controller/SessionTimeoutControllerTest.php b/test/Olcs/src/Controller/SessionTimeoutControllerTest.php index d91ba294a..369520341 100644 --- a/test/Olcs/src/Controller/SessionTimeoutControllerTest.php +++ b/test/Olcs/src/Controller/SessionTimeoutControllerTest.php @@ -1,111 +1,58 @@ serviceManager, 'Expected service manager to be set. Hint: You may need to call `setUpServiceManager` before trying to get a service manager'); - return $this->serviceManager; - } + private IdentityProviderInterface $identityProviderMock; + protected Redirect $redirectHelperMock; + protected SessionTimeoutController $sut; /** * @test */ public function indexAction_IsCallable() { - // Setup - $serviceLocator = $this->setUpServiceManager(); - - $sut = $this->setUpSut($serviceLocator, new Request()); - // Assert - $this->assertTrue(method_exists($sut, 'indexAction') && is_callable([$sut, 'indexAction'])); - } - - /** - * @return ServiceManager - */ - protected function setUpServiceManager(): ServiceManager - { - $this->serviceManager = new ServiceManager(); - $this->serviceManager->setAllowOverride(true); - $services = $this->setUpDefaultServices($this->serviceManager); - - // Maintain support for deprecated way of registering services via an array of services. Instead, services - // should be registered by calling the available setter methods on the ServiceManager instance. - if (is_array($services)) { - foreach ($services as $serviceName => $service) { - $this->serviceManager->setService($serviceName, $service); - } - } - - // Set controller plugin manager to the main service manager so that all services can be resolved from the one - // service manager instance. - $this->serviceManager->setService('ControllerPluginManager', $this->serviceManager); - - return $this->serviceManager; + $this->assertTrue(method_exists($this->sut, 'indexAction') && is_callable([$this->sut, 'indexAction'])); } /** * @test - * @depends indexAction_IsCallable */ public function indexAction_ReturnsViewModelIfIdentityIsAnonymous() { - // Setup - $serviceLocator = $this->setUpServiceLocator(); - $sut = $this->setUpSut($serviceLocator, new Request()); - // Define Expectations - $identity = $this->setUpMockService(User::class); + $identity = m::mock(User::class); $identity->shouldReceive('isAnonymous')->andReturnTrue(); - $currentUser = $this->resolveMockService($serviceLocator, IdentityProviderInterface::class); - $currentUser->shouldReceive('getIdentity')->withNoArgs()->andReturn($identity); + $this->identityProviderMock->shouldReceive('getIdentity')->withNoArgs()->andReturn($identity); // Execute - $result = $sut->indexAction($this->setUpRequest()); + $result = $this->sut->indexAction($this->setUpRequest()); // Assert $this->assertInstanceOf(ViewModel::class, $result); @@ -113,41 +60,37 @@ public function indexAction_ReturnsViewModelIfIdentityIsAnonymous() /** * @test - * @depends indexAction_ReturnsViewModelIfIdentityIsAnonymous */ public function indexAction_ReturnsViewModelIfIdentityIsNull() { - // Setup - $serviceLocator = $this->setUpServiceLocator(); - $sut = $this->setUpSut($serviceLocator, new Request()); - // Define Expectations - $currentUser = $this->resolveMockService($serviceLocator, IdentityProviderInterface::class); - $currentUser->shouldReceive('getIdentity')->withNoArgs()->andReturnNull()->once(); + $this->identityProviderMock->shouldReceive('getIdentity')->withNoArgs()->andReturnNull()->once(); // Execute - $result = $sut->indexAction($this->setUpRequest()); + $result = $this->sut->indexAction($this->setUpRequest()); // Assert $this->assertInstanceOf(ViewModel::class, $result); } - /** * @test - * @depends indexAction_ReturnsViewModelIfIdentityIsNull */ public function indexAction_LogsOutUserIfLoggedIn() { - // Setup - $serviceLocator = $this->setUpServiceLocator(); + //setup $request = $this->setUpRequest(); - $sut = $this->setUpSut($serviceLocator, new Request()); - $this->setUpIdentityWithClearSession($this->identityProviderClass); + //Define Expectation + $this->redirectHelperMock->shouldReceive('refresh') + ->withNoArgs() + ->andReturn($expectedResponse = new Response()) + ->once(); + + $this->setUpIdentityWithClearSession(); // Execute - $response = $sut->indexAction($request); + $response = $this->sut->indexAction($request); // Assert $this->assertInstanceOf(Response::class, $response); @@ -155,27 +98,23 @@ public function indexAction_LogsOutUserIfLoggedIn() /** * @test - * @depends indexAction_LogsOutUserIfLoggedIn * @dataProvider dpIdentityProviderClass */ public function indexAction_RedirectsUserIfLoggedIn(string $identityProviderClass) { // Setup - $serviceLocator = $this->setUpServiceLocator(); $request = $this->setUpRequest(); - $sut = $this->setUpSut($serviceLocator, new Request()); - $this->setUpIdentityWithClearSession($identityProviderClass); + $this->setUpIdentityWithClearSession(); // Define Expectations - $redirectHelper = $this->resolveMockService($serviceLocator, Redirect::class); - $redirectHelper->shouldReceive('refresh') + $this->redirectHelperMock->shouldReceive('refresh') ->withNoArgs() ->andReturn($expectedResponse = new Response()) ->once(); // Execute - $response = $sut->indexAction($request); + $response = $this->sut->indexAction($request); // Assert $this->assertSame($expectedResponse, $response); @@ -189,63 +128,38 @@ public function dpIdentityProviderClass(): array ]; } - /** - * " - * @param ServiceLocatorInterface $serviceLocator - * @return array - */ - protected function setUpDefaultServices(ServiceLocatorInterface $serviceLocator): array + protected function setup(): void { - return [ - IdentityProviderInterface::class => $this->setUpIdentity($this->identityProviderClass), - Redirect::class => $this->setUpRedirect(), - 'request' => $this->setUpMockService(Request::class), - ]; - } - - /** - * @param Request $request - * @param RouteMatch $routeMatch - * @return MvcEvent - */ - protected function setUpMvcEvent(Request $request, RouteMatch $routeMatch): MvcEvent - { - $event = new MvcEvent(); - $event->setRequest($request); - $event->setRouteMatch($routeMatch); - $router = $this->setUpMockService(TreeRouteStack::class); - $event->setRouter($router); - return $event; - } + $this->identityProviderMock = m::mock(IdentityProviderInterface::class); + $this->redirectHelperMock = m::mock(Redirect::class); - /** - * @param ServiceLocatorInterface $serviceLocator - * @return PluginManager - */ - protected function setUpPluginManager(ServiceLocatorInterface $serviceLocator): PluginManager - { - $pluginManager = new PluginManager($serviceLocator); - return $pluginManager; + $this->sut = new SessionTimeoutController( + $this->identityProviderMock, + $this->redirectHelperMock + ); } /** * @param ServiceLocatorInterface $serviceLocator * @param Request $request * @param RouteMatch|null $routeMatch - * @return SessionTimeoutController */ - protected function setUpSut(ServiceLocatorInterface $serviceLocator, Request $request): SessionTimeoutController + protected function setUpSut() { - $routeMatch = new RouteMatch([]); - $factory = new SessionTimeoutControllerFactory(); - $instance = $factory->__invoke($serviceLocator, SessionTimeoutController::class); - $instance->setEvent($this->setUpMvcEvent($request, $routeMatch)); - $instance->setPluginManager($this->setUpPluginManager($serviceLocator)); + $container = m::mock(ContainerInterface::class); + $controllerPluginManagerMock = m::mock(PluginManager::class); + // Set expectations for the container's `get` method + $controllerPluginManagerMock->shouldReceive('get')->withArgs([ + 'ControllerPluginManager', + ])->andReturn($controllerPluginManagerMock); - // Dispatch a request so that the request gets set on the controller. - $instance->dispatch($request); + $container->shouldReceive('get')->withArgs([ + IdentityProviderInterface::class, + ])->andReturn($this->identityProviderMock); - return $instance->getDelegate(); + $controllerPluginManagerMock->shouldReceive('get')->withArgs([ + Redirect::class, + ])->andReturn($controllerPluginManagerMock); } /** @@ -266,67 +180,17 @@ protected function setUpRequest(?string $url = null, array $input = null) return $request; } - /** - * @param string $identityProvider - * @return m\MockInterface - */ - protected function setUpIdentity(string $identityProvider): m\MockInterface + protected function setUpIdentityWithClearSession(): void { - $identity = $this->setUpMockService(User::class); - $identity->shouldReceive('isAnonymous') - ->andReturnFalse() - ->byDefault(); - - $currentUser = $this->getMockServiceWithName($identityProvider, IdentityProviderInterface::class); - $currentUser->shouldReceive('getIdentity') - ->withNoArgs() - ->andReturn($identity) - ->byDefault(); - $currentUser->expects('clearSession') - ->never() - ->byDefault(); - - return $currentUser; - } - - protected function getMockServiceWithName(string $class, string $serviceName): MockInterface - { - if (!$this->serviceManager->has($serviceName)) { - $this->serviceManager->setService( - $serviceName, - $this->setUpMockService($class) - ); - } - - return $this->serviceManager->get($serviceName); - } - - protected function setUpIdentityWithClearSession(string $identityProvider): void - { - $identity = $this->setUpMockService(User::class); + $identity = m::mock(User::class); $identity->expects('isAnonymous') ->withNoArgs() ->andReturnFalse(); - $currentUser = $this->getMockServiceWithName($identityProvider, IdentityProviderInterface::class); - $currentUser->expects('getIdentity') + $this->identityProviderMock->expects('getIdentity') ->withNoArgs() ->andReturn($identity); - $currentUser->expects('clearSession') + $this->identityProviderMock->expects('clearSession') ->withNoArgs(); } - - /** - * @return m\MockInterface - */ - protected function setUpRedirect(): m\MockInterface - { - $redirect = $this->setUpMockService(Redirect::class); - $redirect->shouldReceive('refresh') - ->withNoArgs() - ->andReturn(new Response()) - ->byDefault(); - - return $redirect; - } }