Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,67 @@
],
],
'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',
'phpab.engine' => 'PhpAbModule\\Service\\EngineFactory',
'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',
],
],
];
Expand Down
57 changes: 57 additions & 0 deletions config/phpab.global.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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());
},
],
],
];
8 changes: 4 additions & 4 deletions src/Controller/Plugin/IsActive.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace PhpAbModule\Controller\Plugin;

use PhpAb\Participation\ParticipationManagerInterface;
use PhpAb\Participation\ManagerInterface;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;

/**
Expand All @@ -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;
}
Expand Down
35 changes: 35 additions & 0 deletions src/Service/DefaultAnalyticsHandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* This file is part of phpab/phpab-module. (https://github.com/phpab/phpab-module)
*
* @link https://github.com/phpab/phpab-module for the canonical source repository
* @copyright Copyright (c) 2015-2016 phpab. (https://github.com/phpab/)
* @license https://raw.githubusercontent.com/phpab/phpab-module/master/LICENSE MIT
*/

namespace PhpAbModule\Service;

use PhpAb\Analytics\Renderer\Google\GoogleUniversalAnalytics;
use RuntimeException;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class DefaultAnalyticsHandlerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->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());
}
}
4 changes: 2 additions & 2 deletions src/Service/DefaultFilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

namespace PhpAbModule\Service;

use PhpAb\Participation\PercentageFilter;
use PhpAb\Participation\Filter\Percentage;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class DefaultFilterFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
return new PercentageFilter(100);
return new Percentage(100);
}
}
2 changes: 1 addition & 1 deletion src/Service/DefaultVariantChooserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace PhpAbModule\Service;

use PhpAb\Variant\RandomChooser;
use PhpAb\Variant\Chooser\RandomChooser;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand Down
21 changes: 20 additions & 1 deletion src/Service/DispatcherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
8 changes: 4 additions & 4 deletions src/View/Helper/IsActive.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace PhpAbModule\View\Helper;

use PhpAb\Participation\ParticipationManagerInterface;
use PhpAb\Participation\ManagerInterface;
use Zend\View\Helper\AbstractHelper;

/**
Expand All @@ -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;
}
Expand Down
50 changes: 50 additions & 0 deletions src/View/Helper/Script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* This file is part of phpab/phpab-module. (https://github.com/phpab/phpab-module)
*
* @link https://github.com/phpab/phpab-module for the canonical source repository
* @copyright Copyright (c) 2015-2016 phpab. (https://github.com/phpab/)
* @license https://raw.githubusercontent.com/phpab/phpab-module/master/LICENSE MIT
*/

namespace PhpAbModule\View\Helper;

use PhpAb\Analytics\Renderer\AbstractGoogleAnalytics;
use PhpAb\Analytics\Renderer\JavascriptRendererInterface;
use PhpAb\Analytics\Renderer\RendererInterface;
use Zend\View\Helper\AbstractHelper;

/**
* A view helper that can be used to render a script for analytics.
*/
class Script extends AbstractHelper
{
/**
* @var RendererInterface
*/
private $renderer;

/**
* Initializes a new instance of this class.
*
* @param RendererInterface $renderer The analytics to render a script for.
*/
public function __construct(RendererInterface $renderer)
{
$this->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 '';
}
}
36 changes: 36 additions & 0 deletions src/View/Helper/Service/ScriptFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* This file is part of phpab/phpab-module. (https://github.com/phpab/phpab-module)
*
* @link https://github.com/phpab/phpab-module for the canonical source repository
* @copyright Copyright (c) 2015-2016 phpab. (https://github.com/phpab/)
* @license https://raw.githubusercontent.com/phpab/phpab-module/master/LICENSE MIT
*/

namespace PhpAbModule\View\Helper\Service;

use PhpAbModule\View\Helper\Script;
use RuntimeException;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ScriptFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$serviceManager = $serviceLocator->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);
}
}
4 changes: 2 additions & 2 deletions tests/PhpAbModuleTest/Controller/Plugin/IsActiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down
Loading