Skip to content

Commit fb45e4f

Browse files
authored
Merge pull request #23 from owsy/metadata
Control/Trial Metadata
2 parents d89a8a8 + f99fa27 commit fb45e4f

File tree

8 files changed

+131
-16
lines changed

8 files changed

+131
-16
lines changed

src/Experiment.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ class Experiment
3131
*/
3232
protected $control;
3333

34+
/**
35+
* Context for the control.
36+
*
37+
* @var mixed
38+
*/
39+
protected $controlContext;
40+
3441
/**
3542
* Trial callbacks.
3643
*
@@ -104,12 +111,14 @@ public function getLaboratory()
104111
* Register a control callback.
105112
*
106113
* @param callable $callback
114+
* @param mixed $context
107115
*
108116
* @return $this
109117
*/
110-
public function control(callable $callback)
118+
public function control(callable $callback, $context = null)
111119
{
112120
$this->control = $callback;
121+
$this->controlContext = $context;
113122

114123
return $this;
115124
}
@@ -124,6 +133,11 @@ public function getControl()
124133
return $this->control;
125134
}
126135

136+
public function getControlContext()
137+
{
138+
return $this->controlContext;
139+
}
140+
127141
/**
128142
* Register a trial callback.
129143
*
@@ -132,9 +146,9 @@ public function getControl()
132146
*
133147
* @return $this
134148
*/
135-
public function trial($name, callable $callback)
149+
public function trial($name, callable $callback, $context = null)
136150
{
137-
$this->trials[$name] = $callback;
151+
$this->trials[$name] = new Trial($name, $callback, $context);
138152

139153
return $this;
140154
}
@@ -148,7 +162,7 @@ public function trial($name, callable $callback)
148162
*/
149163
public function getTrial($name)
150164
{
151-
return $this->trials[$name];
165+
return $this->trials[$name]->getCallback();
152166
}
153167

154168
/**

src/Intern.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ public function run(Experiment $experiment)
4040
*/
4141
protected function runControl(Experiment $experiment)
4242
{
43-
return (new Machine($experiment->getControl(), $experiment->getParams()))->execute();
43+
return (new Machine(
44+
$experiment->getControl(),
45+
$experiment->getParams(),
46+
false,
47+
$experiment->getControlContext()
48+
))->execute();
4449
}
4550

4651
/**
@@ -56,9 +61,10 @@ protected function runTrials(Experiment $experiment)
5661

5762
foreach ($experiment->getTrials() as $name => $trial) {
5863
$executions[$name] = (new Machine(
59-
$trial,
64+
$trial->getCallback(),
6065
$experiment->getParams(),
61-
true
66+
true,
67+
$trial->getContext()
6268
))->execute();
6369
}
6470

src/Machine.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ class Machine
4646
* @param array $params
4747
* @param boolean $muted
4848
*/
49-
public function __construct(callable $callback, array $params = [], $muted = false)
49+
public function __construct(callable $callback, array $params = [], $muted = false, $context = null)
5050
{
5151
$this->callback = $callback;
5252
$this->params = $params;
5353
$this->muted = $muted;
54-
$this->result = new Result;
54+
$this->result = new Result($context);
5555
}
5656

5757
/**

src/Result.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Scientist;
43

54
use Exception;
@@ -63,6 +62,16 @@ class Result
6362
*/
6463
protected $match = false;
6564

