GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 20a803...a4a304 )
by Pavel
38:30
created

BehaviorTest::testValidatorRequiredTest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
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
Unused Code introduced by
$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 $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.

Loading history...
57
    }
58
59
    /**
60
     * @expectedException \yii\base\InvalidParamException
61
     */
62
    public function testInitStringNumberScalarException()
63
    {
64
        $model = new JsonField('0');
0 ignored issues
show
Unused Code introduced by
$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 $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.

Loading history...
65
    }
66
67
    /**
68
     * @expectedException \yii\base\InvalidParamException
69
     */
70
    public function testInitStringNullScalarException()
71
    {
72
        $model = new JsonField('null');
0 ignored issues
show
Unused Code introduced by
$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 $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.

Loading history...
73
    }
74
75
    /**
76
     * @expectedException \yii\base\InvalidParamException
77
     */
78
    public function testInitStringFalseScalarException()
79
    {
80
        $model = new JsonField('false');
0 ignored issues
show
Unused Code introduced by
$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 $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.

Loading history...
81
    }
82
83
    /**
84
     * @expectedException \yii\base\InvalidParamException
85
     */
86
    public function testInitStringTrueScalarException()
87
    {
88
        $model = new JsonField('true');
0 ignored issues
show
Unused Code introduced by
$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 $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.

Loading history...
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
Documentation introduced by
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);
Loading history...
Unused Code introduced by
$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 $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.

Loading history...
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
Documentation introduced by
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);
Loading history...
Unused Code introduced by
$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 $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.

Loading history...
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
Unused Code introduced by
$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 $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.

Loading history...
319
        $this->assertSame($item->update(false), 0);
320
        $item->params['one'] = 1;
321
        $testChanged = ['params' => '{"one":3,"two":2}'];
0 ignored issues
show
Unused Code introduced by
$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 $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.

Loading history...
322
        $this->assertSame($item->update(false), 1);
323
    }
324
325
    public function testValidatorTest()
326
    {
327
        $item = new Item();
328
329
        $item->attributes = ['params' => '{"json": true}'];
330
        $this->assertSame($item->validate(), true);
331
332
        $item->attributes = ['params' => ['json' => true]];
333
        $this->assertSame($item->validate(), true);
334
335
        $item->attributes = ['params' => '{json:}'];
336
        $this->assertSame($item->validate(), false);
337
        $this->assertArrayHasKey('params', $item->errors);
338
339
        $item->attributes = ['params' => 'true'];
340
        $this->assertSame($item->validate(), false);
341
        $this->assertArrayHasKey('params', $item->errors);
342
    }
343
344
    public function testValidatorMergeTest()
345
    {
346
        $item = new ItemMerge();
347
        $item->params['test1'] = 123;
348
        $item->params['test2'] = 456;
349
        $item->save();
350
351
        $item->load(['params' => '{"test2": 789}'], '');
352
        $this->assertSame($item->validate(), true);
353
        $this->assertSame($item->params->toArray(), ['test1' => 123, 'test2' => 789]);
354
355
        $item->load(['params' => ['test2' => 789]], '');
356
        $this->assertSame($item->validate(), true);
357
        $this->assertSame($item->params->toArray(), ['test1' => 123, 'test2' => 789]);
358
    }
359
360
    public function testValidatorRequiredTest()
361
    {
362
        $item = new ItemRequired();
363
        $item->params->set(null);
364
        $this->assertSame($item->validate(), false);
365
366
        $item = new ItemRequired();
367
        $item->params->set('{}');
368
        $this->assertSame($item->validate(), false);
369
370
        $item = new ItemRequired();
371
        $item->params['test'] = 1;
372
        $this->assertSame($item->validate(), true);
373
    }
374
375
    /**
376
     * @inheritdoc
377
     */
378
    public static function setUpBeforeClass()
379
    {
380
        (new TestMigration())->up();
381
        parent::setUpBeforeClass();
382
    }
383
}