Skip to content

Commit

Permalink
Merge pull request #5 from waltertamboer/feature/fix-example
Browse files Browse the repository at this point in the history
Feature/fix example
  • Loading branch information
waltertamboer committed Mar 28, 2016
2 parents 41dcc4c + 52f64a3 commit 28b4dad
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions example/example.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
<?php
/**
* This file is part of phpab/phpab (https://github.com/phpab/phpab)
*
* @link https://github.com/phpab/phpab for the canonical source repository
* @copyright Copyright (c) 2015-2016 phpab. (https://github.com/phpab)
* @license https://raw.githubusercontent.com/phpab/phpab/master/LICENSE MIT
*/

require '../vendor/autoload.php';

use PhpAb\AbRunner;
use PhpAb\AbTest;
use PhpAb\Analytics\AnalyticsInterface;
use PhpAb\Participation\Strategy\StrategyInterface;
use PhpAb\RunnerInterface;
use PhpAb\Storage\CookieStorage;
use PhpAb\TestInterface;

class BrowserStrategy implements StrategyInterface
{
public function isParticipating(AbRunner $runner)
public function isParticipating(RunnerInterface $runner)
{
// Only execute in Chrome.
return strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false;
}
}

class Analytics implements AnalyticsInterface
{
public function registerHit(AbTest $test)
public function registerHit(TestInterface $test)
{
echo 123;
echo 'Registering a new hit for test "' . $test->getName() . '".<br />';
}

public function registerExistingVisitor(AbTest $test, $choice)
public function registerExistingVisitor(TestInterface $test, $choice)
{
echo 123;
echo 'Registering choice "' . $choice . '" in test "' . $test->getname() . '" for existing visitor.<br />';
}

public function registerNewVisitor(AbTest $test, $choice)
public function registerNewVisitor(TestInterface $test, $choice)
{
echo 456;
echo 'Registering choice "' . $choice . '" in test "' . $test->getname() . '" for new visitor.<br />';
}
}

Expand All @@ -42,7 +53,7 @@ public function __construct($percentage)
$this->percentage = $percentage;
}

public function isParticipating(AbRunner $runner)
public function isParticipating(RunnerInterface $runner)
{
$random = mt_rand() / mt_getrandmax();

Expand All @@ -51,14 +62,14 @@ public function isParticipating(AbRunner $runner)
}

$callbackA = function(AbRunner $phpab, AbTest $test, $choice) {
echo __FUNCTION__;
echo 'Executing test A<br />';
};

$callbackB = function(AbRunner $phpab, AbTest $test, $choice) {
echo __FUNCTION__;
echo 'Executing test B<br />';
};

$phpab = new AbRunner(new \PercentageStrategy(0.1));
$phpab = new AbRunner(new \PercentageStrategy(1.0));
$phpab->setAnalytics(new Analytics());
$phpab->setStorage(new CookieStorage('abtest', 3600));
$phpab->addTest(new AbTest('My Test', $callbackA, $callbackB, null));
Expand Down

0 comments on commit 28b4dad

Please sign in to comment.