Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
test(refactor): refactored tests by removing dependency on serviceMan…
Browse files Browse the repository at this point in the history
…ager to
  • Loading branch information
sanakhandvsa committed Jan 30, 2024
1 parent 8505ed4 commit 4265f8b
Showing 1 changed file with 50 additions and 186 deletions.
236 changes: 50 additions & 186 deletions test/Olcs/src/Controller/SessionTimeoutControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,181 +1,120 @@
<?php
declare(strict_types = 1);

declare(strict_types=1);

namespace OlcsTest\Controller;

use Common\Controller\Plugin\Redirect;
use Common\Rbac\JWTIdentityProvider;
use Common\Rbac\User;
use Common\Test\MocksServicesTrait;
use Laminas\ServiceManager\ServiceManager;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Interop\Container\Containerinterface;
use Laminas\Http\Request;
use Laminas\Http\Response;
use Laminas\Mvc\Controller\PluginManager;
use Laminas\Mvc\MvcEvent;
use Laminas\Router\Http\TreeRouteStack;
use Laminas\Router\RouteMatch;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\Stdlib\Parameters;
use Laminas\Uri\Http;
use Laminas\View\Model\ViewModel;
use Mockery as m;
use Mockery\MockInterface;
use Olcs\Controller\SessionTimeoutController;
use Olcs\Controller\SessionTimeoutControllerFactory;
use LmcRbacMvc\Identity\IdentityProviderInterface;
use PHPUnit\Framework\TestCase;

/**
* @see SessionTimeoutController
*/
class SessionTimeoutControllerTest extends MockeryTestCase
class SessionTimeoutControllerTest extends TestCase
{
use MocksServicesTrait;

protected const COOKIE_NAME = 'cookie';

private $identityProviderClass = JWTIdentityProvider::class;

/**
* @var ServiceManager
*/
private $serviceManager;

/**
* @return ServiceManager
*/
protected function serviceManager(): ServiceManager
{
assert(null !== $this->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);
}

/**
* @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);
}

/**
* @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);
Expand All @@ -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);
}

/**
Expand All @@ -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;
}
}

0 comments on commit 4265f8b

Please sign in to comment.