|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scientist; |
|
4
|
|
|
|
|
5
|
|
|
use Scientist\Chances\Chance; |
|
6
|
|
|
use Scientist\Chances\StandardChance; |
|
7
|
|
|
use Scientist\Matchers\Matcher; |
|
8
|
|
|
use Scientist\Matchers\StandardMatcher; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Experiment |
|
12
|
|
|
* |
|
13
|
|
|
* An experiment allows us to implement our code in a new way without |
|
14
|
|
|
* risking the introduction of bugs or regressions. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Scientist |
|
17
|
|
|
*/ |
|
18
|
|
|
class Experiment |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Experiment name. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $name; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The control callback. |
|
29
|
|
|
* |
|
30
|
|
|
* @var callable |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $control; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Context for the control. |
|
36
|
|
|
* |
|
37
|
|
|
* @var mixed |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $controlContext; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Arguments for the control. |
|
43
|
|
|
* |
|
44
|
|
|
* @var mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $controlArguments; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Trial callbacks. |
|
50
|
|
|
* |
|
51
|
|
|
* @var array |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $trials = []; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Parameters for our callbacks. |
|
57
|
|
|
* |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $params = []; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Laboratory instance. |
|
64
|
|
|
* |
|
65
|
|
|
* @var \Scientist\Laboratory |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $laboratory; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Matcher for experiment values. |
|
71
|
|
|
* |
|
72
|
|
|
* @var \Scientist\Matchers\Matcher |
|
73
|
21 |
|
*/ |
|
74
|
|
|
protected $matcher; |
|
75
|
21 |
|
|
|
76
|
21 |
|
/** |
|
77
|
21 |
|
* Execution chance. |
|
78
|
21 |
|
* |
|
79
|
|
|
* @var \Scientist\Chances\Chance |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $chance; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Create a new experiment. |
|
85
|
12 |
|
* |
|
86
|
|
|
* @param string $name |
|
87
|
12 |
|
* @param \Scientist\Laboratory $laboratory |
|
88
|
|
|
*/ |
|
89
|
|
|
public function __construct($name, Laboratory $laboratory) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->name = $name; |
|
92
|
|
|
$this->laboratory = $laboratory; |
|
93
|
|
|
$this->matcher = new StandardMatcher; |
|
94
|
|
|
$this->chance = new StandardChance; |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
/** |
|
98
|
|
|
* Fetch the experiment name. |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getName() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->name; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
14 |
|
/** |
|
108
|
|
|
* Retrieve the laboratory instance. |
|
109
|
14 |
|
* |
|
110
|
|
|
* @return \Scientist\Laboratory|null |
|
111
|
14 |
|
*/ |
|
112
|
|
|
public function getLaboratory() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->laboratory; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Register a control callback. |
|
119
|
14 |
|
* |
|
120
|
|
|
* @param callable $callback |
|
121
|
14 |
|
* @param mixed $context |
|
122
|
|
|
* |
|
123
|
|
|
* @return $this |
|
124
|
|
|
*/ |
|
125
|
|
|
public function control(callable $callback, $context = null, $arguments = []) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->control = $callback; |
|
128
|
|
|
$this->controlContext = $context; |
|
129
|
|
|
$this->controlArguments = $arguments; |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
12 |
|
} |
|
133
|
|
|
|
|
134
|
12 |
|
/** |
|
135
|
|
|
* Fetch the control callback. |
|
136
|
12 |
|
* |
|
137
|
|
|
* @return callable |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getControl() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->control; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function getControlContext() |
|
145
|
|
|
{ |
|
146
|
2 |
|
return $this->controlContext; |
|
147
|
|
|
} |
|
148
|
2 |
|
|
|
149
|
|
|
public function getControlArguments() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->controlArguments; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Register a trial callback. |
|
156
|
12 |
|
* |
|
157
|
|
|
* @param string $name |
|
158
|
12 |
|
* @param callable $callback |
|
159
|
|
|
* |
|
160
|
|
|
* @return $this |
|
161
|
|
|
*/ |
|
162
|
|
|
public function trial($name, callable $callback, $context = null, $arguments = []) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->trials[$name] = new Trial($name, $callback, $context, $arguments); |
|
165
|
|
|
|
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
1 |
|
|
|
169
|
|
|
/** |
|
170
|
1 |
|
* Fetch a trial callback by name. |
|
171
|
|
|
* |
|
172
|
1 |
|
* @param string $name |
|
173
|
|
|
* |
|
174
|
|
|
* @return mixed |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getTrial($name) |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->trials[$name]->getCallback(); |
|
179
|
|
|
} |
|
180
|
12 |
|
|
|
181
|
|
|
/** |
|
182
|
12 |
|
* Fetch an array of trial callbacks. |
|
183
|
|
|
* |
|
184
|
|
|
* @return array |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getTrials() |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->trials; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
2 |
|
* Set a matcher for this experiment. |
|
193
|
|
|
* |
|
194
|
2 |
|
* @param \Scientist\Matchers\Matcher $matcher |
|
195
|
|
|
* |
|
196
|
2 |
|
* @return $this |
|
197
|
|
|
*/ |
|
198
|
|
|
public function matcher(Matcher $matcher) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->matcher = $matcher; |
|
201
|
|
|
|
|
202
|
|
|
return $this; |
|
203
|
|
|
} |
|
204
|
1 |
|
|
|
205
|
|
|
/** |
|
206
|
1 |
|
* Get the matcher for this experiment. |
|
207
|
|
|
* |
|
208
|
|
|
* @return \Scientist\Matchers\Matcher |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getMatcher() |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->matcher; |
|
213
|
|
|
} |
|
214
|
6 |
|
|
|
215
|
|
|
/** |
|
216
|
6 |
|
* Set the execution chance. |
|
217
|
|
|
* |
|
218
|
|
|
* @param Chances\Chance $chance |
|
219
|
|
|
* |
|
220
|
|
|
* @return $this |
|
221
|
|
|
*/ |
|
222
|
|
|
public function chance(Chance $chance) |
|
223
|
|
|
{ |
|
224
|
13 |
|
$this->chance = $chance; |
|
225
|
|
|
|
|
226
|
13 |
|
return $this; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Get the execution chance. |
|
231
|
|
|
* |
|
232
|
|
|
* @return Chances\Chance |
|
233
|
|
|
*/ |
|
234
|
6 |
|
public function getChance() |
|
235
|
|
|
{ |
|
236
|
6 |
|
return $this->chance; |
|
237
|
|
|
} |
|
238
|
6 |
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Determine whether an experiment should run based on chance. |
|
241
|
|
|
* |
|
242
|
|
|
* @return boolean |
|
243
|
|
|
*/ |
|
244
|
|
|
public function shouldRun() |
|
245
|
|
|
{ |
|
246
|
3 |
|
return $this->chance |
|
247
|
|
|
->shouldRun(); |
|
248
|
3 |
|
} |
|
249
|
|
|
|
|
250
|
3 |
|
/** |
|
251
|
|
|
* Get the experiment parameters. |
|
252
|
|
|
* |
|
253
|
|
|
* @return array |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getParams() |
|
256
|
|
|
{ |
|
257
|
|
|
return $this->params; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Execute the experiment within the laboratory. |
|
262
|
|
|
* |
|
263
|
|
|
* @return mixed |
|
264
|
|
|
*/ |
|
265
|
|
|
public function run() |
|
266
|
|
|
{ |
|
267
|
|
|
$this->params = func_get_args(); |
|
268
|
|
|
|
|
269
|
|
|
return $this->laboratory->runExperiment($this); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Execute the experiment and return a report. |
|
274
|
|
|
* |
|
275
|
|
|
* @return \Scientist\Report |
|
276
|
|
|
*/ |
|
277
|
|
|
public function report() |
|
278
|
|
|
{ |
|
279
|
|
|
$this->params = func_get_args(); |
|
280
|
|
|
|
|
281
|
|
|
return $this->laboratory->getReport($this); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|