Test Setup Failed
Push — test ( 24878d...6ecee8 )
by Jonathan
02:38
created

BasicObjectTest::testRemoveRepresentation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Kint\Test\Object;
4
5
use Kint\Object\BasicObject;
6
use Kint\Object\Representation\Representation;
7
8
class BasicObjectTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @covers \Kint\Object\BasicObject::getRepresentations
12
     * @covers \Kint\Object\BasicObject::addRepresentation
13
     */
14
    public function testAddRepresentation()
15
    {
16
        $o = new BasicObject();
17
18
        $this->assertSame(array(), $o->getRepresentations());
19
        $this->assertSame(null, $o->value);
20
21
        $this->assertTrue($o->addRepresentation($r1 = new Representation('Rep 1')));
22
        $this->assertSame(
23
            array(
24
                'rep_1' => $r1,
25
            ),
26
            $o->getRepresentations()
27
        );
28
        $this->assertSame(null, $o->value);
29
30
        $this->assertFalse($o->addRepresentation($r1));
31
        $this->assertSame(
32
            array(
33
                'rep_1' => $r1,
34
            ),
35
            $o->getRepresentations()
36
        );
37
38
        $this->assertTrue($o->addRepresentation($r2 = new Representation('Rep 2')));
39
        $this->assertSame(
40
            array(
41
                'rep_1' => $r1,
42
                'rep_2' => $r2,
43
            ),
44
            $o->getRepresentations()
45
        );
46
47
        $this->assertTrue($o->addRepresentation($r3 = new Representation('Rep 3'), 0));
48
        $this->assertSame(
49
            array(
50
                'rep_3' => $r3,
51
                'rep_1' => $r1,
52
                'rep_2' => $r2,
53
            ),
54
            $o->getRepresentations()
55
        );
56
57
        $this->assertTrue($o->addRepresentation($r4 = new Representation('Rep 4'), 1));
58
        $this->assertSame(
59
            array(
60
                'rep_3' => $r3,
61
                'rep_4' => $r4,
62
                'rep_1' => $r1,
63
                'rep_2' => $r2,
64
            ),
65
            $o->getRepresentations()
66
        );
67
68
        $this->assertTrue($o->addRepresentation($r5 = new Representation('Rep 5'), 100));
69
        $this->assertSame(
70
            array(
71
                'rep_3' => $r3,
72
                'rep_4' => $r4,
73
                'rep_1' => $r1,
74
                'rep_2' => $r2,
75
                'rep_5' => $r5,
76
            ),
77
            $o->getRepresentations()
78
        );
79
80
        $this->assertTrue($o->addRepresentation($r6 = new Representation('Rep 6'), -100));
81
        $this->assertSame(
82
            array(
83
                'rep_6' => $r6,
84
                'rep_3' => $r3,
85
                'rep_4' => $r4,
86
                'rep_1' => $r1,
87
                'rep_2' => $r2,
88
                'rep_5' => $r5,
89
            ),
90
            $o->getRepresentations()
91
        );
92
93
        $this->assertSame(null, $o->value);
94
    }
95
96
    /**
97
     * @covers \Kint\Object\BasicObject::replaceRepresentation
98
     */
99
    public function testReplaceRepresentation()
100
    {
101
        $o = new BasicObject();
102
        $o->addRepresentation($r1 = new Representation('Rep 1'));
103
        $o->addRepresentation($r2 = new Representation('Rep 2'));
104
        $o->addRepresentation($r3 = new Representation('Rep 3'));
105
106
        $this->assertSame(
107
            array(
108
                'rep_1' => $r1,
109
                'rep_2' => $r2,
110
                'rep_3' => $r3,
111
            ),
112
            $o->getRepresentations()
113
        );
114
115
        $o->replaceRepresentation($r2_2 = new Representation('Rep 2'));
116
117
        $this->assertSame(
118
            array(
119
                'rep_1' => $r1,
120
                'rep_2' => $r2_2,
121
                'rep_3' => $r3,
122
            ),
123
            $o->getRepresentations()
124
        );
125
126
        $o->replaceRepresentation($r2, 0);
127
128
        $this->assertSame(
129
            array(
130
                'rep_2' => $r2,
131
                'rep_1' => $r1,
132
                'rep_3' => $r3,
133
            ),
134
            $o->getRepresentations()
135
        );
136
137
        $o->replaceRepresentation($r2_2, 1);
138
139
        $this->assertSame(
140
            array(
141
                'rep_1' => $r1,
142
                'rep_2' => $r2_2,
143
                'rep_3' => $r3,
144
            ),
145
            $o->getRepresentations()
146
        );
147
    }