65+
/**
66+
* @var mixed
67+
*/
68+
protected $context;
69+
70+
public function __construct($context = null)
71+
{
72+
$this->context = $context;
73+
}
74+
6675
/**
6776
* Get the callback result value.
6877
*
@@ -227,6 +236,11 @@ public function setException($exception)
227236
return $this;
228237
}
229238

239+
public function getContext()
240+
{
241+
return $this->context;
242+
}
243+
230244
/**
231245
* Determine whether the callback result matches the control.
232246
*

src/Trial.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Scientist;
4+
5+
class Trial
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $name;
11+
12+
/**
13+
* @var callable
14+
*/
15+
protected $callback;
16+
17+
/**
18+
* @var mixed
19+
*/
20+
protected $context;
21+
22+
public function __construct($name, callable $callback, $context)
23+
{
24+
$this->name = $name;
25+
$this->callback = $callback;
26+
$this->context = $context;
27+
}
28+
29+
public function getName()
30+
{
31+
return $this->name;
32+
}
33+
34+
public function getCallback()
35+
{
36+
return $this->callback;
37+
}
38+
39+
public function getContext()
40+
{
41+
return $this->context;
42+
}
43+
}

tests/ExperimentTest.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ public function test_that_a_control_callback_can_be_defined()
2828
$this->assertSame($control, $e->getControl());
2929
}
3030

31+
public function test_that_control_context_defaults_to_null()
32+
{
33+
$e = new Experiment('test experiment', new Laboratory);
34+
$e->control(function () {
35+
return true;
36+
});
37+
$this->assertNull($e->getControlContext());
38+
}
39+
40+
public function test_that_control_context_can_be_defined()
41+
{
42+
$context = ['foo' => 'bar'];
43+
$e = new Experiment('test experiment', new Laboratory);
44+
$e->control(function () {
45+
return true;
46+
}, $context);
47+
$this->assertSame($context, $e->getControlContext());
48+
}
49+
3150
public function test_that_a_trial_callback_can_be_defined()
3251
{
3352
$e = new Experiment('test experiment', new Laboratory);
@@ -54,11 +73,13 @@ public function test_that_multiple_trial_callbacks_can_be_defined()
5473
$e->trial('second', $second);
5574
$e->trial('third', $third);
5675
$expected = [
57-
'first' => $first,
58-
'second' => $second,
59-
'third' => $third
76+
'first',
77+
'second',
78+
'third',
6079
];
61-
$this->assertSame($expected, $e->getTrials());
80+
$trials = $e->getTrials();
81+
$this->assertSame($expected, \array_keys($trials));
82+
$this->assertContainsOnlyInstancesOf(\Scientist\Trial::class, $trials);
6283
}
6384

6485
public function test_that_a_chance_variable_can_be_set()

tests/MachineTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class MachineTest extends \PHPUnit\Framework\TestCase
88
public function test_that_machine_can_be_created()
99
{
1010
$m = new Machine(function () {});
11-
11+
1212
$this->assertInstanceOf(Machine::class, $m);
1313
}
1414

@@ -40,6 +40,15 @@ public function test_that_machine_determines_callback_result_value()
4040
$this->assertEquals('foo', $m->execute()->getValue());
4141
}
4242

43+
public function test_that_machine_sets_context_result()
44+
{
45+
$context = ['foo' => 'bar'];
46+
47+
$m = new Machine(function () { return 'foo'; }, [], true, $context);
48+
49+
$this->assertSame($context, $m->execute()->getContext());
50+
}
51+
4352
public function test_that_machine_executes_callback_with_parameters()
4453
{
4554
$m = new Machine(function ($one, $two, $three) {

tests/ResultTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ResultTest extends \PHPUnit\Framework\TestCase
77
public function test_result_can_be_created()
88
{
99
$r = new Result;
10-
10+
1111
$this->assertInstanceOf(Result::class, $r);
1212
}
1313

@@ -60,6 +60,14 @@ public function test_result_can_have_match_status()
6060
$this->assertTrue(true, $r->isMatch());
6161
}
6262

63+
public function test_can_have_context()
64+
{
65+
$context = ['foo' => 'bar'];
66+
67+
$r = new Result($context);
68+
$this->assertSame($context, $r->getContext());
69+
}
70+
6371
public function test_result_can_have_total_execution_time()
6472
{
6573
$r = new Result;

0 commit comments

Comments
 (0)