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

Commit

Permalink
test(refactor): Removing dependencies on Common ServiceManage
Browse files Browse the repository at this point in the history
  • Loading branch information
sanakhandvsa committed Jan 15, 2024
1 parent d039147 commit 60954e0
Showing 1 changed file with 45 additions and 65 deletions.
110 changes: 45 additions & 65 deletions test/Olcs/src/Controller/Auth/LoginControllerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
<?php

declare(strict_types=1);

namespace OlcsTest\Controller\Auth;

use Common\Auth\Adapter\CommandAdapter;
use Common\Auth\Service\AuthenticationServiceInterface;
use Common\Controller\Dispatcher;
use Common\Controller\Plugin\CurrentUser;
use Common\Controller\Plugin\Redirect;
use Common\Form\View\Helper\Form;
use Common\Service\Helper\FormHelperService;
use CommonTest\Common\Controller\Plugin\ControllerStub;
use Dvsa\Olcs\Auth\Service\Auth\CookieService;
use Interop\Container\ContainerInterface;
use Laminas\Mvc\Controller\Plugin\FlashMessenger;
use Laminas\Mvc\Controller\Plugin\Url;
use Laminas\ServiceManager\ServiceManager;
use Mockery\MockInterface;
use Olcs\Auth\Adapter\SelfserveCommandAdapter;
use Olcs\Controller\Auth\LoginController;
use Olcs\Controller\Auth\LoginControllerFactory;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Common\Test\MocksServicesTrait;
use Mockery as m;
use Olcs\Form\Model\Form\Auth\Login;

class LoginControllerFactoryTest extends MockeryTestCase
{
//use MocksServicesTrait;
use MocksServicesTrait;

/**
* @var LoginControllerFactory
*/
protected $sut;
private $serviceManager;

public function setUp(): void
{
$this->sut = m::mock(LoginControllerFactory::class)->makePartial();
$this->serviceManager = $this->createMockedServiceManager()
; $this->setUpDefaultServices($this->serviceManager);
$this->setUpServiceManager();
}

/**
Expand All @@ -64,13 +60,13 @@ public function createService_CallsInvoke()

// Expectations
$this->sut->expects('__invoke')->withArgs(function ($serviceManager, $requestedName) {
$this->assertSame($this->serviceManager, $serviceManager, 'Expected first argument to be the ServiceManager passed to createService');
$this->assertSame($this->serviceManager(), $serviceManager, 'Expected first argument to be the ServiceManager passed to createService');
$this->assertSame(Dispatcher::class, $requestedName, 'Expected requestedName to be NULL');
return true;
});

// Execute
$this->sut->createService($this->serviceManager);
$this->sut->createService($this->serviceManager());
}

/**
Expand All @@ -91,13 +87,40 @@ public function __invoke_IsCallable(): void
*/
public function __invoke_ReturnsAnInstanceOfDispatcherWithLoginController()
{
// Setup
$this->setUpSut();
//$this->setUpDefaultServices();
$mockServiceManager = $this->createMockedServiceManager();
// Mock the ContainerInterface
$mockedContainer = $this->createMock(ContainerInterface::class);

// Mock the AuthenticationServiceInterface
$mockedAuthService = $this->createMock(AuthenticationServiceInterface::class);

// Mock the ControllerPluginManager
$mockedControllerPluginManager = $this->createMock(\Laminas\Mvc\Controller\PluginManager::class);

$this->setupMockedControllerPluginManager($mockedControllerPluginManager);

$mockedCommandAdapter = $this->createMock(SelfserveCommandAdapter::class);

$mockedContainer->expects($this->once())
->method('get')
->willReturnMap([
[SelfserveCommandAdapter::class, $mockedCommandAdapter],
['ControllerPluginManager', $mockedControllerPluginManager],
[AuthenticationServiceInterface::class, $mockedAuthService],
[FormHelperService::class, $this->createMock(FormHelperService::class)]
]);

$mockedControllerPluginManager->expects($this->once())
->method('get')
->willReturnMap([
[CurrentUser::class, $this->createMock(CurrentUser::class)],
[FlashMessenger::class, $this->createMock(FlashMessenger::class)],
[Redirect::class, $this->createMock(Redirect::class)]
]);

$factor = new LoginControllerFactory();

// Execute
$result = $this->sut->__invoke($mockServiceManager, null);
$result = $factor->__invoke($mockedContainer, null);

// Assert
$this->assertInstanceOf(Dispatcher::class, $result);
Expand All @@ -124,56 +147,13 @@ protected function setUpDefaultServices(ServiceManager $serviceManager)
$serviceManager->setService(Url::class, $this->setUpMockService(Url::class));
}

private function createMockedServiceManager()
private function setupMockedControllerPluginManager($mockedControllerPluginManager)
{
$mockedServiceManager = $this->getMockBuilder(ServiceManager::class)
->disableOriginalConstructor()
->getMock();

// Set up Expectation for method calls
$mockedServiceManager->expects($this->any())
$mockedControllerPluginManager->expects($this->any())
->method('get')
->willReturnCallback([$this, 'getService']);

return $mockedServiceManager;
}
public function getService($serviceName)
{
// Return the mocked services based on the service name
switch ($serviceName) {
case SelfserveCommandAdapter::class:
return $this->setUpMockService(SelfserveCommandAdapter::class);
case AuthenticationServiceInterface::class:
return $this->setUpMockService(AuthenticationServiceInterface::class);
case 'Auth\CookieService':
return $this->setUpMockService(CookieService::class);
case CurrentUser::class:
return $this->setUpMockService(CurrentUser::class);
case FlashMessenger::class:
return $this->setUpMockService(FlashMessenger::class);
case FormHelperService::class:
return $this->setUpMockService(FormHelperService::class);
case Redirect::class:
return $this->setUpMockService(Redirect::class);
case Url::class:
return $this->setUpMockService(Url::class);
case CommandAdapter::class:
return $this->setUpMockService(CommandAdapter::class);

default:
return null;
}
}


/**
* @param string $class
* @return MockInterface
*/
protected function setUpMockService(string $class): MockInterface
{
$instance = m::mock($class);
$instance->shouldIgnoreMissing();
return $instance;
->willReturnMap([
[CurrentUser::class, $this->createMock(CurrentUser::class)],
[FlashMessenger::class, $this->createMock(FlashMessenger::class)]
]);
}
}

0 comments on commit 60954e0

Please sign in to comment.