148
149
    /**
150
     * @covers \Kint\Object\BasicObject::replaceRepresentation
151
     */
152
    public function testRemoveRepresentation()
153
    {
154
        $o = new BasicObject();
155
        $o->addRepresentation($r1 = new Representation('Rep 1'));
156
        $o->addRepresentation($r2 = new Representation('Rep 2'));
157
        $o->addRepresentation($r3 = new Representation('Rep 3'));
158
159
        $this->assertSame(
160
            array(
161
                'rep_1' => $r1,
162
                'rep_2' => $r2,
163
                'rep_3' => $r3,
164
            ),
165
            $o->getRepresentations()
166
        );
167
168
        $o->removeRepresentation('rep_2');
169
170
        $this->assertSame(
171
            array(
172
                'rep_1' => $r1,
173
                'rep_3' => $r3,
174
            ),
175
            $o->getRepresentations()
176
        );
177
    }
178
179
    /**
180
     * @covers \Kint\Object\BasicObject::replaceRepresentation
181
     */
182
    public function testGetRepresentation()
183
    {
184
        $o = new BasicObject();
185
        $o->addRepresentation($r1 = new Representation('Rep 1'));
186
        $o->addRepresentation($r2 = new Representation('Rep 2'));
187
        $o->addRepresentation($r3 = new Representation('Rep 3'));
188
189
        $this->assertSame($r1, $o->getRepresentation('rep_1'));
190
        $this->assertSame($r2, $o->getRepresentation('rep_2'));
191
        $this->assertSame($r3, $o->getRepresentation('rep_3'));
192
        $this->assertSame(null, $o->getRepresentation('Non-existant representation name'));
193
    }
194
195
    /**
196
     * @covers \Kint\Object\BasicObject::getRepresentations
197
     */
198
    public function testGetRepresentations()
199
    {
200
        $o = new BasicObject();
201
        $o->addRepresentation($r1 = new Representation('Rep 1'));
202
        $o->addRepresentation($r2 = new Representation('Rep 2'));
203
        $o->addRepresentation($r3 = new Representation('Rep 3'));
204
205
        $this->assertSame(
206
            array(
207
                'rep_1' => $r1,
208
                'rep_2' => $r2,
209
                'rep_3' => $r3,
210
            ),
211
            $o->getRepresentations()
212
        );
213
    }
214
215
    /**
216
     * @covers \Kint\Object\BasicObject::clearRepresentations
217
     */
218
    public function testClearRepresentations()
219
    {
220
        $o = new BasicObject();
221
        $o->addRepresentation($r1 = new Representation('Rep 1'));
222
        $o->addRepresentation(new Representation('Rep 2'));
223
        $o->addRepresentation(new Representation('Rep 3'));
224
        $o->value = $r1;
225
226
        $o->clearRepresentations();
227
228
        $this->assertSame(array(), $o->getRepresentations());
229
        $this->assertSame($r1, $o->value);
230
    }
231
232
    /**
233
     * @covers \Kint\Object\BasicObject::getType
234
     */
235
    public function testGetType()
236
    {
237
        $o = new BasicObject();
238
        $o->type = 'array';
239
        $this->assertEquals('array', $o->getType());
240
    }
241
242
    public function modifierProvider()
243
    {
244
        return array(
245
            'public' => array(
246
                false,
247
                false,
248
                BasicObject::ACCESS_PUBLIC,
249
                'public',
250
            ),
251
            'public const' => array(
252
                true,
253
                false,
254
                BasicObject::ACCESS_PUBLIC,
255
                'public const',
256
            ),
257
            'public static' => array(
258
                false,
259
                true,
260
                BasicObject::ACCESS_PUBLIC,
261
                'public static',
262
            ),
263
            'protected' => array(
264
                false,
265
                false,
266
                BasicObject::ACCESS_PROTECTED,
267
                'protected',
268
            ),
269
            'private' => array(
270
                false,
271
                false,
272
                BasicObject::ACCESS_PRIVATE,
273
                'private',
274
            ),
275
            'none' => array(
276
                false,
277
                false,
278
                BasicObject::ACCESS_NONE,
279
                null,
280
            ),
281
            'private static' => array(
282
                false,
283
                true,
284
                BasicObject::ACCESS_PRIVATE,
285
                'private static',
286
            ),
287
            'public const static' => array(
288
                true,
289
                true,
290
                BasicObject::ACCESS_PUBLIC,
291
                'public const static',
292
            ),
293
            'const' => array(
294
                true,
295
                false,
296
                BasicObject::ACCESS_NONE,
297
                'const',
298
            ),
299
        );
300
        $accesses = array(
301
            BasicObject::ACCESS_NONE,
302
            BasicObject::ACCESS_PUBLIC,
303
            BasicObject::ACCESS_PROTECTED,
304
            BasicObject::ACCESS_PRIVATE,
305
        );
306
    }
