Ejemplo n.º 1
0
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     $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;
 }
Ejemplo n.º 2
0
 public function testDispatchMultipleEvents()
 {
     // Arrange
     $dispatcher = new Dispatcher();
     $dispatcher->addListener('event.foo', function ($subject) {
         $subject->touched++;
     });
     $dispatcher->addListener('event.bar', function ($subject) {
         $subject->touched++;
     });
     $subject = new \stdClass();
     $subject->touched = 0;
     // Act
     $dispatcher->dispatch('event.foo', $subject);
     $dispatcher->dispatch('event.bar', $subject);
     // Assert
     $this->assertEquals(2, $subject->touched);
 }
Ejemplo n.º 3
0
require_once __DIR__ . '/../vendor/autoload.php';
use PhpAb\Storage\Cookie;
use PhpAb\Participation\Manager;
use PhpAb\Analytics\DataCollector\Generic;
use PhpAb\Event\Dispatcher;
use PhpAb\Participation\Filter\Percentage;
use PhpAb\Variant\Chooser\RandomChooser;
use PhpAb\Variant\SimpleVariant;
use PhpAb\Variant\CallbackVariant;
use PhpAb\Engine\Engine;
use PhpAb\Test\Test;
$storage = new Cookie('phpab');
$manager = new Manager($storage);
$analyticsData = new Generic();
$dispatcher = new Dispatcher();
$dispatcher->addSubscriber($analyticsData);
$filter = new Percentage(50);
$chooser = new RandomChooser();
$engine = new Engine($manager, $dispatcher, $filter, $chooser);
$test = new Test('foo_test');
$test->addVariant(new SimpleVariant('_control'));
$test->addVariant(new CallbackVariant('v1', function () {
    echo 'v1';
}));
$test->addVariant(new CallbackVariant('v2', function () {
    echo 'v2';
}));
$test->addVariant(new CallbackVariant('v3', function () {
    echo 'v3';
}));
Ejemplo n.º 4
0
 /**
  * Testing that Engine picks previous test runs values
  */
 public function testPreviousRunConsistencyInStorage()
 {
     // Arrange
     $storage = new Storage(new Runtime(['foo_test' => 'v1', 'bar_test' => '_control']));
     $manager = new Manager($storage);
     $analyticsData = new Google();
     $dispatcher = new Dispatcher();
     $dispatcher->addSubscriber($analyticsData);
     $filter = new Percentage(5);
     $chooser = new RandomChooser();
     $engine = new Engine($manager, $dispatcher, $filter, $chooser);
     $test = new Test('foo_test', [], [Google::EXPERIMENT_ID => 'EXPID1']);
     $test->addVariant(new SimpleVariant('_control'));
     $test->addVariant(new SimpleVariant('v1'));
     $test->addVariant(new SimpleVariant('v2'));
     $test2 = new Test('bar_test', [], [Google::EXPERIMENT_ID => 'EXPID2']);
     $test2->addVariant(new SimpleVariant('_control'));
     $test2->addVariant(new SimpleVariant('v1'));
     $engine->addTest($test);
     $engine->addTest($test2);
     $engine->start();
     // Act
     $testData = $analyticsData->getTestsData();
     // Assert
     $this->assertSame(['EXPID1' => 1, 'EXPID2' => 0], $testData);
 }