Permalink
Browse files

let's roll

  • Loading branch information...
BaylorRae committed Apr 26, 2012
0 parents commit ad1a468627e856d2687f01debd6f61699de6e384
Showing with 166 additions and 0 deletions.
  1. BIN .DS_Store
  2. +44 −0 README.md
  3. +28 −0 lib/split.php
  4. +15 −0 lib/split/alternative.php
  5. +45 −0 lib/split/experiment.php
  6. +34 −0 test/split_experiment_test.php
BIN .DS_Store
Binary file not shown.
@@ -0,0 +1,44 @@
+## Syntax
+
+```php
+<?php
+
+if( ab_test('show_message') ) {
+
+}
+
+$cart_title = ab_test('basket_title', 'Shopping Cart', 'Basket', 'Cart'); // Basket
+
+ab_test('name', 'option 1', 'option 2', function($text) {
+ echo $text;
+});
+
+// alias to Split\Experiment
+function ab_test($name, $alternative_names = null) {
+ return Split\Experiment($title, array_shift(func_get_args()));
+}
+
+// split/experiment.php
+
+namespace Split;
+
+class Experiment {
+
+ public $name,
+ $winner;
+
+ private $alternatives;
+
+ function __construct($name, array $alternative_names = null) {
+ $this->name = $name;
+
+ // store each alternative into its own class
+ foreach( $alternative_names as $alternative_name ) {
+ array_push($this->alternatives, new Alternative($alternative_name, $name));
+ }
+ }
+
+}
+
+?>
+```
@@ -0,0 +1,28 @@
+<?php
+
+spl_autoload_register(array('Split', 'autoload'));
+
+class Split {
+
+ public static function autoload($class_name) {
+ if( substr($class_name, 0, 5) === 'Split' ) {
+
+ $file = dirname(__FILE__) . '/' . strtolower(str_replace('\\', '/', $class_name)) . '.php';
+ if( file_exists($file) ) {
+ require_once $file;
+ }
+ }
+ }
+
+}
+
+function ab_test($name, $alternative_names = null) {
+ $alternatives = func_get_args();
+ array_shift($alternatives);
+
+ $callable = end($alternatives);
+ reset($alternatives);
+
+ $experiment = new Split\Experiment($name, $alternatives, $callable);
+ return $experiment->get_winner();
+}
@@ -0,0 +1,15 @@
+<?php
+
+namespace Split;
+
+class Alternative {
+
+ public $name,
+ $experiment_name;
+
+ function __construct($name, $experiment_name) {
+ $this->name = $name;
+ $this->experiment_name = $experiment_name;
+ }
+
+}
@@ -0,0 +1,45 @@
+<?php
+
+namespace Split;
+
+class Experiment {
+
+ public $name,
+ $winner;
+
+ private $alternatives = array();
+ private $callable;
+
+ function __construct($name, array $alternative_names = null, $callable = null) {
+ $this->name = $name;
+
+ if( is_array($alternative_names) ) {
+ foreach( $alternative_names as $alternative_name ) {
+ array_push($this->alternatives, new Alternative($alternative_name, $name));
+ }
+ }
+
+ if( is_callable($callable) ) {
+ $this->callable = $callable;
+ }
+ }
+
+ public function get_winner() {
+
+ if( empty($this->alternatives) ) {
+ $winner = (boolean) mt_rand(0, 1);
+ }else {
+ $key = array_rand($this->alternatives);
+ $winner = $this->alternatives[$key]->name;
+ }
+
+ $this->winner = $winner;
+
+ if( isset($this->callable) ) {
+ call_user_func_array($this->callable, array($this->winner));
+ }else {
+ return $this->winner;
+ }
+ }
+
+}
@@ -0,0 +1,34 @@
+<?php
+
+require dirname(__FILE__) . '/../lib/split.php';
+
+class SplitExperimentTest extends PHPUnit_Framework_TestCase {
+
+ /**
+ * @test
+ */
+ public function it_should_return_boolean_value_if_not_alternative_names() {
+ $this->assertInternalType('bool', ab_test('test_name'));
+ }
+
+ /**
+ * @test
+ */
+ public function it_should_return_a_random_choice() {
+ $this->assertRegExp('/^(shopping cart|basket)$/', ab_test('cart_title', 'shopping cart', 'basket'));
+ }
+
+ /**
+ * @test
+ */
+ public function it_should_run_a_callable_function() {
+ $return_value = null;
+
+ ab_test('cart_title', 'shopping cart', 'basket', function($cart_title) use(&$return_value) {
+ $return_value = 'basket';
+ });
+
+ $this->assertRegExp('/^(shopping cart|basket)$/', $return_value);
+ }
+
+}

0 comments on commit ad1a468

Please sign in to comment.