* @license MIT License */ class AuthorizationServiceDelegatorTest extends \PHPUnit\Framework\TestCase { use ProphecyTrait; public function testDelegatorFactory() { $authServiceClassName = 'LmcRbacMvc\Service\AuthorizationService'; $delegator = new AuthorizationServiceDelegatorFactory(); $serviceLocator = $this->prophesize(ServiceLocatorInterface::class); $serviceLocator->willImplement(ContainerInterface::class); $authorizationService = $this->getMockBuilder('LmcRbacMvc\Service\AuthorizationService') ->disableOriginalConstructor() ->getMock(); $callback = function () { return new AuthorizationAwareFake(); }; $serviceLocator->get($authServiceClassName)->willReturn($authorizationService)->shouldBeCalled(); $decoratedInstance = $delegator->createDelegatorWithName($serviceLocator->reveal(), 'name', 'requestedName', $callback); $this->assertEquals($authorizationService, $decoratedInstance->getAuthorizationService()); } public function testAuthorizationServiceIsNotInjectedWithoutDelegator() { $serviceManager = ServiceManagerFactory::getServiceManager(); $serviceManager->setAllowOverride(true); // $authorizationService = $this->getMock('LmcRbacMvc\Service\AuthorizationService', [], [], '', false); $authorizationService = $this->getMockBuilder('LmcRbacMvc\Service\AuthorizationService') ->disableOriginalConstructor() ->getMock(); $serviceManager->setService( 'LmcRbacMvc\Service\AuthorizationService', $authorizationService ); $serviceManager->setAllowOverride(false); $serviceManager->setInvokableClass( 'LmcRbacMvcTest\AuthorizationAware', 'LmcRbacMvcTest\Initializer\AuthorizationAwareFake' ); $decoratedInstance = $serviceManager->get('LmcRbacMvcTest\AuthorizationAware'); $this->assertNull($decoratedInstance->getAuthorizationService()); } public function testAuthorizationServiceIsInjectedWithDelegatorV3() { $serviceManager = ServiceManagerFactory::getServiceManager(); if (! method_exists($serviceManager, 'build')) { $this->markTestSkipped('this test is only for zend-servicemanager v3'); } $serviceManager->setAllowOverride(true); // $authorizationService = $this->getMock('LmcRbacMvc\Service\AuthorizationService', [], [], '', false); $authorizationService = $this->getMockBuilder('LmcRbacMvc\Service\AuthorizationService') ->disableOriginalConstructor() ->getMock(); $serviceManager->setService( 'LmcRbacMvc\Service\AuthorizationService', $authorizationService ); $serviceManager->setAllowOverride(false); $serviceManager->setInvokableClass( 'LmcRbacMvcTest\AuthorizationAware', 'LmcRbacMvcTest\Initializer\AuthorizationAwareFake' ); $serviceManager->addDelegator( 'LmcRbacMvcTest\Initializer\AuthorizationAwareFake', 'LmcRbacMvc\Factory\AuthorizationServiceDelegatorFactory' ); $decoratedInstance = $serviceManager->get('LmcRbacMvcTest\AuthorizationAware'); $this->assertEquals($authorizationService, $decoratedInstance->getAuthorizationService()); } public function testDelegatorThrowExceptionWhenBadInterface() { $serviceManager = ServiceManagerFactory::getServiceManager(); $serviceManager->setAllowOverride(true); // $authorizationService = $this->getMock('LmcRbacMvc\Service\AuthorizationService', [], [], '', false); $authorizationService = $this->getMockBuilder('LmcRbacMvc\Service\AuthorizationService') ->disableOriginalConstructor() ->getMock(); $serviceManager->setService( 'LmcRbacMvc\Service\AuthorizationService', $authorizationService ); $serviceManager->setAllowOverride(false); $serviceManager->setFactory( 'LmcRbacTest\AuthorizationAware', function () { return new \StdClass(); } ); $serviceManager->addDelegator( 'LmcRbacTest\AuthorizationAware', 'LmcRbacMvc\Factory\AuthorizationServiceDelegatorFactory' ); $thrown = false; try { $serviceManager->get('LmcRbacTest\AuthorizationAware'); } catch (\Exception $e) { $thrown = true; $this->assertStringEndsWith('The service LmcRbacTest\AuthorizationAware must implement AuthorizationServiceAwareInterface.', $e->getMessage()); if ($e->getPrevious()) { $this->assertInstanceOf('LmcRbacMvc\Exception\RuntimeException', $e->getPrevious()); } } $this->assertTrue($thrown); } }