1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/paulzi/yii2-json-behavior |
4
|
|
|
* @copyright Copyright (c) 2015 PaulZi <[email protected]> |
5
|
|
|
* @license MIT (https://github.com/paulzi/yii2-json-behavior/blob/master/LICENSE) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace paulzi\jsonBehavior\tests; |
9
|
|
|
|
10
|
|
|
use tests\Item; |
11
|
|
|
use tests\TestMigration; |
12
|
|
|
use paulzi\jsonBehavior\JsonField; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author PaulZi <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class BehaviorTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
public function testInitEmpty() |
20
|
|
|
{ |
21
|
|
|
$model = new JsonField(); |
22
|
|
|
$this->assertSame((string)$model, ''); |
23
|
|
|
|
24
|
|
|
$model = new JsonField(''); |
25
|
|
|
$this->assertSame((string)$model, ''); |
26
|
|
|
|
27
|
|
|
$model = new JsonField(null); |
28
|
|
|
$this->assertSame((string)$model, ''); |
29
|
|
|
|
30
|
|
|
$model = new JsonField([]); |
31
|
|
|
$this->assertSame((string)$model, ''); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testInitString() |
35
|
|
|
{ |
36
|
|
|
$model = new JsonField('{ "test": false }'); |
37
|
|
|
$this->assertSame((string)$model, '{"test":false}'); |
38
|
|
|
|
39
|
|
|
$model = new JsonField('{ "test": [1, 2, 3] }'); |
40
|
|
|
$this->assertSame((string)$model, '{"test":[1,2,3]}'); |
41
|
|
|
|
42
|
|
|
$model = new JsonField('{ "test": { "best": true}, "best": 2 }'); |
43
|
|
|
$this->assertSame((string)$model, '{"test":{"best":true},"best":2}'); |
44
|
|
|
|
45
|
|
|
$model = new JsonField('[1, false, "test", null]'); |
46
|
|
|
$this->assertSame((string)$model, '[1,false,"test",null]'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @expectedException \yii\base\InvalidParamException |
51
|
|
|
*/ |
52
|
|
|
public function testInitStringParseException() |
53
|
|
|
{ |
54
|
|
|
$model = new JsonField('{test:}'); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @expectedException \yii\base\InvalidParamException |
59
|
|
|
*/ |
60
|
|
|
public function testInitStringNumberScalarException() |
61
|
|
|
{ |
62
|
|
|
$model = new JsonField('0'); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @expectedException \yii\base\InvalidParamException |
67
|
|
|
*/ |
68
|
|
|
public function testInitStringNullScalarException() |
69
|
|
|
{ |
70
|
|
|
$model = new JsonField('null'); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @expectedException \yii\base\InvalidParamException |
75
|
|
|
*/ |
76
|
|
|
public function testInitStringFalseScalarException() |
77
|
|
|
{ |
78
|
|
|
$model = new JsonField('false'); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @expectedException \yii\base\InvalidParamException |
83
|
|
|
*/ |
84
|
|
|
public function testInitStringTrueScalarException() |
85
|
|
|
{ |
86
|
|
|
$model = new JsonField('true'); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testInitArray() |
90
|
|
|
{ |
91
|
|
|
$model = new JsonField(['test' => false]); |
92
|
|
|
$this->assertSame((string)$model, '{"test":false}'); |
93
|
|
|
|
94
|
|
|
$model = new JsonField(['test' => [1, 2, 3]]); |
95
|
|
|
$this->assertSame((string)$model, '{"test":[1,2,3]}'); |
96
|
|
|
|
97
|
|
|
$model = new JsonField([1, false, "test", null]); |
98
|
|
|
$this->assertSame((string)$model, '[1,false,"test",null]'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @expectedException \yii\base\InvalidParamException |
103
|
|
|
*/ |
104
|
|
|
public function testInitArrayException() |
105
|
|
|
{ |
106
|
|
|
$model = new JsonField(new \stdClass()); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testSetString() |
110
|
|
|
{ |
111
|
|
|
$model = new JsonField(); |
112
|
|
|
|
113
|
|
|
$model->set('{ "test": false }'); |
114
|
|
|
$this->assertSame((string)$model, '{"test":false}'); |
115
|
|
|
|
116
|
|
|
$model->set('{ "test": [1, 2, 3] }'); |
117
|
|
|
$this->assertSame((string)$model, '{"test":[1,2,3]}'); |
118
|
|
|
|
119
|
|
|
$model->set('{ "test": { "best": true}, "best": 2 }'); |
120
|
|
|
$this->assertSame((string)$model, '{"test":{"best":true},"best":2}'); |
121
|
|
|
|
122
|
|
|
$model->set('[1, false, "test", null]'); |
123
|
|
|
$this->assertSame((string)$model, '[1,false,"test",null]'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @expectedException \yii\base\InvalidParamException |
128
|
|
|
*/ |
129
|
|
|
public function testSetStringParseException() |
130
|
|
|
{ |
131
|
|
|
$model = new JsonField(); |
132
|
|
|
$model->set('{test:}'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @expectedException \yii\base\InvalidParamException |
137
|
|
|
*/ |
138
|
|
|
public function testSetStringNumberScalarException() |
139
|
|
|
{ |
140
|
|
|
$model = new JsonField(); |
141
|
|
|
$model->set('0'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @expectedException \yii\base\InvalidParamException |
146
|
|
|
*/ |
147
|
|
|
public function testSetStringNullScalarException() |
148
|
|
|
{ |
149
|
|
|
$model = new JsonField(); |
150
|
|
|
$model->set('null'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @expectedException \yii\base\InvalidParamException |
155
|
|
|
*/ |
156
|
|
|
public function testSetStringFalseScalarException() |
157
|
|
|
{ |
158
|
|
|
$model = new JsonField(); |
159
|
|
|
$model->set('false'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @expectedException \yii\base\InvalidParamException |
164
|
|
|
*/ |
165
|
|
|
public function testSetStringTrueScalarException() |
166
|
|
|
{ |
167
|
|
|
$model = new JsonField(); |
168
|
|
|
$model->set('true'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testSetArray() |
172
|
|
|
{ |
173
|
|
|
$model = new JsonField(); |
174
|
|
|
|
175
|
|
|
$model->set(['test' => false]); |
176
|
|
|
$this->assertSame((string)$model, '{"test":false}'); |
177
|
|
|
|
178
|
|
|
$model->set(['test' => [1, 2, 3]]); |
179
|
|
|
$this->assertSame((string)$model, '{"test":[1,2,3]}'); |
180
|
|
|
|
181
|
|
|
$model->set(['test' => ['best' => true], 'best' => 2]); |
182
|
|
|
$this->assertSame((string)$model, '{"test":{"best":true},"best":2}'); |
183
|
|
|
|
184
|
|
|
$model->set([1, false, "test", null]); |
185
|
|
|
$this->assertSame((string)$model, '[1,false,"test",null]'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @expectedException \yii\base\InvalidParamException |
190
|
|
|
*/ |
191
|
|
|
public function testSetArrayException() |
192
|
|
|
{ |
193
|
|
|
$model = new JsonField(new \stdClass()); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testArrayObjectAccess() |
197
|
|
|
{ |
198
|
|
|
$model = new JsonField('{ "one": { "test": true }, "two": 2, "three": [1, 2, 3], "four": "4" }'); |
199
|
|
|
$this->assertSame($model['one'], ['test' => true]); |
200
|
|
|
$this->assertSame($model['one']['test'], true); |
201
|
|
|
$this->assertSame($model['two'], 2); |
202
|
|
|
$this->assertSame($model['three'][2], 3); |
203
|
|
|
$this->assertSame($model['four'], '4'); |
204
|
|
|
|
205
|
|
|
$model['one']['test'] = false; |
206
|
|
|
$model['two'] = 3; |
207
|
|
|
$model['three'][2] = 0; |
208
|
|
|
$model['four'] = null; |
209
|
|
|
$this->assertSame((string)$model, '{"one":{"test":false},"two":3,"three":[1,2,0],"four":null}'); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function testArrayArrayAccess() |
213
|
|
|
{ |
214
|
|
|
$model = new JsonField('[1, false, "test", null]'); |
215
|
|
|
$this->assertSame($model[0], 1); |
216
|
|
|
$this->assertSame($model[1], false); |
217
|
|
|
$this->assertSame($model[2], 'test'); |
218
|
|
|
$this->assertSame($model[3], null); |
219
|
|
|
|
220
|
|
|
$model[0] = 2; |
221
|
|
|
$model[1] = true; |
222
|
|
|
$model[2] = 'best'; |
223
|
|
|
$model[3] = ['test' => 'test']; |
224
|
|
|
$this->assertSame((string)$model, '[2,true,"best",{"test":"test"}]'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function testFields() |
228
|
|
|
{ |
229
|
|
|
$model = new JsonField('{ "test": { "best": true}, "best": 2 }'); |
230
|
|
|
$this->assertSame($model->fields(), ['test' => 'test', 'best' => 'best']); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function testToArray() |
234
|
|
|
{ |
235
|
|
|
$model = new JsonField('{ "test": false }'); |
236
|
|
|
$this->assertSame($model->toArray(), ['test' => false]); |
237
|
|
|
|
238
|
|
|
$model = new JsonField('{ "test": [1, null, 3] }'); |
239
|
|
|
$this->assertSame($model->toArray(), ['test' => [1, null, 3]]); |
240
|
|
|
|
241
|
|
|
$model = new JsonField('{ "test": { "best": true}, "best": 2 }'); |
242
|
|
|
$this->assertSame($model->toArray(), ['test' => ['best' => true], 'best' => 2]); |
243
|
|
|
|
244
|
|
|
$model = new JsonField('[1, false, "test", null]'); |
245
|
|
|
$this->assertSame($model->toArray(), [1, false, "test", null]); |
246
|
|
|
|
247
|
|
|
$model = new JsonField('{ "test": { "best": true}, "best": 2 }'); |
248
|
|
|
$this->assertSame($model->toArray(['test']), ['test' => ['best' => true]]); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function testIsEmpty() |
252
|
|
|
{ |
253
|
|
|
$model = new JsonField(); |
254
|
|
|
$this->assertSame($model->isEmpty(), true); |
255
|
|
|
|
256
|
|
|
$model->set('{}'); |
257
|
|
|
$this->assertSame($model->isEmpty(), true); |
258
|
|
|
|
259
|
|
|
$model->set('[]'); |
260
|
|
|
$this->assertSame($model->isEmpty(), true); |
261
|
|
|
|
262
|
|
|
$model->set('[false]'); |
263
|
|
|
$this->assertSame($model->isEmpty(), false); |
264
|
|
|
|
265
|
|
|
$model->set('[0]'); |
266
|
|
|
$this->assertSame($model->isEmpty(), false); |
267
|
|
|
|
268
|
|
|
$model->set('[[]]'); |
269
|
|
|
$this->assertSame($model->isEmpty(), false); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
public function testBehavior() |
273
|
|
|
{ |
274
|
|
|
$item = new Item(); |
275
|
|
|
$item->params['one'] = 'value'; |
276
|
|
|
$item->params['two'] = []; |
277
|
|
|
$item->params['two']['test'] = true; |
278
|
|
|
$this->assertSame($item->toArray(), [ |
279
|
|
|
'params' => [ |
280
|
|
|
'one' => 'value', |
281
|
|
|
'two' => ['test' => true], |
282
|
|
|
], |
283
|
|
|
]); |
284
|
|
|
$this->assertSame($item->params->toArray(['one']), ['one' => 'value']); |
285
|
|
|
$this->assertSame($item->save(false), true); |
286
|
|
|
$item->params['one'] = 42; |
287
|
|
|
$this->assertSame($item->params['one'], 42); |
288
|
|
|
|
289
|
|
|
$item = Item::findOne($item->id); |
290
|
|
|
$this->assertSame($item->params['one'], 'value'); |
291
|
|
|
$this->assertSame($item->params['two']['test'], true); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function testRefresh() |
295
|
|
|
{ |
296
|
|
|
$item = new Item(); |
297
|
|
|
$item->params['one'] = 3; |
298
|
|
|
$item->params['two'] = 2; |
299
|
|
|
$this->assertSame($item->save(false), true); |
300
|
|
|
$item->params['one'] = 'one'; |
301
|
|
|
$item->params['three'] = 3; |
302
|
|
|
$item->refresh(); |
303
|
|
|
$this->assertSame($item->params->toArray(), ['one' => 3, 'two' => 2]); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function testDirtyAttributesOnUpdate() |
307
|
|
|
{ |
308
|
|
|
$item = new Item(); |
309
|
|
|
$testChanged = null; |
310
|
|
|
$item->on($item::EVENT_AFTER_UPDATE, function ($event) use (&$testChanged) { |
311
|
|
|
$this->assertSame($event->changedAttributes, $testChanged); |
312
|
|
|
}); |
313
|
|
|
$item->params['one'] = 3; |
314
|
|
|
$item->params['two'] = 2; |
315
|
|
|
$this->assertSame($item->save(false), true); |
316
|
|
|
$testChanged = []; |
|
|
|
|
317
|
|
|
$this->assertSame($item->update(false), 0); |
318
|
|
|
$item->params['one'] = 1; |
319
|
|
|
$testChanged = ['params' => '{"one":3,"two":2}']; |
|
|
|
|
320
|
|
|
$this->assertSame($item->update(false), 1); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function testValidatorTest() |
324
|
|
|
{ |
325
|
|
|
$item = new Item(); |
326
|
|
|
|
327
|
|
|
$item->attributes = ['params' => '{"json": true}']; |
328
|
|
|
$this->assertSame($item->validate(), true); |
329
|
|
|
|
330
|
|
|
$item->attributes = ['params' => ['json' => true]]; |
331
|
|
|
$this->assertSame($item->validate(), true); |
332
|
|
|
|
333
|
|
|
$item->attributes = ['params' => '{json:}']; |
334
|
|
|
$this->assertSame($item->validate(), false); |
335
|
|
|
$this->assertArrayHasKey('params', $item->errors); |
336
|
|
|
|
337
|
|
|
$item->attributes = ['params' => 'true']; |
338
|
|
|
$this->assertSame($item->validate(), false); |
339
|
|
|
$this->assertArrayHasKey('params', $item->errors); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @inheritdoc |
344
|
|
|
*/ |
345
|
|
|
public static function setUpBeforeClass() |
346
|
|
|
{ |
347
|
|
|
(new TestMigration())->up(); |
348
|
|
|
parent::setUpBeforeClass(); |
349
|
|
|
} |
350
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.