1
+ <?php
2
+ final class AbstractProtoClassFillQueryTest extends TestCase
3
+ {
4
+ public function testFullInsertQueryByCity ()
5
+ {
6
+ $ city = $ this ->spawnCity ();
7
+
8
+ $ insertCity = $ city ->proto ()->fillQuery (OSQL ::insert (), $ city );
9
+ $ this ->assertEquals (
10
+ 'INSERT INTO (id, name, capital, large) VALUES (20, Saint-Peterburg, TRUE, TRUE) ' ,
11
+ $ insertCity ->toDialectString (ImaginaryDialect::me ())
12
+ );
13
+ }
14
+
15
+ public function testFullUpdateQueryByUser ()
16
+ {
17
+ $ city = $ this ->spawnCity ();
18
+ $ user = $ this ->spawnUser (array ('city ' => $ city ));
19
+ $ updateUser = $ user ->proto ()->fillQuery (OSQL ::update (), $ user );
20
+ $ this ->assertEquals (
21
+ 'UPDATE SET id = 77, nickname = NULL, password = NULL, '
22
+ .'very_custom_field_name = 2011-12-31 00:00:00, '
23
+ .'registered = 2011-12-30 00:00:00, strange_time = 01:23:45, '
24
+ .'city_id = 20, first_optional_id = NULL, second_optional_id = NULL, '
25
+ .'url = https://www.github.com, '
26
+ .'properties = "a"=>"apple","b"=>"bananas",, ip = 127.0.0.1 ' ,
27
+ $ updateUser ->toDialectString (ImaginaryDialect::me ())
28
+ );
29
+ }
30
+
31
+ public function testFullUpdateQueryByUserWithContactExt ()
32
+ {
33
+ $ contactValue = $ this ->spawnContactValueExt ();
34
+ $ user = $ this ->spawnUserWithContactExt (array ('contactExt ' => $ contactValue ));
35
+
36
+ $ updateUser = $ user ->proto ()->fillQuery (OSQL ::update (), $ user );
37
+ $ this ->assertEquals (
38
+ 'UPDATE SET id = 77, name = Aleksey, surname = Alekseev, email = [email protected] , '
39
+ .'icq = 12345678, phone = 89012345678, city_id = NULL, '
40
+ .'web = https://www.github.com/, skype = github ' ,
41
+ $ updateUser ->toDialectString (ImaginaryDialect::me ())
42
+ );
43
+ }
44
+
45
+ public function testUpdateQueryByCityOneBoolean ()
46
+ {
47
+ $ cityOld = $ this ->spawnCity (array ('capital ' => false ));
48
+ $ city = $ this ->spawnCity (array ('capital ' => true )); //1918
49
+
50
+ $ updateCity = $ city ->proto ()->fillQuery (OSQL ::update (), $ city , $ cityOld );
51
+ $ this ->assertEquals (
52
+ 'UPDATE SET capital = TRUE ' ,
53
+ $ updateCity ->toDialectString (ImaginaryDialect::me ())
54
+ );
55
+ }
56
+
57
+ public function testUpdateQueryByCityOneString ()
58
+ {
59
+ $ cityOld = $ this ->spawnCity (array ('name ' => 'Leningrad ' ));
60
+ $ city = $ this ->spawnCity (array ('name ' => 'Saint-Peterburg ' ));
61
+
62
+ $ updateCity = $ city ->proto ()->fillQuery (OSQL ::update (), $ city , $ cityOld );
63
+ $ this ->assertEquals (
64
+ 'UPDATE SET name = Saint-Peterburg ' ,
65
+ $ updateCity ->toDialectString (ImaginaryDialect::me ())
66
+ );
67
+ }
68
+
69
+ public function testUpdateQueryByUserTimesAndUrl ()
70
+ {
71
+ $ oldUser = $ this ->spawnUser (array (
72
+ 'strangeTime ' => Time::create ('12:12:12 ' ),
73
+ 'url ' => HttpUrl::create ()->parse ('http://code.google.com/ ' ),
74
+ ));
75
+ $ user = $ this ->spawnUser (array ('lastLogin ' => Timestamp::create ('2012-01-01 ' )));
76
+
77
+ $ updateUser = $ user ->proto ()->fillQuery (OSQL ::update (), $ user , $ oldUser );
78
+ $ this ->assertEquals (
79
+ 'UPDATE SET very_custom_field_name = 2012-01-01 00:00:00, '
80
+ .'strange_time = 01:23:45, url = https://www.github.com ' ,
81
+ $ updateUser ->toDialectString (ImaginaryDialect::me ())
82
+ );
83
+ }
84
+
85
+ public function testUpdateQueryByUserCitiesAndHstore ()
86
+ {
87
+ $ moscow = $ this ->spawnCity (array ('id ' => 1 , 'name ' => 'Moscow ' ));
88
+ $ piter = $ this ->spawnCity (array ('id ' => 2 , 'name ' => 'Saint-Peterburg ' ));
89
+ $ omsk = $ this ->spawnCity (array ('id ' => 3 , 'name ' => 'Omsk ' ));
90
+
91
+ $ userParams = array (
92
+ 'city ' => $ moscow ,
93
+ 'firstOptional ' => $ piter ,
94
+ 'secondOptional ' => null ,
95
+ 'properties ' => Hstore::make (array ()),
96
+ );
97
+ $ oldUser = $ this ->spawnUser ($ userParams );
98
+ $ userParams = array (
99
+ 'city ' => $ piter ,
100
+ 'firstOptional ' => null ,
101
+ 'secondOptional ' => $ omsk ,
102
+ 'properties ' => Hstore::make (array ('param ' => 'value ' )),
103
+ );
104
+ $ user = $ this ->spawnUser ($ userParams );
105
+
106
+ $ updateUser = $ user ->proto ()->fillQuery (OSQL ::update (), $ user , $ oldUser );
107
+ $ this ->assertEquals (
108
+ 'UPDATE SET city_id = 2, first_optional_id = NULL, '
109
+ .'second_optional_id = 3, properties = "param"=>"value", ' ,
110
+ $ updateUser ->toDialectString (ImaginaryDialect::me ())
111
+ );
112
+ }
113
+
114
+ public function testUpdateQueryByUserWithValueObject ()
115
+ {
116
+ $ moscow = $ this ->spawnCity ();
117
+ $ oldContactExt =
$ this ->
spawnContactValueExt (
array (
'email ' =>
'[email protected] ' ));
118
+ $ oldUser = $ this ->spawnUserWithContactExt (array ('contactExt ' => $ oldContactExt ));
119
+ $ contactExt = $ this ->spawnContactValueExt (array ('city ' => $ moscow ));
120
+ $ user = $ this ->spawnUserWithContactExt (array ('contactExt ' => $ contactExt ));
121
+
122
+ $ updateUser = $ user ->proto ()->fillQuery (OSQL ::update (), $ user , $ oldUser );
123
+ $ this ->assertEquals (
124
+ 'UPDATE SET email = [email protected] , city_id = 20 ' ,
125
+ $ updateUser ->toDialectString (ImaginaryDialect::me ())
126
+ );
127
+ }
128
+
129
+ public function testUpdateQueryByUserWithSameValueObject ()
130
+ {
131
+ //if value object same for both main objects - we'll update all fields from value object
132
+ $ contactExt = $ this ->spawnContactValueExt ();
133
+ $ oldUser = $ this ->spawnUserWithContactExt (array ('contactExt ' => $ contactExt ));
134
+ $ user = $ this ->spawnUserWithContactExt (array ('contactExt ' => $ contactExt ));
135
+
136
+ $ updateUser = $ user ->proto ()->fillQuery (OSQL ::update (), $ user , $ oldUser );
137
+ $ this ->assertEquals (
138
+ 'UPDATE SET email = [email protected] , icq = 12345678, '
139
+ .'phone = 89012345678, city_id = NULL, '
140
+ .'web = https://www.github.com/, skype = github ' ,
141
+ $ updateUser ->toDialectString (ImaginaryDialect::me ())
142
+ );
143
+ }
144
+
145
+ /**
146
+ * @return TestCity
147
+ */
148
+ private function spawnCity ($ options = array ())
149
+ {
150
+ $ options += array (
151
+ 'capital ' => true ,
152
+ 'large ' => true ,
153
+ 'name ' => 'Saint-Peterburg ' ,
154
+ 'id ' => 20 ,
155
+ );
156
+
157
+ return $ this ->spawnObject (TestCity::create (), $ options );
158
+ }
159
+
160
+ /**
161
+ * @return TestUser
162
+ */
163
+ private function spawnUser ($ options = array ())
164
+ {
165
+ $ options += array (
166
+ 'id ' => '77 ' ,
167
+ 'credentials ' => Credentials::create (),
168
+ 'lastLogin ' => Timestamp::create ('2011-12-31 ' ),
169
+ 'registered ' => Timestamp::create ('2011-12-30 ' ),
170
+ 'strangeTime ' => Time::create ('01:23:45 ' ),
171
+ 'city ' => null ,
172
+ 'firstOptional ' => null ,
173
+ 'secondOptional ' => null ,
174
+ 'url ' => HttpUrl::create ()->parse ('https://www.github.com ' ),
175
+ 'properties ' => Hstore::make (array ('a ' => 'apple ' , 'b ' => 'bananas ' )),
176
+ 'ip ' => IpAddress::create ('127.0.0.1 ' ),
177
+ );
178
+
179
+ return $ this ->spawnObject (TestUser::create (), $ options );
180
+
181
+ }
182
+
183
+ /**
184
+ * @return TestContactValueExtended
185
+ */
186
+ private function spawnContactValueExt ($ options = array ())
187
+ {
188
+ $ options += array (
189
+ 'web ' => 'https://www.github.com/ ' ,
190
+ 'skype ' => 'github ' ,
191
+
192
+ 'icq ' => 12345678 ,
193
+ 'phone ' => '89012345678 ' ,
194
+ 'city ' => null ,
195
+ );
196
+
197
+ return $ this ->spawnObject (TestContactValueExtended::create (), $ options );
198
+ }
199
+
200
+ /**
201
+ * @return TestUserWithContactExtended
202
+ */
203
+ private function spawnUserWithContactExt ($ options = array ())
204
+ {
205
+ $ options += array (
206
+ 'id ' => '77 ' ,
207
+ 'name ' => 'Aleksey ' ,
208
+ 'surname ' => 'Alekseev ' ,
209
+ 'contactExt ' => null ,
210
+ );
211
+
212
+ return $ this ->spawnObject (TestUserWithContactExtended::create (), $ options );
213
+ }
214
+
215
+ private function spawnObject (Prototyped $ object , array $ options )
216
+ {
217
+ foreach ($ object ->proto ()->getPropertyList () as $ propName => $ property ) {
218
+ /* @var $property LightMetaProperty */
219
+ if (isset ($ options [$ propName ])) {
220
+ $ setter = $ property ->getSetter ();
221
+ $ object ->{$ setter }($ options [$ propName ]);
222
+ }
223
+ }
224
+ return $ object ;
225
+ }
226
+ }
227
+ ?>
0 commit comments