-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fixturenator.php
380 lines (340 loc) · 11.9 KB
/
Fixturenator.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 syntax=php: */
class Fixturenator
{
protected static $factories = array();
protected static $sequences = array();
// options are FixturenatorDefinition::OPT_*
public static function define($name, $data = array(), $options = array())
{
// wire up parent
if (isset($options[FixturenatorDefinition::OPT_PARENT]))
{
if (!isset(self::$factories[$options[FixturenatorDefinition::OPT_PARENT]])) throw new Exception("No FixturenatorDefinition for {$options[FixturenatorDefinition::OPT_PARENT]}.");
$options[FixturenatorDefinition::OPT_PARENT] = self::$factories[$options[FixturenatorDefinition::OPT_PARENT]];
}
if (isset(self::$factories[$name])) throw new Exception("A factory named {$name} has already been defined.");
self::$factories[$name] = new FixturenatorDefinition($name, $data, $options);
}
public static function createSequence($name, $sequenceProcessor = NULL)
{
if (isset(self::$sequences[$name])) throw new Exception("A sequence named {$name} has already been defined.");
$s = new FixturenatorSequence($sequenceProcessor);
self::$sequences[$name] = $s;
}
public static function getSequence($name)
{
if (!isset(self::$sequences[$name])) throw new Exception("A sequence named {$name} has not been defined.");
return self::$sequences[$name];
}
public static function nextSequence($name)
{
return self::getSequence($name)->next();
}
public static function clearFactories()
{
self::$factories = array();
}
public static function clearSequences()
{
self::$sequences = array();
}
private static function requireFactoryNamed($name)
{
if (!isset(self::$factories[$name])) throw new Exception("A factory named {$name} has not been defined.");
return self::$factories[$name];
}
public static function create($name, $data = array())
{
return self::requireFactoryNamed($name)->create($data);
}
/**
* Helper function to call "saved" on the named FixturenatorDefinition.
*
* @param string THe FixturenatorDefinition name (must be previously defined).
* @param array A hash of data to override the default data.
* @param mixed Any additional parameters will be passed along as to the "save method".
* @return object Whatever
* @throws object Exception
*/
public static function saved($name, $data = array())
{
$saveMethodArgs = array();
if (func_num_args() > 2)
{
$passedArgs = func_get_args();
$saveMethodArgs = array_slice($passedArgs, 2);
}
$allArgs = array_merge(array($data), $saveMethodArgs);
return call_user_func_array(array(self::requireFactoryNamed($name), 'saved'), $allArgs);
}
public static function stub($name, $data = array())
{
return self::requireFactoryNamed($name)->stub($data);
}
// maybe call this "asArray"? "toArray"?
public static function asArray($name, $data = array())
{
return self::requireFactoryNamed($name)->asArray($data);
}
}
class FixturenatorDefinition
{
protected $name;
protected $class;
protected $valueGenerators;
protected $saveMethod;
protected $saveMethodArgs;
protected $parent;
const OPT_CLASS = 'class';
const OPT_PARENT = 'parent';
const OPT_SAVE_METHOD = 'saveMethod';
const OPT_SAVE_METHOD_ARGS = 'saveMethodArgs';
public function __construct($name, $valueGenerators = array(), $options = array())
{
$this->name = $name;
$this->valueGenerators = array();
$this->parent = NULL;
if (is_callable($valueGenerators)) // allow a passed block to dynamically add generators to FixturenatorDefinition
{
$valueGenerators($this);
}
else // generate a "static" generator
{
foreach ($valueGenerators as $k => $v) {
$this->prepareAttributeGenerator($k, $v);
}
}
foreach (array_merge(array(
self::OPT_CLASS => $name,
self::OPT_PARENT => NULL,
self::OPT_SAVE_METHOD => 'save',
self::OPT_SAVE_METHOD_ARGS => array(),
), $options) as $k => $v) {
$this->$k = $v;
}
if ($this->parent)
{
if (!($this->parent instanceof FixturenatorDefinition)) throw new Exception("OPT_PARENT must be a FixturenatorDefinition object.");
$this->class = $this->parent->class;
}
}
private function prepareAttributeGenerator($k, $v)
{
if ($v instanceof FixturenatorGenerator)
{
$this->valueGenerators[$k] = $v;
}
else // static data
{
$this->valueGenerators[$k] = new FixturenatorGenerator($v);
}
}
/**
* Magic-ness for when new'ing a FixturenatorDefinition with a block, so that you can:
*
* <code>
* $f->attr = 'foo';
* $f->attr = new FixturenatorGenerator(function($o) { return rand(1000,9999); } );
* $f->attr = new FixturenatorSequence;
* </code>
*
* inside the block.
*
* Internally this just calls "prepareAttributeGenerator" with the info.
*
* @param string Key
* @param mixed Value
*/
public function __set($k, $v)
{
$this->prepareAttributeGenerator($k, $v);
}
public function resolveData($newObj, $overrideData = array())
{
// resolve inheritance
if ($this->parent)
{
$this->parent->resolveData($newObj);
}
// wire up data for this factory
// NOTE: if there is override data that is dynamic (closure), and it relies on the value from another field, the order
// adding setters in the FixturenatorDefinition and overridedata matters!
// EX: username = X, pass = {usermame}1234 only works if you do it in username, password order.
// for any key in overrideData or valueGenerators, need to process it...
$allKeys = array_unique(array_merge(array_keys($this->valueGenerators), array_keys($overrideData)));
foreach ($allKeys as $k) {
// figure out which value to use
$value = NULL;
if (array_key_exists($k, $overrideData))
{
$value = $overrideData[$k];
if (is_callable($value))
{
$value = call_user_func($value, $newObj);
}
else if (is_string($value) && FixturenatorDefinition::__detectLambda($value, '$o'))
{
$lambda = create_function('$o', $value);
$value = $lambda($newObj);
}
}
else
{
$generator = $this->valueGenerators[$k];
if ($generator instanceof FixturenatorGenerator)
{
$value = $generator->next($newObj);
}
else if (is_callable($generator))
{
$value = $generator($newObj);
}
}
// set value
if (is_callable(array($newObj, 'setValueForKey'))) // KVC support
{
$newObj->setValueForKey($value, $k);
}
else // emulated setValueForKey
{
$performed = false;
// try calling setter
$setMethod = "set" . ucfirst($k);
if (method_exists($newObj, $setMethod))
{
$newObj->$setMethod($value);
$performed = true;
}
if (!$performed)
{
// try accesing instance var directly
$vars = get_object_vars($newObj);
if (array_key_exists($k, $vars))
{
$newObj->$k = $value;
$performed = true;
}
}
if (!$performed) throw new Exception("Couldn't manage to set '$k' for object '" . get_class($newObj) . "'.");
}
}
}
public function create($overrideData = array())
{
$newObj = new $this->class;
$this->resolveData($newObj, $overrideData);
return $newObj;
}
/**
* Return a saved object.
*
* @param array A hash of data to override the default data.
* @param mixed Any additional parameters will be passed along as to the "save method".
* @return object Whatever An instance of the new object.
* @throws object Exception If the save method is not callable.
*/
public function saved($overrideData = array())
{
$saveMethodArgs = $this->saveMethodArgs;
if (func_num_args() > 1)
{
$passedArgs = func_get_args();
$saveMethodArgs = array_slice($passedArgs, 1);
}
$newObj = $this->create($overrideData);
call_user_func_array(array($newObj, $this->saveMethod), $saveMethodArgs);
return $newObj;
}
public function stub($overrideData = array())
{
$newObj = new MagicArray;
$this->resolveData($newObj, $overrideData);
return $newObj;
}
public function asArray($overrideData = array())
{
return $this->stub($overrideData)->getArrayCopy();
}
public static function __detectLambda($possibleLambda, $expectedVarName)
{
if (!is_string($possibleLambda)) return false;
if (strncasecmp('return', $possibleLambda, 6) === 0) return true;
if (strpos($possibleLambda, "\${$expectedVarName}") !== false) return true;
return false;
}
}
class FixturenatorGenerator
{
public $generator;
public function __construct($value)
{
if (is_string($value) && FixturenatorDefinition::__detectLambda($value, '$o'))
{
$this->generator = create_function('$o', $value);
}
else
{
$this->generator = $value;
}
}
public function next($obj = NULL)
{
if (is_callable($this->generator))
{
$genF = $this->generator;
$result = call_user_func($genF, $obj);
unset($genF);
return $result;
}
else
{
return $this->generator; // static values
}
}
}
class FixturenatorSequence extends FixturenatorGenerator
{
protected $val;
protected $sequenceProcessor;
public function __construct($sequenceProcessor = NULL)
{
$this->val = 1;
$this->sequenceProcessor = NULL;
if (is_callable($sequenceProcessor))
{
$this->sequenceProcessor = $sequenceProcessor;
}
else if (is_string($sequenceProcessor) && FixturenatorDefinition::__detectLambda($sequenceProcessor, '$n') !== false)
{
$this->sequenceProcessor = create_function('$n', $sequenceProcessor);
}
else if ($sequenceProcessor !== NULL)
{
throw new Exception("Someting besides a function was passed.");
}
parent::__construct(array($this, 'nextVal'));
}
public function nextVal()
{
$nextVal = $this->val++;
if ($this->sequenceProcessor)
{
$sequenceProcessorF = $this->sequenceProcessor;
$nextVal = call_user_func($sequenceProcessorF, $nextVal);
unset($sequenceProcessorF);
}
return $nextVal;
}
}
class MagicArray extends ArrayObject
{
public function setValueForKey($v, $k)
{
$this->offsetSet($k, $v);
}
public function __get($k)
{
return $this->offsetGet($k);
}
}