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
|
|
|
/** |
150
|
|
|
* Fetch the arguments to use with the control callback. |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
public function getControlArguments(): array |
155
|
|
|
{ |
156
|
12 |
|
return $this->controlArguments; |
157
|
|
|
} |
158
|
12 |
|
|
159
|
|
|
/** |
160
|
|
|
* Register a trial callback. |
161
|
|
|
* |
162
|
|
|
* @param string $name |
163
|
|
|
* @param callable $callback |
164
|
|
|
* |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
public function trial($name, callable $callback, $context = null, $arguments = []) |
168
|
1 |
|
{ |
169
|
|
|
$this->trials[$name] = new Trial($name, $callback, $context, $arguments); |
170
|
1 |
|
|
171
|
|
|
return $this; |
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Fetch a trial callback by name. |
176
|
|
|
* |
177
|
|
|
* @param string $name |
178
|
|
|
* |
179
|
|
|
* @return mixed |
180
|
12 |
|
*/ |
181
|
|
|
public function getTrial($name) |
182
|
12 |
|
{ |
183
|
|
|
return $this->trials[$name]->getCallback(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Fetch an array of trial callbacks. |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
public function getTrials() |
192
|
2 |
|
{ |
193
|
|
|
return $this->trials; |
194
|
2 |
|
} |
195
|
|
|
|
196
|
2 |
|
/** |
197
|
|
|
* Set a matcher for this experiment. |
198
|
|
|
* |
199
|
|
|
* @param \Scientist\Matchers\Matcher $matcher |
200
|
|
|
* |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function matcher(Matcher $matcher) |
204
|
1 |
|
{ |
205
|
|
|
$this->matcher = $matcher; |
206
|
1 |
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the matcher for this experiment. |
212
|
|
|
* |
213
|
|
|
* @return \Scientist\Matchers\Matcher |
214
|
6 |
|
*/ |
215
|
|
|
public function getMatcher() |
216
|
6 |
|
{ |
217
|
|
|
return $this->matcher; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Set the execution chance. |
222
|
|
|
* |
223
|
|
|
* @param Chances\Chance $chance |
224
|
13 |
|
* |
225
|
|
|
* @return $this |
226
|
13 |
|
*/ |
227
|
|
|
public function chance(Chance $chance) |
228
|
|
|
{ |
229
|
|
|
$this->chance = $chance; |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
6 |
|
/** |
235
|
|
|
* Get the execution chance. |
236
|
6 |
|
* |
237
|
|
|
* @return Chances\Chance |
238
|
6 |
|
*/ |
239
|
|
|
public function getChance() |
240
|
|
|
{ |
241
|
|
|
return $this->chance; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Determine whether an experiment should run based on chance. |
246
|
3 |
|
* |
247
|
|
|
* @return boolean |
248
|
3 |
|
*/ |
249
|
|
|
public function shouldRun() |
250
|
3 |
|
{ |
251
|
|
|
return $this->chance |
252
|
|
|
->shouldRun(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the experiment parameters. |
257
|
|
|
* |
258
|
|
|
* @return array |
259
|
|
|
*/ |
260
|
|
|
public function getParams() |
261
|
|
|
{ |
262
|
|
|
return $this->params; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Execute the experiment within the laboratory. |
267
|
|
|
* |
268
|
|
|
* @return mixed |
269
|
|
|
*/ |
270
|
|
|
public function run() |
271
|
|
|
{ |
272
|
|
|
$this->params = func_get_args(); |
273
|
|
|
|
274
|
|
|
return $this->laboratory->runExperiment($this); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Execute the experiment and return a report. |
279
|
|
|
* |
280
|
|
|
* @return \Scientist\Report |
281
|
|
|
*/ |
282
|
|
|
public function report() |
283
|
|
|
{ |
284
|
|
|
$this->params = func_get_args(); |
285
|
|
|
|
286
|
|
|
return $this->laboratory->getReport($this); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|