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 ( 82fcf2...d8fb5e )
by Pavel
01:55
created

BehaviorTest::testBehavior()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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