Passed
Push — master ( 26975a...e2e428 )
by Maciej
01:16 queued 11s
created

tests/Unit/ObjectArrayTest.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * File:ObjectArrayTest.php
6
 *
7
 * @author      Maciej Sławik <[email protected]>
8
 * Github:      https://github.com/maciejslawik
9
 */
10
11
namespace MSlwk\TypeSafeArray\Test\Unit;
12
13
use DateTime;
14
use DateTimeImmutable;
15
use InvalidArgumentException;
16
use MSlwk\TypeSafeArray\ObjectArray;
17
use PHPUnit\Framework\TestCase;
18
19
/**
20
 * Class ObjectArrayTest
21
 * @package MSlwk\TypeSafeArray\Test\Unit
22
 */
23
class ObjectArrayTest extends TestCase
24
{
25
    /**
26
     * @test
27
     */
28
    public function testOperationsOnArray()
29
    {
30
        $object1 = new DateTime();
31
        $object2 = new DateTime();
32
        $object3 = new DateTime();
33
        $object4 = new DateTime();
34
        $objectArray = new ObjectArray(DateTime::class, [$object2]);
35
        $objectArray[1] = $object3;
36
        $objectArray->offsetSet(2, $object4);
37
        $objectArray->add($object1);
38
39
        $this->assertSame($object2, $objectArray->offsetGet(0));
40
        $this->assertSame($object3, $objectArray[1]);
41
        $this->assertSame($object4, $objectArray[2]);
42
        $this->assertSame($object1, $objectArray[3]);
43
44
        $this->assertSame($object2, $objectArray->current());
45
        $objectArray->next();
46
        $objectArray->next();
47
        $this->assertSame($object4, $objectArray->current());
48
        $objectArray->rewind();
49
        $this->assertSame($object2, $objectArray->current());
50
51
        $this->assertEquals(4, $objectArray->count());
52
        $objectArray->offsetUnset(2);
53
        $this->assertEquals(3, $objectArray->count());
54
55
        $this->assertTrue($objectArray->offsetExists(1));
56
        $this->assertFalse($objectArray->offsetExists(2));
57
58
        $this->assertTrue($objectArray->valid());
59
60
        $this->assertEquals(0, $objectArray->key());
61
        $objectArray->next();
62
        $this->assertEquals(1, $objectArray->key());
63
    }
64
65
    /**
66
     * @test
67
     */
68 View Code Duplication
    public function testAddingWrongTypeViaConstructor()
69
    {
70
        $this->expectException(InvalidArgumentException::class);
71
72
        $object = new DateTimeImmutable();
73
        $objectArray = new ObjectArray(DateTime::class, [$object]);
0 ignored issues
show
$objectArray 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...
74
    }
75
76
    /**
77
     * @test
78
     */
79 View Code Duplication
    public function testAddingWrongTypeViaOffsetSet()
80
    {
81
        $this->expectException(InvalidArgumentException::class);
82
83
        $object = new DateTimeImmutable();
84
        $objectArray = new ObjectArray(DateTime::class);
85
        $objectArray->offsetSet(0, $object);
86
    }
87
88
    /**
89
     * @test
90
     */
91 View Code Duplication
    public function testAddingWrongTypeViaAdd()
92
    {
93
        $this->expectException(InvalidArgumentException::class);
94
95
        $object = new DateTimeImmutable();
96
        $objectArray = new ObjectArray(DateTime::class);
97
        $objectArray->add($object);
98
    }
99
100
    /**
101
     * @test
102
     */
103 View Code Duplication
    public function testAddingWrongTypeViaArrayAccess()
104
    {
105
        $this->expectException(InvalidArgumentException::class);
106
107
        $object = new DateTimeImmutable();
108
        $objectArray = new ObjectArray(DateTime::class);
109
        $objectArray[0] = $object;
110
    }
111
}
112