/
DocumentTest.php
818 lines (663 loc) · 23.3 KB
/
DocumentTest.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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\tests\cases\data\entity;
use MongoId;
use MongoDate;
use lithium\data\Connections;
use lithium\data\source\MongoDb;
use lithium\data\source\http\adapter\CouchDb;
use lithium\data\entity\Document;
use lithium\data\collection\DocumentSet;
use lithium\data\collection\DocumentArray;
use lithium\tests\mocks\data\model\MockDocumentPost;
use lithium\tests\mocks\data\model\MockDocumentMultipleKey;
class DocumentTest extends \lithium\test\Unit {
protected $_model = 'lithium\tests\mocks\data\model\MockDocumentPost';
protected $_backup = array();
public function skip() {
$this->skipIf(!MongoDb::enabled(), 'MongoDb is not enabled');
$this->skipIf(!CouchDb::enabled(), 'CouchDb is not enabled');
}
public function setUp() {
if (empty($this->_backup)) {
foreach (Connections::get() as $conn) {
$this->_backup[$conn] = Connections::get($conn, array('config' => true));
}
}
Connections::reset();
Connections::add('mongo', array('type' => 'MongoDb', 'autoConnect' => false));
Connections::add('couch', array('type' => 'http', 'adapter' => 'CouchDb'));
MockDocumentPost::config(array('connection' => 'mongo'));
MockDocumentMultipleKey::config(array('connection' => 'couch'));
}
public function tearDown() {
foreach ($this->_backup as $name => $config) {
Connections::add($name, $config);
}
}
public function testFindAllAndIterate() {
$set = MockDocumentPost::find('all');
$expected = array('id' => 1, 'name' => 'One', 'content' => 'Lorem ipsum one');
$result = $set->current()->data();
$this->assertEqual($expected, $result);
$expected = array('id' => 2, 'name' => 'Two', 'content' => 'Lorem ipsum two');
$result = $set->next()->data();
$this->assertEqual($expected, $result);
$expected = array('id' => 3, 'name' => 'Three', 'content' => 'Lorem ipsum three');
$set->next();
$result = $set->current()->data();
$this->assertEqual($expected, $result);
$result = $set->next();
$this->assertTrue(empty($result));
$expected = array('id' => 1, 'name' => 'One', 'content' => 'Lorem ipsum one');
$result = $set->rewind()->data();
$this->assertEqual($expected, $result);
}
public function testFindOne() {
$document = MockDocumentPost::find('first');
$expected = array('id' => 2, 'name' => 'Two', 'content' => 'Lorem ipsum two');
$result = $document->data();
$this->assertEqual($expected, $result);
}
public function testGetFields() {
$document = MockDocumentPost::find('first');
$expected = 2;
$result = $document->id;
$this->assertEqual($expected, $result);
$expected = 'Two';
$result = $document->name;
$this->assertEqual($expected, $result);
$expected = 'Lorem ipsum two';
$result = $document->content;
$this->assertEqual($expected, $result);
}
public function testSetField() {
$doc = new Document();
$doc->id = 4;
$doc->name = 'Four';
$doc->content = 'Lorem ipsum four';
$expected = array(
'id' => 4,
'name' => 'Four',
'content' => 'Lorem ipsum four'
);
$result = $doc->data();
$this->assertEqual($expected, $result);
}
public function testSyncModified() {
$doc = new Document(array('model' => $this->_model));
$doc->id = 4;
$doc->name = 'Four';
$doc->content = 'Lorem ipsum four';
$doc->array = array(1, 2, 3, 4);
$doc->subdoc = array(
'setting' => 'something',
'foo' => 'bar',
'sub' => array(
'name' => 'A sub sub doc'
)
);
$doc->subdocs = array(
array(
'id' => 1
),
array(
'id' => 2
),
array(
'id' => 3
),
array(
'id' => 4
)
);
$fields = array(
'id', 'name', 'content',
'array', 'subdoc', 'subdocs'
);
$expected = array_fill_keys($fields, true);
$this->assertEqual($expected, $doc->modified());
$doc->sync();
$this->assertEqual(array_fill_keys($fields, false), $doc->modified());
$doc->id = 5;
$doc->content = null;
$doc->new = null;
$doc->subdoc->foo = 'baz';
$doc->array[] = 5;
$doc->subdocs[] = array('id' => 5);
$expected['name'] = false;
$expected['new'] = true;
$fields[] = 'new';
$this->assertEqual($expected, $doc->modified());
$doc->sync();
$expected = array_fill_keys($fields, false);
$this->assertEqual($expected, $doc->modified());
$doc->sync();
$doc->subdocs[1]->updated = true;
$expected['subdocs'] = true;
$this->assertEqual($expected, $doc->modified());
$doc->sync();
$expected = array_fill_keys($fields, false);
$doc->array[1] = array(
'foo' => 'bar'
);
$expected['array'] = true;
$this->assertEqual($expected, $doc->modified());
$doc->sync();
}
public function testNestedKeyGetSet() {
$doc = new Document(array('model' => $this->_model, 'data' => array(
'name' => 'Bob', 'location' => 'New York, NY', 'profile' => array(
'occupation' => 'Developer', 'likes' => 'PHP', 'dislikes' => 'Java'
)
)));
$expected = array('occupation' => 'Developer', 'likes' => 'PHP', 'dislikes' => 'Java');
$this->assertEqual($expected, $doc->profile->data());
$this->assertEqual('Java', $doc->profile->dislikes);
$this->assertEqual('Java', $doc->{'profile.dislikes'});
$this->assertNull($doc->{'profile.'});
$this->assertNull($doc->{'profile.foo'});
$this->assertNull($doc->{'profile.foo.bar'});
$doc->{'profile.dislikes'} = 'Crystal Reports';
$this->assertEqual('Crystal Reports', $doc->profile->dislikes);
$doc->{'profile.foo.bar'} = 'baz';
$this->assertTrue($doc->profile->foo instanceof Document);
$this->assertEqual(array('bar' => 'baz'), $doc->profile->foo->data());
$post = new Document(array('model' => $this->_model, 'data' => array(
'title' => 'Blog Post',
'body' => 'Some post content.',
'meta' => array('tags' => array('foo', 'bar', 'baz'))
)));
$this->assertEqual(array('foo', 'bar', 'baz'), $post->meta->tags->data());
$post->{'meta.tags'}[] = 'dib';
$this->assertEqual(array('foo', 'bar', 'baz', 'dib'), $post->meta->tags->data());
}
public function testNoItems() {
$doc = new Document(array('model' => $this->_model, 'data' => array()));
$result = $doc->id;
$this->assertFalse($result);
}
public function testWithData() {
$doc = new DocumentSet(array('model' => $this->_model, 'data' => array(
array('id' => 1, 'name' => 'One', 'content' => 'Lorem ipsum one'),
array('id' => 2, 'name' => 'Two', 'content' => 'Lorem ipsum two'),
array('id' => 3, 'name' => 'Three', 'content' => 'Lorem ipsum three')
)));
$expected = array('id' => 1, 'name' => 'One', 'content' => 'Lorem ipsum one');
$result = $doc->current()->data();
$this->assertEqual($expected, $result);
$expected = array('id' => 2, 'name' => 'Two', 'content' => 'Lorem ipsum two');
$result = $doc->next()->data();
$this->assertEqual($expected, $result);
}
public function testExplicitSet() {
$doc = new Document();
$doc->set(array('id' => 4));
$doc->set(array('name' => 'Four'));
$doc->set(array('content' => 'Lorem ipsum four'));
$expected = array('id' => 4, 'name' => 'Four', 'content' => 'Lorem ipsum four');
$result = $doc->data();
$this->assertEqual($expected, $result);
}
public function testSetMultiple() {
$doc = new DocumentSet(array('model' => $this->_model));
$doc->set(array(
array('id' => 1, 'name' => 'One', 'content' => 'Lorem ipsum one'),
array('id' => 2, 'name' => 'Two', 'content' => 'Lorem ipsum two'),
array('id' => 3, 'name' => 'Three', 'content' => 'Lorem ipsum three')
));
$expected = array('id' => 1, 'name' => 'One', 'content' => 'Lorem ipsum one');
return;
$result = $doc->current()->data();
$this->assertEqual($expected, $result);
$expected = array('id' => 2, 'name' => 'Two', 'content' => 'Lorem ipsum two');
$result = $doc->next()->data();
$this->assertEqual($expected, $result);
}
public function testSetMultipleNested() {
$doc = new Document(array('model' => $this->_model));
$doc->id = 123;
$doc->type = 'father';
$doc->set(array('children' => array(
array('id' => 124, 'type' => 'child', 'children' => null),
array('id' => 125, 'type' => 'child', 'children' => null)
)));
$this->assertEqual('father', $doc->type);
$this->assertTrue($doc->children instanceof DocumentArray);
$expected = array('id' => 124, 'type' => 'child', 'children' => null);
$result = $doc->children[0]->data();
$this->assertEqual($expected, $result);
$expected = array('id' => 125, 'type' => 'child', 'children' => null);
$result = $doc->children[1]->data();
// @todo Make $result = $doc->children->{1}->data(); work as well (and ...->{'1'}->...)
$this->assertEqual($expected, $result);
}
public function testSetNested() {
$doc = new Document(array('model' => $this->_model));
$doc->id = 123;
$doc->name = 'father';
$doc->set(array('child' => array('id' => 124, 'name' => 'child')));
$this->assertEqual('father', $doc->name);
$this->assertTrue(is_object($doc->child), 'children is not an object');
$this->assertTrue($doc->child instanceof Document, 'Child is not of the type Document');
$this->skipIf(!$doc->child instanceof Document, 'Child is not of the type Document');
$expected = 124;
$result = $doc->child->id;
$this->assertEqual($expected, $result);
$expected = 'child';
$result = $doc->child->name;
$this->assertEqual($expected, $result);
}
public function testNestedSingle() {
$doc = new Document(array('model' => $this->_model));
$doc->arr1 = array('something' => 'else');
$doc->arr2 = array('some' => 'noses', 'have' => 'it');
$this->assertTrue($doc->arr1 instanceof Document);
$this->assertTrue($doc->arr2 instanceof Document);
}
public function testRewindNoData() {
$doc = new DocumentSet();
$result = $doc->rewind();
$this->assertNull($result);
}
public function testRewindData() {
$doc = new DocumentSet(array('model' => $this->_model, 'data' => array(
array('id' => 1, 'name' => 'One'),
array('id' => 2, 'name' => 'Two'),
array('id' => 3, 'name' => 'Three')
)));
$expected = array('id' => 1, 'name' => 'One');
$result = $doc->rewind()->data();
$this->assertEqual($expected, $result);
}
public function testUpdateWithSingleKey() {
$doc = new Document(array('model' => $this->_model));
$result = MockDocumentPost::meta('key');
$this->assertEqual('_id', $result);
$doc->_id = 3;
$this->assertFalse($doc->exists());
$doc->sync(12);
$this->assertTrue($doc->exists());
$this->assertEqual(12, $doc->_id);
}
public function testUpdateWithMultipleKeys() {
$model = 'lithium\tests\mocks\data\model\MockDocumentMultipleKey';
$model::config(array('key' => array('id', 'rev'), 'foo' => true));
$doc = new Document(compact('model'));
$result = $model::meta('key');
$this->assertEqual(array('id', 'rev'), $result);
$doc->id = 3;
$this->assertFalse($doc->exists());
$doc->sync(array(12, '1-2'));
$this->assertTrue($doc->exists());
$this->assertEqual(12, $doc->id);
$this->assertEqual('1-2', $doc->rev);
}
public function testArrayValueNestedDocument() {
$doc = new Document(array(
'model' => 'lithium\tests\mocks\data\model\MockDocumentPost',
'data' => array(
'id' => 12, 'arr' => array('id' => 33, 'name' => 'stone'), 'name' => 'bird'
)
));
$this->assertEqual(12, $doc->id);
$this->assertEqual('bird', $doc->name);
$this->assertTrue(is_object($doc->arr), 'arr is not an object');
$this->assertTrue($doc->arr instanceof Document, 'arr is not of the type Document');
$this->skipIf(!$doc->arr instanceof Document, 'arr is not of the type Document');
$this->assertEqual(33, $doc->arr->id);
$this->assertEqual('stone', $doc->arr->name);
}
public function testArrayValueGet() {
$doc = new Document(array(
'model' => $this->_model,
'data' => array('id' => 12, 'name' => 'Joe', 'sons' => array('Moe', 'Greg'))
));
$this->assertEqual(12, $doc->id);
$this->assertEqual('Joe', $doc->name);
$this->assertTrue($doc->sons instanceof DocumentArray, 'arr is not an array');
$this->assertEqual(array('Moe', 'Greg'), $doc->sons->data());
}
public function testArrayValueSet() {
$doc = new Document(array('model' => $this->_model));
$doc->id = 12;
$doc->name = 'Joe';
$doc->sons = array('Moe', 'Greg', 12, 0.3);
$doc->set(array('daughters' => array('Susan', 'Tinkerbell')));
$expected = array(
'id' => 12,
'name' => 'Joe',
'sons' => array('Moe', 'Greg', 12, 0.3),
'daughters' => array('Susan', 'Tinkerbell')
);
$result = $doc->data();
$this->assertEqual($expected, $result);
}
public function testInvalidCall() {
$doc = new Document();
$this->expectException("No model bound or unhandled method call `medicin`.");
$result = $doc->medicin();
$this->assertNull($result);
}
public function testCall() {
$doc = new Document(array('model' => 'lithium\tests\mocks\data\model\MockDocumentPost'));
$expected = 'lithium';
$result = $doc->medicin();
$this->assertEqual($expected, $result);
$result = $doc->ret();
$this->assertNull($result);
$expected = 'nose';
$result = $doc->ret('nose');
$this->assertEqual($expected, $result);
$expected = 'job';
$result = $doc->ret('nose','job');
$this->assertEqual($expected, $result);
}
public function testEmptyValues() {
$doc = new Document(array(
'model' => 'lithium\tests\mocks\data\model\MockDocumentPost',
'data' => array(
'title' => 'Post',
'content' => 'Lorem Ipsum',
'parsed' => null,
'permanent' => false
)
));
$expected = array(
'title' => 'Post',
'content' => 'Lorem Ipsum',
'parsed' => null,
'permanent' => false
);
$result = $doc->data();
$this->assertEqual($expected, $result);
}
public function testBooleanValues() {
$doc = new Document(array('model' => $this->_model));
$doc->tall = false;
$doc->fat = true;
$doc->set(array('hair' => true, 'fast' => false));
$expected = array('fast', 'fat', 'hair', 'tall');
$result = array_keys($doc->data());
sort($result);
$this->assertEqual($expected, $result);
}
public function testIsset() {
$doc = new Document(array('data' => array(
'title' => 'Post',
'content' => 'Lorem Ipsum'
)));
$this->assertTrue(isset($doc->title));
$this->assertTrue(isset($doc->content));
$this->assertFalse(isset($doc->body));
}
public function testData() {
$doc = new Document(array(
'data' => array(
'title' => 'Post',
'content' => 'Lorem Ipsum',
'parsed' => null,
'permanent' => false
)
));
$expected = array(
'title' => 'Post',
'content' => 'Lorem Ipsum',
'parsed' => null,
'permanent' => false
);
$result = $doc->data();
$this->assertEqual($expected, $result);
$expected = 'Post';
$result = $doc->data('title');
$this->assertEqual($expected, $result);
$result = $doc->data('permanent');
$this->assertFalse($result);
$doc = new Document();
$this->assertNull($doc->data('field'));
}
public function testUnset() {
$doc = new Document(array(
'data' => array(
'title' => 'Post',
'content' => 'Lorem Ipsum',
'parsed' => null,
'permanent' => false
)
));
$expected = array(
'title' => 'Post',
'content' => 'Lorem Ipsum',
'parsed' => null,
'permanent' => false
);
$result = $doc->data();
$this->assertEqual($expected, $result);
unset($expected['title']);
unset($doc->title);
$this->assertEqual($expected, $doc->data());
unset($expected['parsed']);
unset($doc->parsed);
$result = $doc->data();
$this->assertEqual($expected, $result);
unset($expected['permanent']);
unset($doc->permanent);
$result = $doc->data();
$this->assertEqual($expected, $result);
unset($doc->none);
}
public function testErrors() {
$doc = new Document(array('data' => array(
'title' => 'Post',
'content' => 'Lorem Ipsum',
'parsed' => null,
'permanent' => false
)));
$errors = array('title' => 'Too short', 'parsed' => 'Empty');
$doc->errors($errors);
$expected = $errors;
$result = $doc->errors();
$this->assertEqual($expected, $result);
$expected = 'Too short';
$result = $doc->errors('title');
$this->assertEqual($expected, $result);
$doc->errors('title', 'Too generic');
$expected = 'Too generic';
$result = $doc->errors('title');
$this->assertEqual($expected, $result);
}
public function testDocumentNesting() {
$model = $this->_model;
$data = array('top' => 'level', 'second' => array('level' => 'of data'));
$doc = new Document(compact('model', 'data'));
$this->assertTrue(isset($doc->top));
$this->assertTrue(isset($doc->second->level));
$this->assertTrue($doc->second instanceof Document);
$this->assertEqual('level', $doc->top);
$this->assertEqual('of data', $doc->second->level);
}
public function testPropertyIteration() {
$doc = new Document(array('data' => array('foo' => 'bar', 'baz' => 'dib')));
$keys = array(null, 'foo', 'baz');
$values = array(null, 'bar', 'dib');
foreach ($doc as $key => $value) {
$this->assertEqual(next($keys), $key);
$this->assertEqual(next($values), $value);
}
}
public function testExport() {
$data = array('foo' => 'bar', 'baz' => 'dib');
$doc = new Document(compact('data') + array('exists' => false));
$expected = array(
'data' => array('foo' => 'bar', 'baz' => 'dib'),
'update' => array('foo' => 'bar', 'baz' => 'dib'),
'increment' => array(),
'key' => '',
'exists' => false
);
$this->assertEqual($expected, $doc->export());
}
/**
* Tests that documents nested within existing documents also exist, and vice versa.
*/
public function testNestedObjectExistence() {
$model = $this->_model;
$data = array('foo' => array('bar' => 'bar', 'baz' => 'dib'));
$doc = new Document(compact('model', 'data') + array('exists' => false));
$this->assertFalse($doc->exists());
$this->assertFalse($doc->foo->exists());
$doc = new Document(compact('model', 'data') + array('exists' => true));
$this->assertTrue($doc->exists());
$this->assertTrue($doc->foo->exists());
}
/**
* Tests that a modified `Document` exports the proper fields in a newly-appended nested
* `Document`.
*/
public function testModifiedExport() {
$model = $this->_model;
$data = array('foo' => 'bar', 'baz' => 'dib');
$doc = new Document(compact('model', 'data') + array('exists' => false));
$doc->nested = array('more' => 'data');
$newData = $doc->export();
$expected = array('foo' => 'bar', 'baz' => 'dib', 'nested.more' => 'data');
$this->assertFalse($newData['exists']);
$this->assertEqual(array('foo' => 'bar', 'baz' => 'dib'), $newData['data']);
$this->assertEqual(3, count($newData['update']));
$this->assertTrue($newData['update']['nested'] instanceof Document);
$result = $newData['update']['nested']->export();
$this->assertFalse($result['exists']);
$this->assertEqual(array('more' => 'data'), $result['data']);
$this->assertEqual(array('more' => 'data'), $result['update']);
$this->assertEqual('nested', $result['key']);
$doc = new Document(compact('model') + array('exists' => true, 'data' => array(
'foo' => 'bar', 'baz' => 'dib'
)));
$result = $doc->export();
$this->assertEqual($result['data'], $result['update']);
$doc->nested = array('more' => 'data');
$this->assertEqual('data', $doc->nested->more);
$modified = $doc->export();
$this->assertTrue($modified['exists']);
$this->assertEqual(array('foo' => 'bar', 'baz' => 'dib'), $modified['data']);
$this->assertEqual(array('nested', 'foo', 'baz'), array_keys($modified['update']));
$this->assertNull($modified['key']);
$nested = $modified['update']['nested']->export();
$this->assertFalse($nested['exists']);
$this->assertEqual(array('more' => 'data'), $nested['data']);
$this->assertEqual('nested', $nested['key']);
$doc->sync();
$result = $doc->export();
$this->assertEqual($result['data'], $result['update']);
$doc->more = 'cowbell';
$doc->nested->evenMore = 'cowbell';
$modified = $doc->export();
$expected = array('more' => 'cowbell') + $modified['data'];
$this->assertEqual($expected, $modified['update']);
$this->assertEqual(array('nested', 'foo', 'baz'), array_keys($modified['data']));
$this->assertEqual('bar', $modified['data']['foo']);
$this->assertEqual('dib', $modified['data']['baz']);
$this->assertTrue($modified['exists']);
$nested = $modified['data']['nested']->export();
$this->assertTrue($nested['exists']);
$this->assertEqual(array('more' => 'data'), $nested['data']);
$this->assertEqual(array('evenMore' => 'cowbell') + $nested['data'], $nested['update']);
$this->assertEqual('nested', $nested['key']);
$doc->sync();
$doc->nested->evenMore = 'foo!';
$modified = $doc->export();
$this->assertEqual($modified['data'], $modified['update']);
$nested = $modified['data']['nested']->export();
$this->assertEqual(array('evenMore' => 'foo!') + $nested['data'], $nested['update']);
}
public function testArrayConversion() {
$doc = new Document(array('data' => array(
'id' => new MongoId(),
'date' => new MongoDate()
)));
$result = $doc->data();
$this->assertPattern('/^[a-f0-9]{24}$/', $result['id']);
$this->assertEqual(time(), $result['date']);
}
public function testArrayInterface() {
$doc = new Document();
$doc->field = 'value';
$this->assertEqual('value', $doc['field']);
$doc['field'] = 'newvalue';
$this->assertEqual('newvalue', $doc->field);
unset($doc['field']);
$this->assertNull($doc->field);
}
/**
* Tests that unassigned fields with default schema values are auto-populated at access time.
*/
public function testSchemaValueInitialization() {
$doc = new Document(array('schema' => array(
'foo' => array('type' => 'string', 'default' => 'bar')
)));
$this->assertFalse($doc->data());
$this->assertEqual('bar', $doc->foo);
$this->assertEqual(array('foo' => 'bar'), $doc->data());
}
public function testInitializationWithNestedFields() {
$doc = new Document(array('model' => $this->_model, 'data' => array(
'simple' => 'value',
'nested.foo' => 'first',
'nested.bar' => 'second',
'really.nested.key' => 'value'
)));
$this->assertEqual('value', $doc->simple);
$this->assertEqual('first', $doc->nested->foo);
$this->assertEqual('second', $doc->nested->bar);
$this->assertEqual('value', $doc->really->nested->key);
$result = array_keys($doc->data());
sort($result);
$this->assertEqual(array('nested', 'really', 'simple'), $result);
}
public function testWithArraySchemaReusedName() {
$model = $this->_model;
$schema = array(
'_id' => array('type' => 'id'),
'bar' => array('array' => true),
'foo' => array('type' => 'object', 'array' => true),
'foo.foo' => array('type' => 'integer'),
'foo.bar' => array('type' => 'integer')
);
$doc = new Document(compact('model', 'schema'));
$doc->foo[] = array('foo' => 1, 'bar' => 100);
$expected = array('foo' => array(array('foo' => 1, 'bar' => 100)));
$this->assertEqual($expected, $doc->data());
}
public function testIdGetDoesNotSet() {
$document = MockDocumentPost::create();
$message = 'The `_id` key should not be set.';
$this->assertFalse(array_key_exists('_id', $document->data()), $message);
$document->_id == "";
$this->assertFalse(array_key_exists('_id', $document->data()), $message);
}
/**
* Ensures that the data returned from the `data()` method matches the
* internal state of the object.
*/
public function testEnsureArrayExportFidelity() {
$data = array(
'department_3' => 0,
4 => 0,
5 => 0,
6 => 0,
'6x' => 0,
7 => 0,
8 => 0,
10 => 0,
12 => 0
);
$doc = new Document(compact('data'));
$this->assertIdentical($data, $doc->data());
}
}
?>