/
fFixture.php
734 lines (549 loc) · 22.7 KB
/
fFixture.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
<?php
/**
* Provides an easy way of creating sample data (fixtures) for unit testing.
*
* The class is intened to use as an easy way to build test data for unit testing. Data that is created and torn down after testing is complete.
* In other words, it does not handle updates or in any other way handle situations where the database has existing data.
*
* The fixtures, though JSON, will first be evalutated as PHP scripts, so you can included <?php echo data('Y-m-d H:i:s); ?> into you json files.
*
* If you use namespaces, either make sure that you have included the namespaced classes before running fFixture::create(). Alternativly you can use
* the fORM::mapClassToTable() method:
*
* fORM::mapClassToTable('My\Namespaced\Class', 'classes');
*
* Limitations:
* - fFixture does not handle multiplie cases where you have records split over several database. It will either use the default ORM database or the
* one you supply using setDatabase().
* - Only table dependency is supported and only at one level.
* - Only relational validation (foreign key checks) is done when invoking validate(). The rest is up to Flourish at build time.
*
* Future:
* - Implement auto-fill mode (missing properties, dependencies, etc.)
* - Create records using partial specifications together with or in place of fixture files.
* - Handle selfreferencing patterns like, trees.
* - Support for complex primary keys
* - Helper method to easily work references in relationships. (eg. page uses [column layout])
* - Other input source (eg. string, array)
*
* @author Michael Bøcker-Larsen mbl@codeboutiuqe.com
*
*/
class fFixture
{
const PreSetBuildHook = 'pre-set::build()';
static private $database;
static private $verbose;
static private $glabal_hook_callbacks = array();
/**
* Return a new fFixture instance after parsing the json files found in either $root or $replacements_root.
*
* Only fixtures in $white_list and their dependencies will be created. Leave $white_list empty to build everything.
*
* Replacements root is useful in cases where you need very specific records. You could include these fixtures with the
* unit tests.
*
* @param $root
* The root of the fixture directory.
* @param $white_list
* An array of fixture names to include when building. NULL or empty means that every fixture found will be build.
* @param $replacements_root
*
*/
static public function create($root, $white_list = NULL, $replacements_root = NULL)
{
$db = self::$database;
if (is_null($db)) {
throw new fEnvironmentException('Database not set');
}
$fixture = new self($root, $db, $white_list, $replacements_root);
return $fixture;
}
/**
* Sets the database to use for ORM operations.
*/
static public function setDatabase(fDatabase $database)
{
self::$database = $database;
fORMDatabase::attach($database);
}
/**
* Registers a global hook callback. Global hooks are registered with the instance fixtures upon creation.
*
* @param $hook_name
* The name of the hook.
* @param $fixture_name
* The fixture for which to register the hook.
* @param $callback
* The callback function
*/
static public function registerGlobalHookCallback($hook_name, $fixture_name, $callback)
{
if (isset(self::$glabal_hook_callbacks) === FALSE) {
self::$glabal_hook_callbacks = array(
self::PreSetBuildHook => array()
);
}
if (in_array($hook_name, array(self::PreSetBuildHook)) === FALSE) {
throw new fValidationException('Invalid hook name, %s', $hook_name);
}
if (isset(self::$glabal_hook_callbacks[$hook_name][$fixture_name]) === FALSE) {
self::$glabal_hook_callbacks[$hook_name][$fixture_name] = array();
}
self::$glabal_hook_callbacks[$hook_name][$fixture_name][] = $callback;
}
static public function setVerbose($value)
{
self::$verbose = $value;
}
// ----------------------------- Instance code -----------------------------
private $db;
private $schema;
private $root;
private $replacements_root;
private $ancestor_mode;
private $fill_mode;
private $fixture_sources;
private $fixture_data;
private $white_list;
private $hook_callbacks;
private $tables_to_tear_down;
private $column_info;
private $foreign_keys;
private $keys;
/**
* Create a new instance. Use fFixture::create()
*/
public function __construct($root, $db, $white_list = NULL, $replacements_root = NULL)
{
// Ready meta data arrays
$this->column_info = array();
$this->foreign_keys = array();
$this->keys = array();
$this->ancestor_mode = FALSE;
$this->fill_mode = FALSE;
$this->white_list = is_null($white_list) ? array() : $white_list;
// Create fDirectory object if root isn't already.
if ($root instanceof fDirectory) {
$this->root = $root;
} else {
$this->root = new fDirectory($root);
}
// Scan root for fixtures
$fixture_files = $this->root->scan('*.json');
if (empty($fixture_files)) {
throw new fEnvironmentException(
'The root specified, %s, does not contain any fixtures',
$this->root
);
}
// Create map fixture name (key) => file (value)
foreach ($fixture_files as $file) {
$fixture_name = $file->getName(TRUE);
$this->fixture_sources[$fixture_name] = $file;
}
// Check for replacements
if ($replacements_root) {
// Create fDirectory object if replacements root isn't already
if ($root instanceof fDirectory) {
$this->replacements_root = $replacements_root;
} else {
$this->replacements_root = new fDirectory($replacements_root);
}
// If replacements has been specified use those
if ($this->replacements_root) {
$replacements_fixture_files = $this->replacements_root->scan('*.json');
if (empty($replacements_fixture_files)) {
throw new fEnvironmentException(
'The replacements root specified, %s, does not contain any fixtures',
$this->replacements_root
);
}
// Create map fixture name (key) => file (value)
foreach ($replacements_fixture_files as $file) {
$fixture_name = $file->getName(TRUE);
$this->fixture_sources[$fixture_name] = $file;
}
}
}
// Setup db and schema
$this->db = $db;
$this->schema = new fSchema($db);
$this->fixture_data = array();
// Merge globally registred hooks
if (isset(self::$glabal_hook_callbacks)) {
$this->hook_callbacks = self::$glabal_hook_callbacks;
} else {
$this->hook_callbacks[self::PreSetBuildHook] = array();
}
}
/**
* Validates fixtures buy loading and checking each record for required keys.
*
* @throws fValidationException In case of missing dependency or if a record is missing a required foreign key.
*/
public function validate()
{
$queue = $this->buildQueue();
foreach ($queue as $fixture_name) {
$this->validateFixture($fixture_name);
}
}
/**
* Build the records in order of dependency.
*
* @throws fProgrammerException if table does not exists
* @throws fValidationException if keys are missing or if non-existing properties is specified
*
* @return Returns an associative array of fRecordSet objects where the fixture names are the keys.
*/
public function build($traverse = TRUE)
{
try {
// Build queue
$build_queue = $this->buildQueue(NULL, $traverse);
$this->tables_to_tear_down = $build_queue;
// Build records
$completed_fixtures = array();
foreach ($build_queue as $fixture_name) {
$this->buildRecords($completed_fixtures, $fixture_name, $traverse);
}
return $completed_fixtures;
} catch (Exception $e) {
// Tear down tables in case of failure
$this->tearDown();
throw $e;
}
}
// ----------------------------- Private methods -----------------------------
/**
* Validates a fixture data set.
*
* @param string $fixture_name
* Name of the fixture to validate
*
* @throws fValidationException In case of missing dependency or if a record is missing a required foreign key.
*/
private function validateFixture(/* string */ $fixture_name)
{
if (isset($this->fixture_data[$fixture_name]) === FALSE) {
$this->loadFixture($fixture_name);
}
$schema = $this->schema;
$column_info = $schema->getColumnInfo($fixture_name);
$many_to_one = $schema->getRelationships($fixture_name, 'many-to-one');
$number_of_relationships = array_reduce($schema->getRelationships($fixture_name), function($number, $relationships) { return $number + count($relationships); }, 0);
$dependencies = array();
if ($number_of_relationships === 0) {
// Join-table like tables
$keys_found = 0;
foreach ($schema->getKeys($fixture_name, 'foreign') as $key_data) {
if ($column_info[$key_data['column']]['not_null'] === TRUE) {
$dependencies[] = $key_data['foreign_table'];
$keys_found += 1;
}
}
if ($keys_found !== 2) {
throw new fValidationException("This fixture, %1$s, has no relationship and doesn't look like a join as it has more than two keys", $fixture_name);
}
} else {
// Many-to-one
foreach ($many_to_one as $relationship) {
$key = $relationship['column'];
$foreign_table = $relationship['related_table'];
if ($fixture_name === $foreign_table) {
continue;
}
$required = $column_info[$key]['not_null'] === TRUE;
// Throw exception if foreign_table is not known, unless fill mode is on.
if ($required && $this->fill_mode === FALSE && isset($this->fixture_data[$foreign_table]) === FALSE) {
throw new fValidationException('Unable to create fixture for, %1$s, due to missing dependency %2$s',
$fixture_name,
$foreign_table
);
}
$records = $this->fixture_data[$fixture_name];
foreach ($records as $record) {
if (isset($this->fixture_data[$foreign_table])) {
continue;
}
$key_set = isset($record->$key);
// Throw exception if a required key is missing, unless fill mode is on.
if ($this->fill_mode === FALSE && $key_set === TRUE) {
if ($this->fixture_sources[$foreign_table]) {
$this->loadFixture($foreign_table);
} else {
throw new fValidationException('Unable to create fixture for %1$s with foreign key, %2$s to dependency %3$s',
$fixture_name,
$record->$key,
$foreign_table);
}
}
if ($this->fill_mode === FALSE && $required && $key_set === FALSE) {
throw new fValidationException('Invalid fixture, %1$s, record requires foreign key %2$s',
$fixture_name,
$key);
}
}
}
}
}
/**
* Loads a fixture and makes the fixture data accessible through $this->fixture_data.
*
* @param string $fixture_name
* Name of the fixture to load.
* @return Returns the loaded fixture data or NULL in the case that there was no source for the requested fixture. Note that if fill mode is off exceptions are thrown in stead of returning NULL.
*/
private function loadFixture(/* string */ $fixture_name)
{
if (isset($this->fixture_data[$fixture_name])) {
return $this->fixture_data[$fixture_name];
}
if (isset($this->fixture_sources[$fixture_name]) === FALSE) {
//if ($this->fill_mode === FALSE) {
throw new fValidationException("There exists no fixtures for, %s", $fixture_name);
//}
return NULL;
}
$fixture_source = $this->fixture_sources[$fixture_name];
// Helper vars
$fixture = $this;
$now = date('Y-m-d H:i:s');
$a_moment_ago = date('Y-m-d H:i:s', strtotime('-5 minutes'));
$in_a_moment_ago = date('Y-m-d H:i:s', strtotime('+5 minutes'));
// Source is a JSON fixture file
if ($fixture_source instanceof fFile) {
// Grab the content from buffer
ob_start();
require $fixture_source->getPath();
$json_data = ob_get_clean();
// Decode
$fixture_data = fJSON::decode($json_data);
if (empty($fixture_data)) {
throw new fValidationException("Invalid fixture file, %s", $fixture_source->getPath());
}
$this->fixture_data[$fixture_name] = $fixture_data;
} else if ($fixture_source instanceof fFixtureSeed) {
// Source is a fFixtureSeed
}
}
/**
* Build a queue that is ordered according to dependencies.
*
* @param $fixture_name
* Fixture to build queue for.
* @return
* Returns an array of table names with dependencies first and table in question last.
*/
private function buildQueue($fixture_name = NULL, $traverse = TRUE)
{
$queue = array();
// If no specific fixture is specified start from the root
if (is_null($fixture_name)) {
foreach ($this->fixture_sources as $fixture_name => $fixture_source) {
if ($this->isWhiteListed($fixture_name)) {
$queue = array_merge($queue, $this->buildQueue($fixture_name, $traverse));
}
}
if (self::$verbose) {
echo "\nwhite list : " . join(', ', $this->white_list) . "\n";
echo "initial queue : " . join(', ', array_unique($queue)) . "\n";
}
return array_unique($queue);
}
if ($traverse === FALSE) {
$queue[] = $fixture_name;
return $queue;
}
// Ready meta data
$schema = $this->schema;
$column_info = $schema->getColumnInfo($fixture_name);
$many_to_one = $schema->getRelationships($fixture_name, 'many-to-one');
$number_of_relationships = array_reduce($schema->getRelationships($fixture_name), function($number, $relationships) { return $number + count($relationships); }, 0);
$dependencies = array();
if ($number_of_relationships === 0) {
// Join-table like tables
$keys_found = 0;
foreach ($schema->getKeys($fixture_name, 'foreign') as $key_data) {
if ($column_info[$key_data['column']]['not_null'] === TRUE) {
$dependencies[] = $key_data['foreign_table'];
$keys_found += 1;
}
}
if ($keys_found !== 2) {
// throw new excption with message: Flourish joins only have keys.
}
} else {
// Many-to-one
foreach ($many_to_one as $relationship) {
$key = $relationship['column'];
$foreign_table = $relationship['related_table'];
if ($fixture_name === $foreign_table) {
continue;
}
if ($this->ancestor_mode === TRUE || $column_info[$key]['not_null'] === TRUE) {
$dependencies[] = $foreign_table;
}
}
}
foreach ($dependencies as $foreign_table) {
// If the dependency is not found in any source throw exception, unless fill mode is on.
if ($this->fill_mode === FALSE && isset($this->fixture_sources[$foreign_table]) === FALSE) {
throw new fProgrammerException('Unable to create fixture for, %1$s, due to missing data for dependency %2$s',
$fixture_name,
$foreign_table
);
}
$queue = array_merge($queue, $this->buildQueue($foreign_table));
}
$queue[] = $fixture_name;
return array_unique($queue);
}
/**
* Recursivly builds records.
*
* @param array* $completed_fixtures
* Completed records is stored in this array
* @param $fixture_data
* Build records of this fixture
*/
private function buildRecords(&$completed_fixtures, $fixture_name, $traverse = TRUE)
{
if (array_key_exists($fixture_name, $completed_fixtures)) {
return;
}
// Load data
if (isset($this->fixture_data[$fixture_name]) === FALSE) {
$this->loadFixture($fixture_name);
}
$class_name = fORM::classize($fixture_name);
// If the class does not exists created it
if (class_exists($class_name) === FALSE) {
fORM::defineActiveRecordClass($class_name);
}
// Create the records
$method_name = NULL;
$record = NULL;
$records = array();
foreach ($this->fixture_data[$fixture_name] as $record_data) {
$record = new $class_name();
foreach ($record_data as $key => $value) {
$method_name = 'set' . fGrammar::camelize($key, $upper = TRUE);
$value = $this->applyHookCallbacks(self::PreSetBuildHook, $fixture_name, $key, $value);
if ($this->isRelationshipKey($fixture_name, $key)) {
$related_table = $this->getRelatedTable($fixture_name, $key);
$required = $this->isRequiredKey($fixture_name, $key);
if ($traverse && array_key_exists($related_table, $completed_fixtures) === FALSE && $fixture_name !== $related_table) {
if (isset($value) && array_key_exists($related_table, $this->fixture_sources)) {
$this->buildRecords($completed_fixtures, $related_table);
array_unshift($this->tables_to_tear_down, $related_table);
}
}
}
$record->$method_name($value);
}
$record->store();
$records[] = $record;
}
$completed_fixtures[$fixture_name] = fRecordSet::buildFromArray($class_name, $records);
}
/**
* Is the fixture white listed.
*/
private function isWhiteListed($fixture_name)
{
return empty($this->white_list) || in_array($fixture_name, $this->white_list);
}
/**
* Empty tables in the reverse order of their creation.
*/
public function tearDown()
{
if (is_array($this->tables_to_tear_down)) {
$tables = array_reverse(array_unique($this->tables_to_tear_down));
if (self::$verbose) {
echo "tear down plan: " . join(', ', $tables) . "\n";
}
foreach ($tables as $fixture_name) {
$this->db->execute("DELETE FROM $fixture_name");
}
}
}
public function key($key_name, $value = NULL)
{
if ($value === NULL) {
return $this->keys[$key_name];
}
$this->keys[$key_name] = $value;
return $value;
}
/**
* Registers a hook callback. Global hooks are registered with the instance fixtures upon creation.
*
* @param $hook_name
* The name of the hook.
* @param $fixture_name
* The fixture for which to register the hook.
* @param $callback
* The callback function
*/
public function registerHookCallback($hook_name, $fixture_name, $callback)
{
if (in_array($hook_name, array(self::PreSetBuildHook)) === FALSE) {
throw new fValidationException('Invalid hook name, %s', $hook_name);
}
if (isset($this->hook_callbacks[$hook_name][$fixture_name]) === FALSE) {
$this->hook_callbacks[$hook_name][$fixture_name] = array();
}
$this->hook_callbacks[$hook_name][$fixture_name][] = $callback;
}
private function applyHookCallbacks($hook_name, $fixture_name, $key, $value)
{
if (isset($this->hook_callbacks[$hook_name][$fixture_name]) === FALSE) {
return $value;
}
$original_value = $value;
foreach ($this->hook_callbacks[$hook_name][$fixture_name] as $callback) {
$value = $callback($key, $value, $original_value);
}
return $value;
}
/**
* Is the key of fixture a relationship key or not
*/
private function isRelationshipKey($fixture_name, $key)
{
$many_to_one = $this->schema->getRelationships($fixture_name, 'many-to-one');
foreach ($many_to_one as $relationship) {
$foreign_table = $relationship['related_table'];
if ($key === $relationship['column']) {
return TRUE; //return $fixture_name !== $foreign_table;
}
}
return FALSE;
}
/**
* Return related table based on key
*/
private function getRelatedTable($fixture_name, $foreign_key)
{
$many_to_one = $this->schema->getRelationships($fixture_name, 'many-to-one');
foreach ($many_to_one as $relationship) {
$key = $relationship['column'];
$foreign_table = $relationship['related_table'];
if ($foreign_key === $key) {
return $foreign_table; //return $fixture_name === $foreign_table ? NULL : $foreign_table;
}
}
return NULL;
}
/**
* Is the key of a fixture a required key or not.
*/
private function isRequiredKey($fixture_name, $key)
{
$column_info = $this->schema->getColumnInfo($fixture_name);
return $column_info[$key]['not_null'] === TRUE;
}
}