Skip to content

Commit

Permalink
Merge pull request #23 from owsy/metadata
Browse files Browse the repository at this point in the history
Control/Trial Metadata
  • Loading branch information
daylerees authored Apr 7, 2018
2 parents d89a8a8 + f99fa27 commit fb45e4f
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 16 deletions.
22 changes: 18 additions & 4 deletions src/Experiment.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class Experiment
*/
protected $control;

/**
* Context for the control.
*
* @var mixed
*/
protected $controlContext;

/**
* Trial callbacks.
*
Expand Down Expand Up @@ -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;
}
Expand All @@ -124,6 +133,11 @@ public function getControl()
return $this->control;
}

public function getControlContext()
{
return $this->controlContext;
}

/**
* Register a trial callback.
*
Expand All @@ -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;
}
Expand All @@ -148,7 +162,7 @@ public function trial($name, callable $callback)
*/
public function getTrial($name)
{
return $this->trials[$name];
return $this->trials[$name]->getCallback();
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/Intern.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Machine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/Result.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace Scientist;

use Exception;
Expand Down Expand Up @@ -63,6 +62,16 @@ class Result
*/
protected $match = false;

/**
* @var mixed
*/
protected $context;

public function __construct($context = null)
{
$this->context = $context;
}

/**
* Get the callback result value.
*
Expand Down Expand Up @@ -227,6 +236,11 @@ public function setException($exception)
return $this;
}

public function getContext()
{
return $this->context;
}

/**
* Determine whether the callback result matches the control.
*
Expand Down
43 changes: 43 additions & 0 deletions src/Trial.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Scientist;

class Trial
{
/**
* @var string
*/
protected $name;

/**
* @var callable
*/
protected $callback;

/**
* @var mixed
*/
protected $context;

public function __construct($name, callable $callback, $context)
{
$this->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;
}
}
29 changes: 25 additions & 4 deletions tests/ExperimentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()
Expand Down
11 changes: 10 additions & 1 deletion tests/MachineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion tests/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fb45e4f

Please sign in to comment.