diff --git a/src/Experiment.php b/src/Experiment.php index 3ae831f..741a1d1 100644 --- a/src/Experiment.php +++ b/src/Experiment.php @@ -31,6 +31,13 @@ class Experiment */ protected $control; + /** + * Context for the control. + * + * @var mixed + */ + protected $controlContext; + /** * Trial callbacks. * @@ -104,12 +111,14 @@ public function getLaboratory() * Register a control callback. * * @param callable $callback + * @param mixed $context * * @return $this */ - public function control(callable $callback) + public function control(callable $callback, $context = null) { $this->control = $callback; + $this->controlContext = $context; return $this; } @@ -124,6 +133,11 @@ public function getControl() return $this->control; } + public function getControlContext() + { + return $this->controlContext; + } + /** * Register a trial callback. * @@ -132,9 +146,9 @@ public function getControl() * * @return $this */ - public function trial($name, callable $callback) + public function trial($name, callable $callback, $context = null) { - $this->trials[$name] = $callback; + $this->trials[$name] = new Trial($name, $callback, $context); return $this; } @@ -148,7 +162,7 @@ public function trial($name, callable $callback) */ public function getTrial($name) { - return $this->trials[$name]; + return $this->trials[$name]->getCallback(); } /** diff --git a/src/Intern.php b/src/Intern.php index 9200085..44091b4 100644 --- a/src/Intern.php +++ b/src/Intern.php @@ -40,7 +40,12 @@ public function run(Experiment $experiment) */ protected function runControl(Experiment $experiment) { - return (new Machine($experiment->getControl(), $experiment->getParams()))->execute(); + return (new Machine( + $experiment->getControl(), + $experiment->getParams(), + false, + $experiment->getControlContext() + ))->execute(); } /** @@ -56,9 +61,10 @@ protected function runTrials(Experiment $experiment) foreach ($experiment->getTrials() as $name => $trial) { $executions[$name] = (new Machine( - $trial, + $trial->getCallback(), $experiment->getParams(), - true + true, + $trial->getContext() ))->execute(); } diff --git a/src/Machine.php b/src/Machine.php index c3d0358..8277b3a 100644 --- a/src/Machine.php +++ b/src/Machine.php @@ -46,12 +46,12 @@ class Machine * @param array $params * @param boolean $muted */ - public function __construct(callable $callback, array $params = [], $muted = false) + public function __construct(callable $callback, array $params = [], $muted = false, $context = null) { $this->callback = $callback; $this->params = $params; $this->muted = $muted; - $this->result = new Result; + $this->result = new Result($context); } /** diff --git a/src/Result.php b/src/Result.php index d7b1105..6e78329 100644 --- a/src/Result.php +++ b/src/Result.php @@ -1,5 +1,4 @@ context = $context; + } + /** * Get the callback result value. * @@ -227,6 +236,11 @@ public function setException($exception) return $this; } + public function getContext() + { + return $this->context; + } + /** * Determine whether the callback result matches the control. * diff --git a/src/Trial.php b/src/Trial.php new file mode 100644 index 0000000..c553687 --- /dev/null +++ b/src/Trial.php @@ -0,0 +1,43 @@ +name = $name; + $this->callback = $callback; + $this->context = $context; + } + + public function getName() + { + return $this->name; + } + + public function getCallback() + { + return $this->callback; + } + + public function getContext() + { + return $this->context; + } +} diff --git a/tests/ExperimentTest.php b/tests/ExperimentTest.php index 1fa064e..6ff9692 100644 --- a/tests/ExperimentTest.php +++ b/tests/ExperimentTest.php @@ -28,6 +28,25 @@ public function test_that_a_control_callback_can_be_defined() $this->assertSame($control, $e->getControl()); } + public function test_that_control_context_defaults_to_null() + { + $e = new Experiment('test experiment', new Laboratory); + $e->control(function () { + return true; + }); + $this->assertNull($e->getControlContext()); + } + + public function test_that_control_context_can_be_defined() + { + $context = ['foo' => 'bar']; + $e = new Experiment('test experiment', new Laboratory); + $e->control(function () { + return true; + }, $context); + $this->assertSame($context, $e->getControlContext()); + } + public function test_that_a_trial_callback_can_be_defined() { $e = new Experiment('test experiment', new Laboratory); @@ -54,11 +73,13 @@ public function test_that_multiple_trial_callbacks_can_be_defined() $e->trial('second', $second); $e->trial('third', $third); $expected = [ - 'first' => $first, - 'second' => $second, - 'third' => $third + 'first', + 'second', + 'third', ]; - $this->assertSame($expected, $e->getTrials()); + $trials = $e->getTrials(); + $this->assertSame($expected, \array_keys($trials)); + $this->assertContainsOnlyInstancesOf(\Scientist\Trial::class, $trials); } public function test_that_a_chance_variable_can_be_set() diff --git a/tests/MachineTest.php b/tests/MachineTest.php index 7b80c85..40d9425 100644 --- a/tests/MachineTest.php +++ b/tests/MachineTest.php @@ -8,7 +8,7 @@ class MachineTest extends \PHPUnit\Framework\TestCase public function test_that_machine_can_be_created() { $m = new Machine(function () {}); - + $this->assertInstanceOf(Machine::class, $m); } @@ -40,6 +40,15 @@ public function test_that_machine_determines_callback_result_value() $this->assertEquals('foo', $m->execute()->getValue()); } + public function test_that_machine_sets_context_result() + { + $context = ['foo' => 'bar']; + + $m = new Machine(function () { return 'foo'; }, [], true, $context); + + $this->assertSame($context, $m->execute()->getContext()); + } + public function test_that_machine_executes_callback_with_parameters() { $m = new Machine(function ($one, $two, $three) { diff --git a/tests/ResultTest.php b/tests/ResultTest.php index 7fb3335..18bdea4 100644 --- a/tests/ResultTest.php +++ b/tests/ResultTest.php @@ -7,7 +7,7 @@ class ResultTest extends \PHPUnit\Framework\TestCase public function test_result_can_be_created() { $r = new Result; - + $this->assertInstanceOf(Result::class, $r); } @@ -60,6 +60,14 @@ public function test_result_can_have_match_status() $this->assertTrue(true, $r->isMatch()); } + public function test_can_have_context() + { + $context = ['foo' => 'bar']; + + $r = new Result($context); + $this->assertSame($context, $r->getContext()); + } + public function test_result_can_have_total_execution_time() { $r = new Result;