307
308
    /**
309
     * @dataProvider modifierProvider
310
     * @covers \Kint\Object\BasicObject::getModifiers
311
     */
312
    public function testGetModifiers($const, $static, $access, $expect)
313
    {
314
        $o = new BasicObject();
315
        $o->const = $const;
316
        $o->static = $static;
317
        $o->access = $access;
318
        $this->assertSame($expect, $o->getModifiers());
319
    }
320
321
    /**
322
     * @covers \Kint\Object\BasicObject::getAccess
323
     */
324
    public function testGetAccess()
325
    {
326
        $o = new BasicObject();
327
        $this->assertNull($o->getAccess());
328
        $o->access = BasicObject::ACCESS_PUBLIC;
329
        $this->assertEquals('public', $o->getAccess());
330
        $o->access = BasicObject::ACCESS_PROTECTED;
331
        $this->assertEquals('protected', $o->getAccess());
332
        $o->access = BasicObject::ACCESS_PRIVATE;
333
        $this->assertEquals('private', $o->getAccess());
334
    }
335
336
    /**
337
     * @covers \Kint\Object\BasicObject::getName
338
     */
339
    public function testGetName()
340
    {
341
        $o = new BasicObject();
342
        $o->name = '$var';
343
        $this->assertEquals('$var', $o->getName());
344
        $o->name = '($a + $b)';
345
        $this->assertEquals('($a + $b)', $o->getName());
346
        $o->name = 'This is just a name, nothing more, nothing less.';
347
        $this->assertEquals('This is just a name, nothing more, nothing less.', $o->getName());
348
    }
349
350
    /**
351
     * @covers \Kint\Object\BasicObject::getOperator
352
     */
353
    public function testGetOperator()
354
    {
355
        $o = new BasicObject();
356
        $this->assertNull($o->getOperator());
357
        $o->operator = BasicObject::OPERATOR_NONE;
358
        $this->assertNull($o->getOperator());
359
        $o->operator = BasicObject::OPERATOR_ARRAY;
360
        $this->assertEquals('=>', $o->getOperator());
361
        $o->operator = BasicObject::OPERATOR_OBJECT;
362
        $this->assertEquals('->', $o->getOperator());
363
        $o->operator = BasicObject::OPERATOR_STATIC;
364
        $this->assertEquals('::', $o->getOperator());
365
    }
366
367
    /**
368
     * @covers \Kint\Object\BasicObject::getSize
369
     */
370
    public function testGetSize()
371
    {
372
        $o = new BasicObject();
373
        $this->assertNull($o->getSize());
374
        $o->size = 0;
375
        $this->assertEquals(0, $o->getSize());
376
        $o->size = 42;
377
        $this->assertEquals(42, $o->getSize());
378
    }
379
380
    /**
381
     * @covers \Kint\Object\BasicObject::getValueShort
382
     */
383
    public function testGetValueShort()
384
    {
385
        $o = new BasicObject();
386
        $r = new Representation('contents');
387
        $this->assertNull($o->getValueShort());
388
        $o->value = $r;
389
390
        $r->contents = true;
391
        $this->assertNull($o->getValueShort());
392
        $o->type = 'boolean';
393
        $this->assertEquals('true', $o->getValueShort());
394
        $r->contents = false;
395
        $this->assertEquals('false', $o->getValueShort());
396
        $o->type = 'integer';
397
        $r->contents = 1234;
398
        $this->assertEquals(1234, $o->getValueShort());
399
        $o->type = 'double';
400
        $r->contents = 1234.5678;
401
        $this->assertEquals(1234.5678, $o->getValueShort());
402
        $o->type = 'array';
403
        $r->contents = array();
404
        $this->assertNull($o->getValueShort());
405
        $o->type = 'string';
406
        $r->contents = 'string';
407
        $this->assertNull($o->getValueShort());
408
    }
409
410
    /**
411
     * @covers \Kint\Object\BasicObject::getAccessPath
412
     */
413
    public function testGetAccessPath()
414
    {
415
        $o = new BasicObject();
416
        $this->assertNull($o->getAccessPath());
417
        $o->access_path = 'abcdefg, hijk elemeno p';
418
        $this->assertEquals('abcdefg, hijk elemeno p', $o->getAccessPath());
419
    }
420
421
    /**
422
     * @covers \Kint\Object\BasicObject::blank
423
     */
424
    public function testBlank()
