This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\ItemMerge; |
||
12 | use tests\ItemRequired; |
||
13 | use tests\TestMigration; |
||
14 | use paulzi\jsonBehavior\JsonField; |
||
15 | |||
16 | /** |
||
17 | * @author PaulZi <[email protected]> |
||
18 | */ |
||
19 | class BehaviorTest extends \PHPUnit_Framework_TestCase |
||
20 | { |
||
21 | public function testInitEmpty() |
||
22 | { |
||
23 | $model = new JsonField(); |
||
24 | $this->assertSame((string)$model, ''); |
||
25 | |||
26 | $model = new JsonField(''); |
||
27 | $this->assertSame((string)$model, ''); |
||
28 | |||
29 | $model = new JsonField(null); |
||
30 | $this->assertSame((string)$model, ''); |
||
31 | |||
32 | $model = new JsonField([]); |
||
33 | $this->assertSame((string)$model, ''); |
||
34 | } |
||
35 | |||
36 | public function testInitString() |
||
37 | { |
||
38 | $model = new JsonField('{ "test": false }'); |
||
39 | $this->assertSame((string)$model, '{"test":false}'); |
||
40 | |||
41 | $model = new JsonField('{ "test": [1, 2, 3] }'); |
||
42 | $this->assertSame((string)$model, '{"test":[1,2,3]}'); |
||
43 | |||
44 | $model = new JsonField('{ "test": { "best": true}, "best": 2 }'); |
||
45 | $this->assertSame((string)$model, '{"test":{"best":true},"best":2}'); |
||
46 | |||
47 | $model = new JsonField('[1, false, "test", null]'); |
||
48 | $this->assertSame((string)$model, '[1,false,"test",null]'); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @expectedException \yii\base\InvalidParamException |
||
53 | */ |
||
54 | public function testInitStringParseException() |
||
55 | { |
||
56 | $model = new JsonField('{test:}'); |
||
0 ignored issues
–
show
|
|||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @expectedException \yii\base\InvalidParamException |
||
61 | */ |
||
62 | public function testInitStringNumberScalarException() |
||
63 | { |
||
64 | $model = new JsonField('0'); |
||
0 ignored issues
–
show
$model is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @expectedException \yii\base\InvalidParamException |
||
69 | */ |
||
70 | public function testInitStringNullScalarException() |
||
71 | { |
||
72 | $model = new JsonField('null'); |
||
0 ignored issues
–
show
$model is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @expectedException \yii\base\InvalidParamException |
||
77 | */ |
||
78 | public function testInitStringFalseScalarException() |
||
79 | { |
||
80 | $model = new JsonField('false'); |
||
0 ignored issues
–
show
$model is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @expectedException \yii\base\InvalidParamException |
||
85 | */ |
||
86 | public function testInitStringTrueScalarException() |
||
87 | { |
||
88 | $model = new JsonField('true'); |
||
0 ignored issues
–
show
$model is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
89 | } |
||
90 | |||
91 | public function testInitArray() |
||
92 | { |
||
93 | $model = new JsonField(['test' => false]); |
||
94 | $this->assertSame((string)$model, '{"test":false}'); |
||
95 | |||
96 | $model = new JsonField(['test' => [1, 2, 3]]); |
||
97 | $this->assertSame((string)$model, '{"test":[1,2,3]}'); |
||
98 | |||
99 | $model = new JsonField([1, false, "test", null]); |
||
100 | $this->assertSame((string)$model, '[1,false,"test",null]'); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @expectedException \yii\base\InvalidParamException |
||
105 | */ |
||
106 | public function testInitArrayException() |
||
107 | { |
||
108 | $model = new JsonField(new \stdClass()); |
||
0 ignored issues
–
show
new \stdClass() is of type object<stdClass> , but the function expects a string|array .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() $model is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
109 | } |
||
110 | |||
111 | public function testSetString() |
||
112 | { |
||
113 | $model = new JsonField(); |
||
114 | |||
115 | $model->set('{ "test": false }'); |
||
116 | $this->assertSame((string)$model, '{"test":false}'); |
||
117 | |||
118 | $model->set('{ "test": [1, 2, 3] }'); |
||
119 | $this->assertSame((string)$model, '{"test":[1,2,3]}'); |
||
120 | |||
121 | $model->set('{ "test": { "best": true}, "best": 2 }'); |
||
122 | $this->assertSame((string)$model, '{"test":{"best":true},"best":2}'); |
||
123 | |||
124 | $model->set('[1, false, "test", null]'); |
||
125 | $this->assertSame((string)$model, '[1,false,"test",null]'); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @expectedException \yii\base\InvalidParamException |
||
130 | */ |
||
131 | public function testSetStringParseException() |
||
132 | { |
||
133 | $model = new JsonField(); |
||
134 | $model->set('{test:}'); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @expectedException \yii\base\InvalidParamException |
||
139 | */ |
||
140 | public function testSetStringNumberScalarException() |
||
141 | { |
||
142 | $model = new JsonField(); |
||
143 | $model->set('0'); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @expectedException \yii\base\InvalidParamException |
||
148 | */ |
||
149 | public function testSetStringNullScalarException() |
||
150 | { |
||
151 | $model = new JsonField(); |
||
152 | $model->set('null'); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @expectedException \yii\base\InvalidParamException |
||
157 | */ |
||
158 | public function testSetStringFalseScalarException() |
||
159 | { |
||
160 | $model = new JsonField(); |
||
161 | $model->set('false'); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @expectedException \yii\base\InvalidParamException |
||
166 | */ |
||
167 | public function testSetStringTrueScalarException() |
||
168 | { |
||
169 | $model = new JsonField(); |
||
170 | $model->set('true'); |
||
171 | } |
||
172 | |||
173 | public function testSetArray() |
||
174 | { |
||
175 | $model = new JsonField(); |
||
176 | |||
177 | $model->set(['test' => false]); |
||
178 | $this->assertSame((string)$model, '{"test":false}'); |
||
179 | |||
180 | $model->set(['test' => [1, 2, 3]]); |
||
181 | $this->assertSame((string)$model, '{"test":[1,2,3]}'); |
||
182 | |||
183 | $model->set(['test' => ['best' => true], 'best' => 2]); |
||
184 | $this->assertSame((string)$model, '{"test":{"best":true},"best":2}'); |
||
185 | |||
186 | $model->set([1, false, "test", null]); |
||
187 | $this->assertSame((string)$model, '[1,false,"test",null]'); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @expectedException \yii\base\InvalidParamException |
||
192 | */ |
||
193 | public function testSetArrayException() |
||
194 | { |
||
195 | $model = new JsonField(new \stdClass()); |
||
0 ignored issues
–
show
new \stdClass() is of type object<stdClass> , but the function expects a string|array .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() $model is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
196 | } |
||
197 | |||
198 | public function testArrayObjectAccess() |
||
199 | { |
||
200 | $model = new JsonField('{ "one": { "test": true }, "two": 2, "three": [1, 2, 3], "four": "4" }'); |
||
201 | $this->assertSame($model['one'], ['test' => true]); |
||
202 | $this->assertSame($model['one']['test'], true); |
||
203 | $this->assertSame($model['two'], 2); |
||
204 | $this->assertSame($model['three'][2], 3); |
||
205 | $this->assertSame($model['four'], '4'); |
||
206 | |||
207 | $model['one']['test'] = false; |
||
208 | $model['two'] = 3; |
||
209 | $model['three'][2] = 0; |
||
210 | $model['four'] = null; |
||
211 | $this->assertSame((string)$model, '{"one":{"test":false},"two":3,"three":[1,2,0],"four":null}'); |
||
212 | } |
||
213 | |||
214 | public function testArrayArrayAccess() |
||
215 | { |
||
216 | $model = new JsonField('[1, false, "test", null]'); |
||
217 | $this->assertSame($model[0], 1); |
||
218 | $this->assertSame($model[1], false); |
||
219 | $this->assertSame($model[2], 'test'); |
||
220 | $this->assertSame($model[3], null); |
||
221 | |||
222 | $model[0] = 2; |
||
223 | $model[1] = true; |
||
224 | $model[2] = 'best'; |
||
225 | $model[3] = ['test' => 'test']; |
||
226 | $this->assertSame((string)$model, '[2,true,"best",{"test":"test"}]'); |
||
227 | } |
||
228 | |||
229 | public function testFields() |
||
230 | { |
||
231 | $model = new JsonField('{ "test": { "best": true}, "best": 2 }'); |
||
232 | $this->assertSame($model->fields(), ['test' => 'test', 'best' => 'best']); |
||
233 | } |
||
234 | |||
235 | public function testToArray() |
||
236 | { |
||
237 | $model = new JsonField('{ "test": false }'); |
||
238 | $this->assertSame($model->toArray(), ['test' => false]); |
||
239 | |||
240 | $model = new JsonField('{ "test": [1, null, 3] }'); |
||
241 | $this->assertSame($model->toArray(), ['test' => [1, null, 3]]); |
||
242 | |||
243 | $model = new JsonField('{ "test": { "best": true}, "best": 2 }'); |
||
244 | $this->assertSame($model->toArray(), ['test' => ['best' => true], 'best' => 2]); |
||
245 | |||
246 | $model = new JsonField('[1, false, "test", null]'); |
||
247 | $this->assertSame($model->toArray(), [1, false, "test", null]); |
||
248 | |||
249 | $model = new JsonField('{ "test": { "best": true}, "best": 2 }'); |
||
250 | $this->assertSame($model->toArray(['test']), ['test' => ['best' => true]]); |
||
251 | } |
||
252 | |||
253 | public function testIsEmpty() |
||
254 | { |
||
255 | $model = new JsonField(); |
||
256 | $this->assertSame($model->isEmpty(), true); |
||
257 | |||
258 | $model->set('{}'); |
||
259 | $this->assertSame($model->isEmpty(), true); |
||
260 | |||
261 | $model->set('[]'); |
||
262 | $this->assertSame($model->isEmpty(), true); |
||
263 | |||
264 | $model->set('[false]'); |
||
265 | $this->assertSame($model->isEmpty(), false); |
||
266 | |||
267 | $model->set('[0]'); |
||
268 | $this->assertSame($model->isEmpty(), false); |
||
269 | |||
270 | $model->set('[[]]'); |
||
271 | $this->assertSame($model->isEmpty(), false); |
||
272 | } |
||
273 | |||
274 | public function testBehavior() |
||
275 | { |
||
276 | $item = new Item(); |
||
277 | $item->params['one'] = 'value'; |
||
278 | $item->params['two'] = []; |
||
279 | $item->params['two']['test'] = true; |
||
280 | $this->assertSame($item->toArray(), [ |
||
281 | 'params' => [ |
||
282 | 'one' => 'value', |
||
283 | 'two' => ['test' => true], |
||
284 | ], |
||
285 | ]); |
||
286 | $this->assertSame($item->params->toArray(['one']), ['one' => 'value']); |
||
287 | $this->assertSame($item->save(false), true); |
||
288 | $item->params['one'] = 42; |
||
289 | $this->assertSame($item->params['one'], 42); |
||
290 | |||
291 | $item = Item::findOne($item->id); |
||
292 | $this->assertSame($item->params['one'], 'value'); |
||
293 | $this->assertSame($item->params['two']['test'], true); |
||
294 | } |
||
295 | |||
296 | public function testRefresh() |
||
297 | { |
||
298 | $item = new Item(); |
||
299 | $item->params['one'] = 3; |
||
300 | $item->params['two'] = 2; |
||
301 | $this->assertSame($item->save(false), true); |
||
302 | $item->params['one'] = 'one'; |
||
303 | $item->params['three'] = 3; |
||
304 | $item->refresh(); |
||
305 | $this->assertSame($item->params->toArray(), ['one' => 3, 'two' => 2]); |
||
306 | } |
||
307 | |||
308 | public function testDirtyAttributesOnUpdate() |
||
309 | { |
||
310 | $item = new Item(); |
||
311 | $testChanged = null; |
||
312 | $item->on($item::EVENT_AFTER_UPDATE, function ($event) use (&$testChanged) { |
||
313 | $this->assertSame($event->changedAttributes, $testChanged); |
||
314 | }); |
||
315 | $item->params['one'] = 3; |
||
316 | $item->params['two'] = 2; |
||
317 | $this->assertSame($item->save(false), true); |
||
318 | $testChanged = []; |
||
0 ignored issues
–
show
$testChanged is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
319 | $this->assertSame($item->update(false), 0); |
||
320 | $item->params['one'] = 1; |
||
321 | $testChanged = ['params' => '{"one":3,"two":2}']; |
||
0 ignored issues
–
show
$testChanged is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
322 | $this->assertSame($item->update(false), 1); |
||
323 | } |
||
324 | |||
325 | public function testEmptyValue() |
||
326 | { |
||
327 | $item = new Item(); |
||
328 | $this->assertSame($item->save(false), true); |
||
329 | $item->refresh(); |
||
330 | $this->assertSame($item->getOldAttribute('params'), null); |
||
331 | |||
332 | $item = new ItemMerge(); |
||
333 | $this->assertSame($item->save(false), true); |
||
334 | $item->refresh(); |
||
335 | $this->assertSame($item->getOldAttribute('params'), '{}'); |
||
336 | } |
||
337 | |||
338 | public function testValidatorTest() |
||
339 | { |
||
340 | $item = new Item(); |
||
341 | |||
342 | $item->attributes = ['params' => '{"json": true}']; |
||
343 | $this->assertSame($item->validate(), true); |
||
344 | |||
345 | $item->attributes = ['params' => ['json' => true]]; |
||
346 | $this->assertSame($item->validate(), true); |
||
347 | |||
348 | $item->attributes = ['params' => '{json:}']; |
||
349 | $this->assertSame($item->validate(), false); |
||
350 | $this->assertArrayHasKey('params', $item->errors); |
||
351 | |||
352 | $item->attributes = ['params' => 'true']; |
||
353 | $this->assertSame($item->validate(), false); |
||
354 | $this->assertArrayHasKey('params', $item->errors); |
||
355 | } |
||
356 | |||
357 | public function testValidatorMergeTest() |
||
358 | { |
||
359 | $item = new ItemMerge(); |
||
360 | $item->params['test1'] = 123; |
||
361 | $item->params['test2'] = 456; |
||
362 | $item->save(); |
||
363 | |||
364 | $item->load(['params' => '{"test2": 789}'], ''); |
||
365 | $this->assertSame($item->validate(), true); |
||
366 | $this->assertSame($item->params->toArray(), ['test1' => 123, 'test2' => 789]); |
||
367 | |||
368 | $item->load(['params' => ['test2' => 789]], ''); |
||
369 | $this->assertSame($item->validate(), true); |
||
370 | $this->assertSame($item->params->toArray(), ['test1' => 123, 'test2' => 789]); |
||
371 | } |
||
372 | |||
373 | public function testValidatorRequiredTest() |
||
374 | { |
||
375 | $item = new ItemRequired(); |
||
376 | $item->params->set(null); |
||
377 | $this->assertSame($item->validate(), false); |
||
378 | |||
379 | $item = new ItemRequired(); |
||
380 | $item->params->set('{}'); |
||
381 | $this->assertSame($item->validate(), false); |
||
382 | |||
383 | $item = new ItemRequired(); |
||
384 | $item->params['test'] = 1; |
||
385 | $this->assertSame($item->validate(), true); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @inheritdoc |
||
390 | */ |
||
391 | public static function setUpBeforeClass() |
||
392 | { |
||
393 | (new TestMigration())->up(); |
||
394 | parent::setUpBeforeClass(); |
||
395 | } |
||
396 | } |
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.