diff --git a/config/module.config.php b/config/module.config.php index 044c5ca..8b7f978 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -6,14 +6,51 @@ ], ], 'phpab' => [ + 'analytics' => [ + /** + * The name of the service that collects analytics. + */ + 'collector' => 'phpab.default_analytics_collector', + + /** + * The name of the service that handles analytics after collecting. + * You can set this to null to ignore analytics. + */ + 'handler' => 'phpab.default_analytics_handler', + ], + + /** + * The name of the service that loads the default filter for tests. + * You can set this to null to ignore the default filter. + */ 'default_filter' => 'phpab.default_filter', + + /** + * The name of the service that loads the default variant chooser for tests. + * You can set this to null to ignore the default variant chooser. + */ 'default_variant_chooser' => 'phpab.default_variant_chooser', + + /** + * The name of the storage that should be loaded. + * For a list with options go to the documentation: https://phpab.github.io/ + */ 'storage' => 'runtime', + + /** + * An array with options for the storage providers. + * The options are defined in the documentation: https://phpab.github.io/ + */ 'storage_options' => [], + + /** + * An array with tests, how to define these tests is defined in the documentation: https://phpab.github.io/ + */ 'tests' => [], ], 'service_manager' => [ 'factories' => [ + 'phpab.default_analytics_handler' => 'PhpAbModule\\Service\\DefaultAnalyticsHandlerFactory', 'phpab.default_filter' => 'PhpAbModule\\Service\\DefaultFilterFactory', 'phpab.default_variant_chooser' => 'PhpAbModule\\Service\\DefaultVariantChooserFactory', 'phpab.dispatcher' => 'PhpAbModule\\Service\\DispatcherFactory', @@ -21,10 +58,15 @@ 'phpab.participation_manager' => 'PhpAbModule\\Service\\ParticipationManagerFactory', 'phpab.storage' => 'PhpAbModule\\Service\\StorageFactory', ], + 'invokables' => [ + 'phpab.default_analytics_collector' => 'PhpAb\\Analytics\\DataCollector\\\Generic', + 'phpab.analytics_data_collector' => 'PhpAb\Analytics\Google\DataCollector', + ], ], 'view_helpers' => [ 'factories' => [ 'phpAbIsActive' => 'PhpAbModule\\View\\Helper\\Service\\IsActiveFactory', + 'phpAbScript' => 'PhpAbModule\\View\\Helper\\Service\\ScriptFactory', ], ], ]; diff --git a/config/phpab.global.php.dist b/config/phpab.global.php.dist index 3dce11b..f804e27 100644 --- a/config/phpab.global.php.dist +++ b/config/phpab.global.php.dist @@ -2,10 +2,67 @@ return [ 'phpab' => [ + 'analytics' => [ + /** + * The name of the service that collects analytics. + */ + 'collector' => 'phpab.default_analytics_collector', + + /** + * The name of the service that handles analytics after collecting. + * You can set this to null to ignore analytics. + */ + 'handler' => 'phpab.default_analytics_handler', + ], + + /** + * The name of the service that loads the default filter for tests. + * You can set this to null to ignore the default filter. + */ 'default_filter' => 'phpab.default_filter', + + /** + * The name of the service that loads the default variant chooser for tests. + * You can set this to null to ignore the default variant chooser. + */ 'default_variant_chooser' => 'phpab.default_variant_chooser', + + /** + * The name of the storage that should be loaded. + * For a list with options go to the documentation: https://phpab.github.io/ + */ 'storage' => 'runtime', + + /** + * An array with options for the storage providers. + * The options are defined in the documentation: https://phpab.github.io/ + */ 'storage_options' => [], + + /** + * An array with tests, how to define these tests is defined in the documentation: https://phpab.github.io/ + */ 'tests' => [], ], + 'service_manager' => [ + 'invokables' => [ + 'phpab.analytics_data_collector' => 'PhpAb\Analytics\Google\DataCollector', + ], + 'factories' => [ + 'phpab.default_analytics_handler' => function($serviceManager) { + $config = $serviceManager->get('Config'); + + if (!$serviceManager->has($config['phpab']['analytics']['collector'])) { + throw new RuntimeException(sprintf( + 'The data collector "%s" does not exists.', + $config['phpab']['analytics']['collector'] + )); + } + + $analyticsData = $serviceManager->get($config['phpab']['analytics']['collector']); + + return new \PhpAb\Analytics\Renderer\GoogleUniversalAnalytics($analyticsData->getTestsData()); + }, + ], + ], ]; diff --git a/src/Controller/Plugin/IsActive.php b/src/Controller/Plugin/IsActive.php index 6dbf95c..472c3af 100644 --- a/src/Controller/Plugin/IsActive.php +++ b/src/Controller/Plugin/IsActive.php @@ -9,7 +9,7 @@ namespace PhpAbModule\Controller\Plugin; -use PhpAb\Participation\ParticipationManagerInterface; +use PhpAb\Participation\ManagerInterface; use Zend\Mvc\Controller\Plugin\AbstractPlugin; /** @@ -18,16 +18,16 @@ class IsActive extends AbstractPlugin { /** - * @var ParticipationManagerInterface The participation manager used to check participation. + * @var ManagerInterface The participation manager used to check participation. */ private $participationManager; /** * Initializes a new instance of this class. * - * @param ParticipationManagerInterface $participationManager The participation manager used to check participation. + * @param ManagerInterface $participationManager The participation manager used to check participation. */ - public function __construct(ParticipationManagerInterface $participationManager) + public function __construct(ManagerInterface $participationManager) { $this->participationManager = $participationManager; } diff --git a/src/Service/DefaultAnalyticsHandlerFactory.php b/src/Service/DefaultAnalyticsHandlerFactory.php new file mode 100644 index 0000000..94bdddf --- /dev/null +++ b/src/Service/DefaultAnalyticsHandlerFactory.php @@ -0,0 +1,35 @@ +get('Config'); + $collectorServiceName = $config['phpab']['analytics']['collector']; + + if (!$serviceLocator->has($collectorServiceName)) { + throw new RuntimeException(sprintf( + 'The data collector "%s" does not exists.', + $config['phpab']['analytics']['collector'] + )); + } + + $analyticsData = $serviceLocator->get($collectorServiceName); + + return new GoogleUniversalAnalytics($analyticsData->getTestsData()); + } +} diff --git a/src/Service/DefaultFilterFactory.php b/src/Service/DefaultFilterFactory.php index 723e2b0..b11adbf 100644 --- a/src/Service/DefaultFilterFactory.php +++ b/src/Service/DefaultFilterFactory.php @@ -9,7 +9,7 @@ namespace PhpAbModule\Service; -use PhpAb\Participation\PercentageFilter; +use PhpAb\Participation\Filter\Percentage; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -17,6 +17,6 @@ class DefaultFilterFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { - return new PercentageFilter(100); + return new Percentage(100); } } diff --git a/src/Service/DefaultVariantChooserFactory.php b/src/Service/DefaultVariantChooserFactory.php index 9bb5848..556a031 100644 --- a/src/Service/DefaultVariantChooserFactory.php +++ b/src/Service/DefaultVariantChooserFactory.php @@ -9,7 +9,7 @@ namespace PhpAbModule\Service; -use PhpAb\Variant\RandomChooser; +use PhpAb\Variant\Chooser\RandomChooser; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; diff --git a/src/Service/DispatcherFactory.php b/src/Service/DispatcherFactory.php index 7f175cb..4f6cfee 100644 --- a/src/Service/DispatcherFactory.php +++ b/src/Service/DispatcherFactory.php @@ -11,6 +11,7 @@ use PhpAb\Engine\Engine; use PhpAb\Event\Dispatcher; +use PhpAb\Event\SubscriberInterface; use PhpAb\Storage\Cookie; use PhpAb\Storage\Runtime; use PhpAb\Storage\Session; @@ -22,6 +23,24 @@ class DispatcherFactory implements FactoryInterface { public function createService(ServiceLocatorInterface $serviceLocator) { - return new Dispatcher(); + $dispatcher = new Dispatcher(); + + $config = $serviceLocator->get('Config'); + $collectorName = $config['phpab']['analytics']['collector']; + + if ($collectorName && $serviceLocator->has($collectorName)) { + $dataCollector = $serviceLocator->get($collectorName); + + if (!$dataCollector instanceof SubscriberInterface) { + throw new RuntimeException(sprintf( + 'The data collector is not an instance of %s', + SubscriberInterface::class + )); + } + + $dispatcher->addSubscriber($dataCollector); + } + + return $dispatcher; } } diff --git a/src/View/Helper/IsActive.php b/src/View/Helper/IsActive.php index 242e632..80b4774 100644 --- a/src/View/Helper/IsActive.php +++ b/src/View/Helper/IsActive.php @@ -9,7 +9,7 @@ namespace PhpAbModule\View\Helper; -use PhpAb\Participation\ParticipationManagerInterface; +use PhpAb\Participation\ManagerInterface; use Zend\View\Helper\AbstractHelper; /** @@ -18,16 +18,16 @@ class IsActive extends AbstractHelper { /** - * @var ParticipationManagerInterface The participation manager used to check participation. + * @var ManagerInterface The participation manager used to check participation. */ private $participationManager; /** * Initializes a new instance of this class. * - * @param ParticipationManagerInterface $participationManager The participation manager used to check participation. + * @param ManagerInterface $participationManager The participation manager used to check participation. */ - public function __construct(ParticipationManagerInterface $participationManager) + public function __construct(ManagerInterface $participationManager) { $this->participationManager = $participationManager; } diff --git a/src/View/Helper/Script.php b/src/View/Helper/Script.php new file mode 100644 index 0000000..557ce25 --- /dev/null +++ b/src/View/Helper/Script.php @@ -0,0 +1,50 @@ +renderer = $renderer; + } + + /** + * Generates the script that is needed to make phpab work. + * + * @return string + */ + public function __invoke() + { + if ($this->renderer instanceof JavascriptRendererInterface) { + return $this->renderer->getScript(); + } + + return ''; + } +} diff --git a/src/View/Helper/Service/ScriptFactory.php b/src/View/Helper/Service/ScriptFactory.php new file mode 100644 index 0000000..9458304 --- /dev/null +++ b/src/View/Helper/Service/ScriptFactory.php @@ -0,0 +1,36 @@ +getServiceLocator(); + + $config = $serviceManager->get('Config'); + + if (!$serviceManager->has($config['phpab']['analytics']['renderer'])) { + throw new RuntimeException(sprintf( + 'The data renderer "%s" does not exists.', + $config['phpab']['analytics']['renderer'] + )); + } + + $renderer = $serviceManager->get($config['phpab']['analytics']['renderer']); + + return new Script($renderer); + } +} diff --git a/tests/PhpAbModuleTest/Controller/Plugin/IsActiveTest.php b/tests/PhpAbModuleTest/Controller/Plugin/IsActiveTest.php index c417dbe..b4ce2b8 100644 --- a/tests/PhpAbModuleTest/Controller/Plugin/IsActiveTest.php +++ b/tests/PhpAbModuleTest/Controller/Plugin/IsActiveTest.php @@ -9,7 +9,7 @@ namespace PhpAbModuleTest\Controller\Plugin; -use PhpAb\Participation\ParticipationManagerInterface; +use PhpAb\Participation\ManagerInterface; use PhpAbModule\Controller\Plugin\IsActive; use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_TestCase; @@ -23,7 +23,7 @@ class IsActiveTest extends PHPUnit_Framework_TestCase protected function setUp() { - $this->participationManager = $this->getMockForAbstractClass(ParticipationManagerInterface::class); + $this->participationManager = $this->getMockForAbstractClass(ManagerInterface::class); } public function testWithParticipationManagerReturningTrue() diff --git a/tests/PhpAbModuleTest/Controller/Plugin/Service/IsActiveFactoryTest.php b/tests/PhpAbModuleTest/Controller/Plugin/Service/IsActiveFactoryTest.php index af1fd7c..68047e8 100644 --- a/tests/PhpAbModuleTest/Controller/Plugin/Service/IsActiveFactoryTest.php +++ b/tests/PhpAbModuleTest/Controller/Plugin/Service/IsActiveFactoryTest.php @@ -9,7 +9,7 @@ namespace PhpAbModuleTest\Controller\Plugin\Service; -use PhpAb\Participation\ParticipationManagerInterface; +use PhpAb\Participation\ManagerInterface; use PhpAbModule\Controller\Plugin\IsActive; use PhpAbModule\Controller\Plugin\Service\IsActiveFactory; use PHPUnit_Framework_TestCase; @@ -23,7 +23,7 @@ public function testCreateService() // Arrange $factory = new IsActiveFactory(); - $participationManager = $this->getMockForAbstractClass(ParticipationManagerInterface::class); + $participationManager = $this->getMockForAbstractClass(ManagerInterface::class); $serviceManager = $this->getMockForAbstractClass(ServiceLocatorInterface::class); $serviceManager diff --git a/tests/PhpAbModuleTest/Service/DefaultAnalyticsHandlerFactoryTest.php b/tests/PhpAbModuleTest/Service/DefaultAnalyticsHandlerFactoryTest.php new file mode 100644 index 0000000..c3fafc2 --- /dev/null +++ b/tests/PhpAbModuleTest/Service/DefaultAnalyticsHandlerFactoryTest.php @@ -0,0 +1,64 @@ +getMockForAbstractClass(ServiceLocatorInterface::class); + $serviceLocator->expects($this->at(1))->method('has')->willReturn(true); + $serviceLocator->expects($this->at(0))->method('get')->with($this->equalTo('Config'))->willReturn([ + 'phpab' => [ + 'analytics' => [ + 'collector' => 'my-collector', + ], + ], + ]); + $serviceLocator->expects($this->at(2))->method('get')->with($this->equalTo('my-collector'))->willReturn( + new Google() + ); + + $service = new DefaultAnalyticsHandlerFactory(); + + // Act + $result = $service->createService($serviceLocator); + + // Assert + $this->assertInstanceOf(GoogleUniversalAnalytics::class, $result); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessage The data collector "" does not exists. + */ + public function testCreateServiceWithoutDataCollector() + { + // Arrange + $serviceLocator = $this->getMockForAbstractClass(ServiceLocatorInterface::class); + $serviceLocator->expects($this->once())->method('has')->willReturn(false); + + $service = new DefaultAnalyticsHandlerFactory(); + + // Act + $result = $service->createService($serviceLocator); + + // Assert + $this->assertInstanceOf(GoogleUniversalAnalytics::class, $result); + } +} diff --git a/tests/PhpAbModuleTest/Service/DefaultFilterFactoryTest.php b/tests/PhpAbModuleTest/Service/DefaultFilterFactoryTest.php index f413a78..abb1812 100644 --- a/tests/PhpAbModuleTest/Service/DefaultFilterFactoryTest.php +++ b/tests/PhpAbModuleTest/Service/DefaultFilterFactoryTest.php @@ -9,7 +9,7 @@ namespace PhpAbModuleTest\Service; -use PhpAb\Participation\PercentageFilter; +use PhpAb\Participation\Filter\Percentage; use PhpAbModule\Service\DefaultFilterFactory; use PHPUnit_Framework_TestCase; use Zend\ServiceManager\ServiceLocatorInterface; @@ -26,6 +26,6 @@ public function testCreateService() $result = $service->createService($serviceLocator); // Assert - $this->assertInstanceOf(PercentageFilter::class, $result); + $this->assertInstanceOf(Percentage::class, $result); } } diff --git a/tests/PhpAbModuleTest/Service/DefaultVariantChooserFactoryTest.php b/tests/PhpAbModuleTest/Service/DefaultVariantChooserFactoryTest.php index 80988c4..3c758a5 100644 --- a/tests/PhpAbModuleTest/Service/DefaultVariantChooserFactoryTest.php +++ b/tests/PhpAbModuleTest/Service/DefaultVariantChooserFactoryTest.php @@ -9,7 +9,7 @@ namespace PhpAbModuleTest\Service; -use PhpAb\Variant\RandomChooser; +use PhpAb\Variant\Chooser\RandomChooser; use PhpAbModule\Service\DefaultVariantChooserFactory; use PHPUnit_Framework_TestCase; use Zend\ServiceManager\ServiceLocatorInterface; diff --git a/tests/PhpAbModuleTest/Service/DispatcherFactoryTest.php b/tests/PhpAbModuleTest/Service/DispatcherFactoryTest.php index fb13c3f..f33b5d2 100644 --- a/tests/PhpAbModuleTest/Service/DispatcherFactoryTest.php +++ b/tests/PhpAbModuleTest/Service/DispatcherFactoryTest.php @@ -9,14 +9,16 @@ namespace PhpAbModuleTest\Service; +use PhpAb\Analytics\DataCollector\Google; use PhpAb\Event\Dispatcher; use PhpAbModule\Service\DispatcherFactory; use PHPUnit_Framework_TestCase; +use stdClass; use Zend\ServiceManager\ServiceLocatorInterface; class DispatcherFactoryTest extends PHPUnit_Framework_TestCase { - public function testCreateService() + public function testWithoutDataCollector() { // Arrange $serviceLocator = $this->getMockForAbstractClass(ServiceLocatorInterface::class); @@ -28,4 +30,81 @@ public function testCreateService() // Assert $this->assertInstanceOf(Dispatcher::class, $result); } + + public function testWithDataCollector() + { + // Arrange + $serviceLocator = $this->getMockForAbstractClass(ServiceLocatorInterface::class); + $serviceLocator + ->expects($this->at(0)) + ->method('get') + ->with($this->equalTo('Config')) + ->willReturn([ + 'phpab' => [ + 'analytics' => [ + 'collector' => 'my-collector', + ], + ], + ]); + + $serviceLocator + ->expects($this->at(1)) + ->method('has') + ->with($this->equalTo('my-collector')) + ->willReturn(true); + + + $serviceLocator + ->expects($this->at(2)) + ->method('get') + ->with($this->equalTo('my-collector')) + ->willReturn(new Google()); + + $service = new DispatcherFactory(); + + // Act + $result = $service->createService($serviceLocator); + + // Assert + $this->assertInstanceOf(Dispatcher::class, $result); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessage The data collector is not an instance of PhpAb\Event\SubscriberInterface + */ + public function testWithInvalidDataCollector() + { + // Arrange + $serviceLocator = $this->getMockForAbstractClass(ServiceLocatorInterface::class); + $serviceLocator + ->expects($this->at(0)) + ->method('get') + ->with($this->equalTo('Config')) + ->willReturn([ + 'phpab' => [ + 'analytics' => [ + 'collector' => 'my-collector', + ], + ], + ]); + + $serviceLocator + ->expects($this->at(1)) + ->method('has') + ->with($this->equalTo('my-collector')) + ->willReturn(true); + + + $serviceLocator + ->expects($this->at(2)) + ->method('get') + ->with($this->equalTo('my-collector')) + ->willReturn(new stdClass()); + + $service = new DispatcherFactory(); + + // Act + $result = $service->createService($serviceLocator); + } } diff --git a/tests/PhpAbModuleTest/Service/EngineFactoryTest.php b/tests/PhpAbModuleTest/Service/EngineFactoryTest.php index 7d6f16b..f6d2acb 100644 --- a/tests/PhpAbModuleTest/Service/EngineFactoryTest.php +++ b/tests/PhpAbModuleTest/Service/EngineFactoryTest.php @@ -11,10 +11,10 @@ use PhpAb\Engine\EngineInterface; use PhpAb\Event\DispatcherInterface; -use PhpAb\Participation\ParticipationManagerInterface; -use PhpAb\Participation\PercentageFilter; +use PhpAb\Participation\Filter\Percentage; +use PhpAb\Participation\ManagerInterface; use PhpAb\Variant\CallbackVariant; -use PhpAb\Variant\RandomChooser; +use PhpAb\Variant\Chooser\RandomChooser; use PhpAb\Variant\SimpleVariant; use PhpAbModule\Service\EngineFactory; use PHPUnit_Framework_MockObject_MockObject; @@ -40,7 +40,7 @@ protected function createMockedServiceLocator($config = null, $withFilterAndVari ]; } - $participationManagerMock = $this->getMockForAbstractClass(ParticipationManagerInterface::class); + $participationManagerMock = $this->getMockForAbstractClass(ManagerInterface::class); $dispatcherMock = $this->getMockForAbstractClass(DispatcherInterface::class); $serviceLocator = $this->getMock(ServiceManager::class); @@ -68,7 +68,7 @@ protected function createMockedServiceLocator($config = null, $withFilterAndVari ->expects($this->at(3)) ->method('get') ->with($this->equalTo('my_filter')) - ->willReturn(new PercentageFilter(100)); + ->willReturn(new Percentage(100)); $serviceLocator ->expects($this->at(4)) @@ -172,7 +172,7 @@ public function testWithDefaultFilter() ->expects($this->at(4)) ->method('get') ->with($this->equalTo('my_filter')) - ->willReturn(new PercentageFilter(100)); + ->willReturn(new Percentage(100)); $factory = new EngineFactory(); diff --git a/tests/PhpAbModuleTest/Service/ParticipationManagerFactoryTest.php b/tests/PhpAbModuleTest/Service/ParticipationManagerFactoryTest.php index d437632..f395375 100644 --- a/tests/PhpAbModuleTest/Service/ParticipationManagerFactoryTest.php +++ b/tests/PhpAbModuleTest/Service/ParticipationManagerFactoryTest.php @@ -9,7 +9,7 @@ namespace PhpAbModuleTest\Service; -use PhpAb\Participation\Manager; +use PhpAb\Participation\ManagerInterface; use PhpAb\Storage\Runtime; use PhpAbModule\Service\ParticipationManagerFactory; use PHPUnit_Framework_TestCase; @@ -38,7 +38,7 @@ public function testCreateService() $result = $service->createService($serviceLocator); // Assert - $this->assertInstanceOf(Manager::class, $result); + $this->assertInstanceOf(ManagerInterface::class, $result); } /** diff --git a/tests/PhpAbModuleTest/View/Helper/IsActiveTest.php b/tests/PhpAbModuleTest/View/Helper/IsActiveTest.php index 432b4f0..e58ef9e 100644 --- a/tests/PhpAbModuleTest/View/Helper/IsActiveTest.php +++ b/tests/PhpAbModuleTest/View/Helper/IsActiveTest.php @@ -9,8 +9,7 @@ namespace PhpAbModuleTest\View\Helper\Plugin; -use PhpAb\Engine\EngineInterface; -use PhpAb\Participation\ParticipationManagerInterface; +use PhpAb\Participation\ManagerInterface; use PhpAbModule\View\Helper\IsActive; use PHPUnit_Framework_TestCase; @@ -23,7 +22,7 @@ class IsActiveTest extends PHPUnit_Framework_TestCase protected function setUp() { - $this->participationManager = $this->getMockForAbstractClass(ParticipationManagerInterface::class); + $this->participationManager = $this->getMockForAbstractClass(ManagerInterface::class); } public function testWithParticipationManagerReturningTrue() diff --git a/tests/PhpAbModuleTest/View/Helper/ScriptTest.php b/tests/PhpAbModuleTest/View/Helper/ScriptTest.php new file mode 100644 index 0000000..404f624 --- /dev/null +++ b/tests/PhpAbModuleTest/View/Helper/ScriptTest.php @@ -0,0 +1,53 @@ +getMockForAbstractClass(JavascriptRendererInterface::class); + $helper = new Script($renderer); + + // Assert + $renderer->expects($this->once())->method('getScript'); + + // Act + $helper->__invoke(); + } + + public function testInvokeWithoutNonRenderer() + { + // Arrange + $renderer = new Handler(); + $helper = new Script($renderer); + + // Act + $result = $helper->__invoke(); + + // Assert + $this->assertEquals('', $result); + } +} diff --git a/tests/PhpAbModuleTest/View/Helper/Service/IsActiveFactoryTest.php b/tests/PhpAbModuleTest/View/Helper/Service/IsActiveFactoryTest.php index c581f68..c2ceb02 100644 --- a/tests/PhpAbModuleTest/View/Helper/Service/IsActiveFactoryTest.php +++ b/tests/PhpAbModuleTest/View/Helper/Service/IsActiveFactoryTest.php @@ -9,8 +9,7 @@ namespace PhpAbModuleTest\View\Helper\Service; -use PhpAb\Engine\EngineInterface; -use PhpAb\Participation\ParticipationManagerInterface; +use PhpAb\Participation\ManagerInterface; use PhpAbModule\View\Helper\IsActive; use PhpAbModule\View\Helper\Service\IsActiveFactory; use PHPUnit_Framework_TestCase; @@ -24,7 +23,7 @@ public function testCreateService() // Arrange $factory = new IsActiveFactory(); - $participationManager = $this->getMockForAbstractClass(ParticipationManagerInterface::class); + $participationManager = $this->getMockForAbstractClass(ManagerInterface::class); $serviceManager = $this->getMockForAbstractClass(ServiceLocatorInterface::class); $serviceManager diff --git a/tests/PhpAbModuleTest/View/Helper/Service/ScriptFactoryTest.php b/tests/PhpAbModuleTest/View/Helper/Service/ScriptFactoryTest.php new file mode 100644 index 0000000..e692520 --- /dev/null +++ b/tests/PhpAbModuleTest/View/Helper/Service/ScriptFactoryTest.php @@ -0,0 +1,113 @@ +getMockForAbstractClass(AbstractGoogleAnalytics::class); + + $serviceManager = $this->getMockForAbstractClass(ServiceLocatorInterface::class); + + $serviceManager + ->expects($this->at(0)) + ->method('get') + ->with($this->equalTo('Config')) + ->willReturn([ + 'phpab' => [ + 'analytics' => [ + 'renderer' => 'my-renderer', + ], + ], + ]); + + $serviceManager + ->expects($this->at(1)) + ->method('has') + ->with($this->equalTo('my-renderer')) + ->willReturn(true); + + $serviceManager + ->expects($this->at(2)) + ->method('get') + ->with($this->equalTo('my-renderer')) + ->willReturn($renderer); + + $serviceLocator = $this + ->getMockBuilder(AbstractPluginManager::class) + ->disableOriginalConstructor() + ->setMethods(['getServiceLocator']) + ->getMockForAbstractClass(); + + $serviceLocator->expects($this->once())->method('getServiceLocator')->willReturn($serviceManager); + + // Act + $result = $factory->createService($serviceLocator); + + // Assert + $this->assertInstanceOf(Script::class, $result); + } + + /** + * @covers PhpAbModule\View\Helper\Service\ScriptFactory::createService + * @expectedException RuntimeException + * @expectedExceptionMessage The data renderer "my-renderer" does not exists. + */ + public function testWithInvalidRenderer() + { + // Arrange + $factory = new ScriptFactory(); + + $serviceManager = $this->getMockForAbstractClass(ServiceLocatorInterface::class); + + $serviceManager + ->expects($this->at(0)) + ->method('get') + ->with($this->equalTo('Config')) + ->willReturn([ + 'phpab' => [ + 'analytics' => [ + 'renderer' => 'my-renderer', + ], + ], + ]); + + $serviceManager + ->expects($this->at(1)) + ->method('has') + ->with($this->equalTo('my-renderer')) + ->willReturn(false); + + $serviceLocator = $this + ->getMockBuilder(AbstractPluginManager::class) + ->disableOriginalConstructor() + ->setMethods(['getServiceLocator']) + ->getMockForAbstractClass(); + + $serviceLocator->expects($this->once())->method('getServiceLocator')->willReturn($serviceManager); + + // Act + $result = $factory->createService($serviceLocator); + } +}