From 19bc8f5d185d6db6d29a55eaf8049b07d34bacc0 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 27 Apr 2016 21:48:31 +0200 Subject: [PATCH 1/6] WIP --- config/module.config.php | 5 +++ src/Service/DispatcherFactory.php | 21 +++++++++++- src/View/Helper/Script.php | 41 +++++++++++++++++++++++ src/View/Helper/Service/ScriptFactory.php | 36 ++++++++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/View/Helper/Script.php create mode 100644 src/View/Helper/Service/ScriptFactory.php diff --git a/config/module.config.php b/config/module.config.php index 044c5ca..4c8186f 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -6,6 +6,10 @@ ], ], 'phpab' => [ + 'analytics' => [ + 'collector' => null, + 'renderer' => null, + ], 'default_filter' => 'phpab.default_filter', 'default_variant_chooser' => 'phpab.default_variant_chooser', 'storage' => 'runtime', @@ -25,6 +29,7 @@ 'view_helpers' => [ 'factories' => [ 'phpAbIsActive' => 'PhpAbModule\\View\\Helper\\Service\\IsActiveFactory', + 'phpAbScript' => 'PhpAbModule\\View\\Helper\\Service\\ScriptFactory', ], ], ]; 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/Script.php b/src/View/Helper/Script.php new file mode 100644 index 0000000..904e46b --- /dev/null +++ b/src/View/Helper/Script.php @@ -0,0 +1,41 @@ +analytics = $analytics; + } + + /** + * Generates the script that is needed to make phpab work. + * + * @return string + */ + public function __invoke() + { + return $this->analytics->getScript(); + } +} 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); + } +} From c634d05fb6c692804584f8af4b48481b81926c41 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 27 Apr 2016 22:50:25 +0200 Subject: [PATCH 2/6] Added tests for the data collector to be added to the event dispatcher. --- .../Service/DispatcherFactoryTest.php | 81 ++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/tests/PhpAbModuleTest/Service/DispatcherFactoryTest.php b/tests/PhpAbModuleTest/Service/DispatcherFactoryTest.php index fb13c3f..fedd8a5 100644 --- a/tests/PhpAbModuleTest/Service/DispatcherFactoryTest.php +++ b/tests/PhpAbModuleTest/Service/DispatcherFactoryTest.php @@ -9,14 +9,16 @@ namespace PhpAbModuleTest\Service; +use PhpAb\Analytics\Google\DataCollector; 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 DataCollector()); + + $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); + } } From 34503c04617e0f3fca523329990ef7c0a7b6f63e Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Wed, 27 Apr 2016 22:58:43 +0200 Subject: [PATCH 3/6] Added all tests so that we have 100% coverage again --- .../View/Helper/ScriptTest.php | 30 +++++ .../View/Helper/Service/ScriptFactoryTest.php | 113 ++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 tests/PhpAbModuleTest/View/Helper/ScriptTest.php create mode 100644 tests/PhpAbModuleTest/View/Helper/Service/ScriptFactoryTest.php diff --git a/tests/PhpAbModuleTest/View/Helper/ScriptTest.php b/tests/PhpAbModuleTest/View/Helper/ScriptTest.php new file mode 100644 index 0000000..a073986 --- /dev/null +++ b/tests/PhpAbModuleTest/View/Helper/ScriptTest.php @@ -0,0 +1,30 @@ +getMockForAbstractClass(AbstractGoogleAnalytics::class); + $helper = new Script($renderer); + + // Assert + $renderer->expects($this->once())->method('getScript'); + + // Act + $helper->__invoke(); + } +} diff --git a/tests/PhpAbModuleTest/View/Helper/Service/ScriptFactoryTest.php b/tests/PhpAbModuleTest/View/Helper/Service/ScriptFactoryTest.php new file mode 100644 index 0000000..1739487 --- /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); + } +} From 704c153c25ef6e0c97f3b4c1b624d9dc9725e896 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Thu, 28 Apr 2016 20:07:29 +0200 Subject: [PATCH 4/6] Implemented the new RendererInterface --- src/View/Helper/Script.php | 14 +++++++++----- tests/PhpAbModuleTest/View/Helper/ScriptTest.php | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/View/Helper/Script.php b/src/View/Helper/Script.php index 904e46b..e3caa3b 100644 --- a/src/View/Helper/Script.php +++ b/src/View/Helper/Script.php @@ -10,6 +10,7 @@ namespace PhpAbModule\View\Helper; use PhpAb\Analytics\Renderer\AbstractGoogleAnalytics; +use PhpAb\Analytics\Renderer\RendererInterface; use Zend\View\Helper\AbstractHelper; /** @@ -17,16 +18,19 @@ */ class Script extends AbstractHelper { - private $analytics; + /** + * @var RendererInterface + */ + private $renderer; /** * Initializes a new instance of this class. * - * @param AbstractGoogleAnalytics $analytics The analytics to render a script for. + * @param RendererInterface $renderer The analytics to render a script for. */ - public function __construct(AbstractGoogleAnalytics $analytics) + public function __construct(RendererInterface $renderer) { - $this->analytics = $analytics; + $this->renderer = $renderer; } /** @@ -36,6 +40,6 @@ public function __construct(AbstractGoogleAnalytics $analytics) */ public function __invoke() { - return $this->analytics->getScript(); + return $this->renderer->getScript(); } } diff --git a/tests/PhpAbModuleTest/View/Helper/ScriptTest.php b/tests/PhpAbModuleTest/View/Helper/ScriptTest.php index a073986..382015b 100644 --- a/tests/PhpAbModuleTest/View/Helper/ScriptTest.php +++ b/tests/PhpAbModuleTest/View/Helper/ScriptTest.php @@ -9,7 +9,7 @@ namespace PhpAbModuleTest\View\Helper\Plugin; -use PhpAb\Analytics\Renderer\AbstractGoogleAnalytics; +use PhpAb\Analytics\Renderer\RendererInterface; use PhpAbModule\View\Helper\Script; use PHPUnit_Framework_TestCase; @@ -18,7 +18,7 @@ class ScriptTest extends PHPUnit_Framework_TestCase public function testInvoke() { // Arrange - $renderer = $this->getMockForAbstractClass(AbstractGoogleAnalytics::class); + $renderer = $this->getMockForAbstractClass(RendererInterface::class); $helper = new Script($renderer); // Assert From 3a6e74e45127f0ad844dcb2992e55620c881144f Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Sat, 30 Apr 2016 19:35:19 +0200 Subject: [PATCH 5/6] Fixed all tests for the newest phpab version. --- src/Controller/Plugin/IsActive.php | 8 ++++---- src/Service/DefaultFilterFactory.php | 4 ++-- src/Service/DefaultVariantChooserFactory.php | 2 +- src/View/Helper/IsActive.php | 8 ++++---- src/View/Helper/Script.php | 14 +++++++++----- .../Controller/Plugin/IsActiveTest.php | 4 ++-- .../Plugin/Service/IsActiveFactoryTest.php | 4 ++-- .../Service/DefaultFilterFactoryTest.php | 4 ++-- .../Service/DefaultVariantChooserFactoryTest.php | 2 +- .../Service/DispatcherFactoryTest.php | 4 ++-- .../PhpAbModuleTest/Service/EngineFactoryTest.php | 12 ++++++------ .../Service/ParticipationManagerFactoryTest.php | 4 ++-- tests/PhpAbModuleTest/View/Helper/IsActiveTest.php | 5 ++--- tests/PhpAbModuleTest/View/Helper/ScriptTest.php | 4 ++-- .../View/Helper/Service/IsActiveFactoryTest.php | 5 ++--- .../View/Helper/Service/ScriptFactoryTest.php | 2 +- 16 files changed, 44 insertions(+), 42 deletions(-) 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/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/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 index e3caa3b..161eaff 100644 --- a/src/View/Helper/Script.php +++ b/src/View/Helper/Script.php @@ -10,7 +10,7 @@ namespace PhpAbModule\View\Helper; use PhpAb\Analytics\Renderer\AbstractGoogleAnalytics; -use PhpAb\Analytics\Renderer\RendererInterface; +use PhpAb\Analytics\Renderer\JavascriptRendererInterface; use Zend\View\Helper\AbstractHelper; /** @@ -19,16 +19,16 @@ class Script extends AbstractHelper { /** - * @var RendererInterface + * @var JavascriptRendererInterface */ private $renderer; /** * Initializes a new instance of this class. * - * @param RendererInterface $renderer The analytics to render a script for. + * @param JavascriptRendererInterface $renderer The analytics to render a script for. */ - public function __construct(RendererInterface $renderer) + public function __construct(JavascriptRendererInterface $renderer) { $this->renderer = $renderer; } @@ -40,6 +40,10 @@ public function __construct(RendererInterface $renderer) */ public function __invoke() { - return $this->renderer->getScript(); + if ($this->renderer instanceof JavascriptRendererInterface) { + return $this->renderer->getScript(); + } + + return ''; } } 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/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 fedd8a5..f33b5d2 100644 --- a/tests/PhpAbModuleTest/Service/DispatcherFactoryTest.php +++ b/tests/PhpAbModuleTest/Service/DispatcherFactoryTest.php @@ -9,7 +9,7 @@ namespace PhpAbModuleTest\Service; -use PhpAb\Analytics\Google\DataCollector; +use PhpAb\Analytics\DataCollector\Google; use PhpAb\Event\Dispatcher; use PhpAbModule\Service\DispatcherFactory; use PHPUnit_Framework_TestCase; @@ -58,7 +58,7 @@ public function testWithDataCollector() ->expects($this->at(2)) ->method('get') ->with($this->equalTo('my-collector')) - ->willReturn(new DataCollector()); + ->willReturn(new Google()); $service = new DispatcherFactory(); 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 index 382015b..5fb657a 100644 --- a/tests/PhpAbModuleTest/View/Helper/ScriptTest.php +++ b/tests/PhpAbModuleTest/View/Helper/ScriptTest.php @@ -9,7 +9,7 @@ namespace PhpAbModuleTest\View\Helper\Plugin; -use PhpAb\Analytics\Renderer\RendererInterface; +use PhpAb\Analytics\Renderer\JavascriptRendererInterface; use PhpAbModule\View\Helper\Script; use PHPUnit_Framework_TestCase; @@ -18,7 +18,7 @@ class ScriptTest extends PHPUnit_Framework_TestCase public function testInvoke() { // Arrange - $renderer = $this->getMockForAbstractClass(RendererInterface::class); + $renderer = $this->getMockForAbstractClass(JavascriptRendererInterface::class); $helper = new Script($renderer); // Assert 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 index 1739487..e692520 100644 --- a/tests/PhpAbModuleTest/View/Helper/Service/ScriptFactoryTest.php +++ b/tests/PhpAbModuleTest/View/Helper/Service/ScriptFactoryTest.php @@ -9,7 +9,7 @@ namespace PhpAbModuleTest\View\Helper\Service; -use PhpAb\Analytics\Renderer\AbstractGoogleAnalytics; +use PhpAb\Analytics\Renderer\Google\AbstractGoogleAnalytics; use PhpAbModule\View\Helper\Script; use PhpAbModule\View\Helper\Service\ScriptFactory; use PHPUnit_Framework_TestCase; From e3c6401d11f9a6dbd69f9a6b4ac7261fa3271e94 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Sat, 30 Apr 2016 20:07:52 +0200 Subject: [PATCH 6/6] Added default analytics handler and collector --- config/module.config.php | 41 +++++++++++- config/phpab.global.php.dist | 57 +++++++++++++++++ .../DefaultAnalyticsHandlerFactory.php | 35 ++++++++++ src/View/Helper/Script.php | 7 +- .../DefaultAnalyticsHandlerFactoryTest.php | 64 +++++++++++++++++++ .../View/Helper/ScriptTest.php | 23 +++++++ 6 files changed, 222 insertions(+), 5 deletions(-) create mode 100644 src/Service/DefaultAnalyticsHandlerFactory.php create mode 100644 tests/PhpAbModuleTest/Service/DefaultAnalyticsHandlerFactoryTest.php diff --git a/config/module.config.php b/config/module.config.php index 4c8186f..8b7f978 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -7,17 +7,50 @@ ], 'phpab' => [ 'analytics' => [ - 'collector' => null, - 'renderer' => null, + /** + * 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', @@ -25,6 +58,10 @@ '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' => [ 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/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/View/Helper/Script.php b/src/View/Helper/Script.php index 161eaff..557ce25 100644 --- a/src/View/Helper/Script.php +++ b/src/View/Helper/Script.php @@ -11,6 +11,7 @@ use PhpAb\Analytics\Renderer\AbstractGoogleAnalytics; use PhpAb\Analytics\Renderer\JavascriptRendererInterface; +use PhpAb\Analytics\Renderer\RendererInterface; use Zend\View\Helper\AbstractHelper; /** @@ -19,16 +20,16 @@ class Script extends AbstractHelper { /** - * @var JavascriptRendererInterface + * @var RendererInterface */ private $renderer; /** * Initializes a new instance of this class. * - * @param JavascriptRendererInterface $renderer The analytics to render a script for. + * @param RendererInterface $renderer The analytics to render a script for. */ - public function __construct(JavascriptRendererInterface $renderer) + public function __construct(RendererInterface $renderer) { $this->renderer = $renderer; } 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/View/Helper/ScriptTest.php b/tests/PhpAbModuleTest/View/Helper/ScriptTest.php index 5fb657a..404f624 100644 --- a/tests/PhpAbModuleTest/View/Helper/ScriptTest.php +++ b/tests/PhpAbModuleTest/View/Helper/ScriptTest.php @@ -9,10 +9,20 @@ namespace PhpAbModuleTest\View\Helper\Plugin; +use PhpAb\Analytics\Renderer\Google\AbstractGoogleAnalytics; use PhpAb\Analytics\Renderer\JavascriptRendererInterface; +use PhpAb\Analytics\Renderer\RendererInterface; use PhpAbModule\View\Helper\Script; use PHPUnit_Framework_TestCase; +class Handler implements RendererInterface +{ + public function getParticipations() + { + return []; + } +} + class ScriptTest extends PHPUnit_Framework_TestCase { public function testInvoke() @@ -27,4 +37,17 @@ public function testInvoke() // Act $helper->__invoke(); } + + public function testInvokeWithoutNonRenderer() + { + // Arrange + $renderer = new Handler(); + $helper = new Script($renderer); + + // Act + $result = $helper->__invoke(); + + // Assert + $this->assertEquals('', $result); + } }