-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathExperimentTest.php
139 lines (123 loc) · 4.19 KB
/
ExperimentTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
use Scientist\Experiment;
use Scientist\Laboratory;
use Scientist\Matchers\StandardMatcher;
class ExperimentTest extends \PHPUnit\Framework\TestCase
{
public function test_that_a_new_experiment_can_be_created()
{
$e = new Experiment('test experiment', new Laboratory);
$this->assertInstanceOf(Experiment::class, $e);
}
public function test_that_experiment_name_is_set()
{
$e = new Experiment('test experiment', new Laboratory);
$this->assertEquals('test experiment', $e->getName());
}
public function test_that_a_control_callback_can_be_defined()
{
$e = new Experiment('test experiment', new Laboratory);
$control = function () {
return true;
};
$e->control($control);
$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);
$trial = function () {
return true;
};
$e->trial('trial', $trial);
$this->assertSame($trial, $e->getTrial('trial'));
}
public function test_that_multiple_trial_callbacks_can_be_defined()
{
$e = new Experiment('test experiment', new Laboratory);
$first = function () {
return 'first';
};
$second = function () {
return 'second';
};
$third = function () {
return 'third';
};
$e->trial('first', $first);
$e->trial('second', $second);
$e->trial('third', $third);
$expected = [
'first',
'second',
'third',
];
$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()
{
$chance = $this->createMock('\Scientist\Chances\Chance');
$e = new Experiment('test experiment', new Laboratory);
$e->chance($chance);
$this->assertEquals($chance, $e->getChance());
}
public function test_that_an_experiment_matcher_can_be_set()
{
$e = new Experiment('test experiment', new Laboratory);
$e->matcher(new StandardMatcher);
$this->assertInstanceOf(StandardMatcher::class, $e->getMatcher());
}
public function test_that_an_experiment_laboratory_can_be_set()
{
$l = new Laboratory;
$e = new Experiment('test experiment', $l);
$this->assertInstanceOf(Laboratory::class, $e->getLaboratory());
$this->assertSame($l, $e->getLaboratory());
}
public function test_that_running_experiment_with_no_laboratory_executes_control()
{
$e = new Experiment('test experiment', new Laboratory);
$e->control(function () { return 'foo'; });
$v = $e->run();
$this->assertEquals('foo', $v);
}
public function test_that_running_experiment_with_zero_chance_executes_control()
{
$chance = $this->getMockChance();
$chance->expects($this->once())
->method('shouldRun')
->willReturn(false);
$l = new Laboratory;
$v = $l->experiment('test experiment')
->control(function () { return 'foo'; })
->chance($chance)
->run();
$this->assertEquals('foo', $v);
}
public function getMockChance()
{
return $this->getMockBuilder('\Scientist\Chances\Chance')
->disableOriginalConstructor()
->disableProxyingToOriginalMethods()
->getMock();
}
}