1
+ <?php
2
+
3
+ namespace Simply \Database ;
4
+
5
+ use Simply \Database \Connection \Connection ;
6
+ use Simply \Database \Test \TestCase \UnitTestCase ;
7
+
8
+ /**
9
+ * RelationshipFillerTest.
10
+ * @author Riikka Kalliomäki <[email protected] >
11
+ * @copyright Copyright (c) 2018 Riikka Kalliomäki
12
+ * @license http://opensource.org/licenses/mit-license.php MIT License
13
+ */
14
+ class RelationshipFillerTest extends UnitTestCase
15
+ {
16
+ public function testEmptyRecordsList (): void
17
+ {
18
+ $ connection = $ this ->createMock (Connection::class);
19
+ $ filler = new RelationshipFiller ($ connection );
20
+
21
+ $ connection ->expects ($ this ->never ())->method ('select ' );
22
+ $ filler ->fill ([], ['spouse ' ]);
23
+ }
24
+
25
+ public function testMixedRecordSchemas (): void
26
+ {
27
+ $ schema = $ this ->getPersonSchema ();
28
+
29
+ $ person = $ schema ->createRecord ();
30
+ $ person ['id ' ] = 1 ;
31
+ $ person ->updateState (Record::STATE_INSERT );
32
+
33
+ $ parent = $ schema ->getRelationship ('parents ' )->getReferencedSchema ()->createRecord ();
34
+ $ parent ['child_id ' ] = 1 ;
35
+ $ parent ['parent_id ' ] = 1 ;
36
+ $ parent ->updateState (Record::STATE_INSERT );
37
+
38
+ $ connection = $ this ->createMock (Connection::class);
39
+ $ filler = new RelationshipFiller ($ connection );
40
+
41
+ $ this ->expectException (\InvalidArgumentException::class);
42
+ $ filler ->fill ([$ person , $ parent ], ['spouse ' ]);
43
+ }
44
+
45
+ public function testFillingWithCompositeForeignKeys (): void
46
+ {
47
+ $ schema = $ this ->getCompositeForeignKeySchema ();
48
+
49
+ $ order = $ schema ->createRecord ();
50
+ $ order ['order_id ' ] = 1 ;
51
+ $ order ['product_id ' ] = 1 ;
52
+ $ order ['replaced_order_id ' ] = 1 ;
53
+ $ order ['replaced_product_id ' ] = 1 ;
54
+ $ order ->updateState (Record::STATE_INSERT );
55
+
56
+ $ connection = $ this ->createMock (Connection::class);
57
+ $ filler = new RelationshipFiller ($ connection );
58
+
59
+ $ this ->expectException (\InvalidArgumentException::class);
60
+ $ filler ->fill ([$ order ], ['replaced ' ]);
61
+ }
62
+
63
+ public function testDuplicatedRecordsInList (): void
64
+ {
65
+ $ schema = $ this ->getPersonSchema ();
66
+
67
+ $ personA = $ schema ->createRecord ();
68
+ $ personA ['id ' ] = 1 ;
69
+ $ personA ->updateState (Record::STATE_INSERT );
70
+
71
+ $ personB = $ schema ->createRecord ();
72
+ $ personB ['id ' ] = 1 ;
73
+ $ personB ->updateState (Record::STATE_INSERT );
74
+
75
+ $ connection = $ this ->createMock (Connection::class);
76
+ $ filler = new RelationshipFiller ($ connection );
77
+
78
+ $ this ->expectException (\RuntimeException::class);
79
+ $ filler ->fill ([$ personA , $ personB ], ['spouse ' ]);
80
+ }
81
+
82
+ public function testNoQueriesWhenCached ()
83
+ {
84
+ $ schema = $ this ->getPersonSchema ();
85
+
86
+ $ personA = $ schema ->createRecord ();
87
+ $ personA ['id ' ] = 1 ;
88
+ $ personA ['spouse_id ' ] = 2 ;
89
+ $ personA ->updateState (Record::STATE_INSERT );
90
+
91
+ $ personB = $ schema ->createRecord ();
92
+ $ personB ['id ' ] = 2 ;
93
+ $ personB ['spouse_id ' ] = 1 ;
94
+ $ personB ->updateState (Record::STATE_INSERT );
95
+
96
+ $ connection = $ this ->createMock (Connection::class);
97
+ $ filler = new RelationshipFiller ($ connection );
98
+
99
+ $ connection ->expects ($ this ->never ())->method ('select ' );
100
+ $ filler ->fill ([$ personA , $ personB ], ['spouse ' ]);
101
+ }
102
+ }
0 commit comments