425
    {
426
        $o = new BasicObject();
427
        $this->assertNull($o->name);
428
        $this->assertNull($o->access_path);
429
430
        $o = BasicObject::blank();
431
        $this->assertNull($o->name);
432
        $this->assertNull($o->access_path);
433
434
        $o = BasicObject::blank('$var');
435
        $this->assertEquals('$var', $o->name);
436
        $this->assertEquals('$var', $o->access_path);
437
438
        $o = BasicObject::blank('Name', 'access_path');
439
        $this->assertEquals('Name', $o->name);
440
        $this->assertEquals('access_path', $o->access_path);
441
    }
442
443
    /**
444
     * @covers \Kint\Object\BasicObject::transplant
445
     */
446
    public function testTransplant()
447
    {
448
        $o = new BasicObject();
449
450
        $o->name = 'name';
451
        $o->size = 42;
452
        $o->access_path = 'access_path';
453
        $o->access = BasicObject::ACCESS_PUBLIC;
454
        $o->static = true;
455
        $o->const = true;
456
        $o->type = 'type';
457
        $o->depth = 43;
458
        $o->owner_class = 'owner_class';
459
        $o->operator = BasicObject::OPERATOR_OBJECT;
460
        $o->reference = true;
461
        $o->hints = array('test', 'transplant', 'hints');
462
463
        $r = new Representation('Test');
464
        $o->addRepresentation($r);
465
466
        $o2 = $o->transplant(new BasicObject());
467
468
        $this->assertEquals($o, $o2);
469
        $this->assertNotSame($o, $o2);
470
        $this->assertSame($o->value, $o2->value);
471
472
        $o2 = new BasicObject();
473
        $r2 = new Representation('Test 2');
474
        $o2->addRepresentation($r2);
475
        $o2->hints = array('test', 'thoroughly');
476
477
        $o2 = $o->transplant($o2);
478
479
        $this->assertSame(array('test_2' => $r2, 'test' => $r), $o2->getRepresentations());
480
        $this->assertSame(array('test', 'transplant', 'hints', 'test', 'thoroughly'), $o2->hints);
481
    }
482
483
    /**
484
     * @covers \Kint\Object\BasicObject::isSequential
485
     */
486
    public function testIsSequential()
487
    {
488
        $this->assertTrue(BasicObject::isSequential(array(1, 2, 3, 4)));
489
        $this->assertFalse(BasicObject::isSequential(array(0 => 1, 1 => 2, 3 => 3, 2 => 4)));
490
        $this->assertFalse(BasicObject::isSequential(array(0 => 1, 1 => 2, '02' => 3, 3 => 4)));
491
    }
492
493
    /**
494
     * @covers \Kint\Object\BasicObject::sortByAccess
495
     */
496
    public function testSortByAccess()
497
    {
498
        $o1 = new BasicObject();
499
        $o2 = new BasicObject();
500
        $o3 = new BasicObject();
501
502
        $a = array($o1, $o2, $o3);
503
504
        $o1->access = BasicObject::ACCESS_PRIVATE;
505
        $o2->access = BasicObject::ACCESS_PROTECTED;
506
        $o3->access = BasicObject::ACCESS_PUBLIC;
507
508
        $this->assertSame(array($o1, $o2, $o3), $a);
509
510
        usort($a, 'Kint\\Object\\BasicObject::sortByAccess');
511
        $this->assertSame(array($o3, $o2, $o1), $a);
512
513
        $o1->access = BasicObject::ACCESS_PROTECTED;
514
        $o2->access = BasicObject::ACCESS_PRIVATE;
515
516
        usort($a, 'Kint\\Object\\BasicObject::sortByAccess');
517
        $this->assertSame(array($o3, $o1, $o2), $a);
518
    }
519
520
    /**
521
     * @covers \Kint\Object\BasicObject::sortByName
522
     */
523
    public function testSortByName()
524
    {
525
        $o1 = new BasicObject();
526
        $o2 = new BasicObject();
527
        $o3 = new BasicObject();
528
529
        $a = array($o1, $o2, $o3);
530
531
        $o1->name = 'Name Z';
532
        $o2->name = 'Name B';
533
        $o3->name = 'Name A';
534
535
        $this->assertSame(array($o1, $o2, $o3), $a);
536
537
        usort($a, 'Kint\\Object\\BasicObject::sortByName');
538
        $this->assertSame(array($o3, $o2, $o1), $a);
539
540
        $o1->name = 'Name M';
541
        $o2->name = 'Name Z2';
542
543
        usort($a, 'Kint\\Object\\BasicObject::sortByName');
544
        $this->assertSame(array($o3, $o1, $o2), $a);
545
    }
546
}
547