1
+ <?php
2
+
3
+ namespace Simply \Database ;
4
+
5
+ use Simply \Database \Test \TestCase \UnitTestCase ;
6
+
7
+ /**
8
+ * RecordTest.
9
+ * @author Riikka Kalliomäki <[email protected] >
10
+ * @copyright Copyright (c) 2018 Riikka Kalliomäki
11
+ * @license http://opensource.org/licenses/mit-license.php MIT License
12
+ */
13
+ class RecordTest extends UnitTestCase
14
+ {
15
+ public function testPrimaryKeyOnEmptyRecord (): void
16
+ {
17
+ $ schema = $ this ->getPersonSchema ();
18
+ $ person = $ schema ->createRecord ();
19
+
20
+ $ this ->expectException (\RuntimeException::class);
21
+ $ person ->getPrimaryKey ();
22
+ }
23
+
24
+ public function testFetchingNoReferences (): void
25
+ {
26
+ $ schema = $ this ->getPersonSchema ();
27
+ $ person = $ schema ->createRecord ();
28
+
29
+ $ this ->expectException (\RuntimeException::class);
30
+ $ person ->getReferencedRecords ('spouse ' );
31
+ }
32
+
33
+ public function testMultipleModelAssociation (): void
34
+ {
35
+ $ schema = $ this ->getPersonSchema ();
36
+
37
+ $ personA = $ schema ->createRecord ();
38
+ $ personB = $ schema ->createRecord ();
39
+
40
+ $ this ->expectException (\InvalidArgumentException::class);
41
+ $ personA ->associate ('parents ' , $ personB ->getModel ());
42
+ }
43
+
44
+ public function testAssociateWithInvalidSchema (): void
45
+ {
46
+ $ schema = $ this ->getPersonSchema ();
47
+
48
+ $ person = $ schema ->createRecord ();
49
+ $ house = $ schema ->getRelationship ('home ' )->getReferencedSchema ()->createRecord ();
50
+
51
+ $ this ->expectException (\InvalidArgumentException::class);
52
+ $ person ->associate ('spouse ' , $ house ->getModel ());
53
+ }
54
+
55
+ public function testAssociatingWithNullValue (): void
56
+ {
57
+ $ schema = $ this ->getPersonSchema ();
58
+
59
+ $ personA = $ schema ->createRecord ();
60
+ $ personB = $ schema ->createRecord ();
61
+
62
+ $ this ->expectException (\RuntimeException::class);
63
+ $ personA ->associate ('spouse ' , $ personB ->getModel ());
64
+ }
65
+
66
+ public function testAddAssociationToUniqueRelationship (): void
67
+ {
68
+ $ schema = $ this ->getPersonSchema ();
69
+
70
+ $ personA = $ schema ->createRecord ();
71
+ $ personB = $ schema ->createRecord ();
72
+
73
+ $ this ->expectException (\InvalidArgumentException::class);
74
+ $ personA ->addAssociation ('spouse ' , $ personB ->getModel ());
75
+ }
76
+
77
+ public function testAssociateMultipleRelationship (): void
78
+ {
79
+ $ schema = $ this ->getPersonSchema ();
80
+
81
+ $ personA = $ schema ->createRecord ();
82
+ $ personB = $ schema ->createRecord ();
83
+
84
+ $ house = $ schema ->getRelationship ('home ' )->getReferencedSchema ()->createRecord ();
85
+ $ house ['id ' ] = 1 ;
86
+
87
+ $ personA ->associate ('home ' , $ house ->getModel ());
88
+
89
+ $ this ->assertSame ([$ house ], $ personA ->getReferencedRecords ('home ' ));
90
+ $ this ->assertFalse ($ house ->hasReferencedRecords ('residents ' ));
91
+
92
+ $ house ->setReferencedRecords ('residents ' , [$ personA ]);
93
+
94
+ $ personB ->associate ('home ' , $ house ->getModel ());
95
+
96
+ $ this ->assertSame ([$ personA , $ personB ], $ house ->getReferencedRecords ('residents ' ));
97
+ }
98
+
99
+ public function testGetRelatedModelForNonUnique (): void
100
+ {
101
+ $ schema = $ this ->getPersonSchema ();
102
+ $ person = $ schema ->createRecord ();
103
+
104
+ $ this ->expectException (\RuntimeException::class);
105
+ $ person ->getRelatedModel ('parents ' );
106
+ }
107
+
108
+ public function testGetEmptyRelatedModel (): void
109
+ {
110
+ $ schema = $ this ->getPersonSchema ();
111
+
112
+ $ person = $ schema ->createRecord ();
113
+ $ person ->setReferencedRecords ('spouse ' , []);
114
+
115
+ $ this ->assertNull ($ person ->getRelatedModel ('spouse ' ));
116
+ }
117
+
118
+ public function testGetMultipleModelsForNonUnique (): void
119
+ {
120
+ $ schema = $ this ->getPersonSchema ();
121
+ $ person = $ schema ->createRecord ();
122
+
123
+ $ this ->expectException (\RuntimeException::class);
124
+ $ person ->getRelatedModels ('spouse ' );
125
+ }
126
+
127
+ public function testUnsettingRecordFields (): void
128
+ {
129
+ $ person = $ this ->getPersonSchema ()->createRecord ();
130
+
131
+ $ this ->assertNull ($ person ['id ' ]);
132
+ $ this ->assertFalse (isset ($ person ['id ' ]));
133
+
134
+ $ person ['id ' ] = 1 ;
135
+
136
+ $ this ->assertSame (1 , $ person ['id ' ]);
137
+ $ this ->assertTrue (isset ($ person ['id ' ]));
138
+
139
+ unset($ person ['id ' ]);
140
+
141
+ $ this ->assertNull ($ person ['id ' ]);
142
+ $ this ->assertFalse (isset ($ person ['id ' ]));
143
+ }
144
+
145
+ public function testGettingInvalidField (): void
146
+ {
147
+ $ person = $ this ->getPersonSchema ()->createRecord ();
148
+
149
+ $ this ->expectException (\InvalidArgumentException::class);
150
+ $ person ['not-a-field ' ];
151
+ }
152
+
153
+ public function testSettingInvalidField (): void
154
+ {
155
+ $ person = $ this ->getPersonSchema ()->createRecord ();
156
+
157
+ $ this ->expectException (\InvalidArgumentException::class);
158
+ $ person ['not-a-field ' ] = 'value ' ;
159
+ }
160
+
161
+ public function testIncorrectValueOrder (): void
162
+ {
163
+ $ schema = $ this ->getPersonSchema ();
164
+ $ person = $ schema ->createRecord ();
165
+
166
+ $ values = array_fill_keys (array_reverse ($ schema ->getFields ()), 1 );
167
+
168
+ $ this ->expectException (\InvalidArgumentException::class);
169
+ $ person ->setDatabaseValues ($ values );
170
+ }
171
+
172
+ public function testInvalidProxyRelation (): void
173
+ {
174
+ $ person = $ this ->getPersonSchema ()->createRecord ();
175
+
176
+ $ this ->expectException (\RuntimeException::class);
177
+ $ person ->getRelatedModelsByProxy ('spouse ' , 'spouse ' );
178
+ }
179
+
180
+ public function testInvalidProxiedRelation (): void
181
+ {
182
+ $ house = $ this ->getPersonSchema ()->getRelationship ('home ' )->getReferencedSchema ()->createRecord ();
183
+
184
+ $ this ->expectException (\RuntimeException::class);
185
+ $ house ->getRelatedModelsByProxy ('residents ' , 'parents ' );
186
+ }
187
+
188
+ public function testEmptyProxy (): void
189
+ {
190
+ $ schema = $ this ->getPersonSchema ();
191
+
192
+ $ person = $ schema ->createRecord ();
193
+ $ parent = $ schema ->getRelationship ('parents ' )->getReferencedSchema ()->createRecord ();
194
+
195
+ $ person ['id ' ] = 1 ;
196
+ $ parent ['child_id ' ] = 1 ;
197
+
198
+ $ person ->setReferencedRecords ('parents ' , [$ parent ]);
199
+ $ parent ->setReferencedRecords ('parent ' , []);
200
+
201
+ $ this ->assertSame ([], $ person ->getRelatedModelsByProxy ('parents ' , 'parent ' ));
202
+ }
203
+ }
0 commit comments