Passed
Pull Request — master (#3)
by Bartosz
01:53
created

ObjectArrayTest::provideDifferentIterables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
cc 1
nc 1
nop 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A ObjectArrayTest.php$0 ➔ __construct() 0 4 1
A ObjectArrayTest.php$0 ➔ getIterator() 0 4 1
A ObjectArrayTest.php$0 ➔ offsetExists() 0 4 1
A ObjectArrayTest.php$0 ➔ offsetGet() 0 4 1
A ObjectArrayTest.php$0 ➔ offsetSet() 0 4 1
A ObjectArrayTest.php$0 ➔ offsetUnset() 0 4 1
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 ArrayAccess;
14
use ArrayIterator;
15
use DateTime;
16
use DateTimeImmutable;
17
use InvalidArgumentException;
18
use IteratorAggregate;
19
use MSlwk\TypeSafeArray\ObjectArray;
20
use PHPUnit\Framework\TestCase;
21
22
/**
23
 * Class ObjectArrayTest
24
 * @package MSlwk\TypeSafeArray\Test\Unit
25
 */
26
class ObjectArrayTest extends TestCase
27
{
28
    /**
29
     * @dataProvider provideDifferentIterables
30
     * @param iterable $iterable
31
     * @return void
32
     */
33
    public function testOperationsOnArray(iterable $iterable): void
34
    {
35
        $object1 = new DateTime();
36
        $object2 = new DateTime();
37
        $object3 = new DateTime();
38
        $object4 = new DateTime();
39
        $iterable[] = $object2;
40
        $objectArray = new ObjectArray(DateTime::class, $iterable);
41
        $objectArray[1] = $object3;
42
        $objectArray->offsetSet(2, $object4);
43
        $objectArray->add($object1);
44
45
        $this->assertSame($object2, $objectArray->offsetGet(0));
46
        $this->assertSame($object3, $objectArray[1]);
47
        $this->assertSame($object4, $objectArray[2]);
48
        $this->assertSame($object1, $objectArray[3]);
49
50
        $this->assertSame($object2, $objectArray->current());
51
        $objectArray->next();
52
        $objectArray->next();
53
        $this->assertSame($object4, $objectArray->current());
54
        $objectArray->rewind();
55
        $this->assertSame($object2, $objectArray->current());
56
57
        $this->assertEquals(4, $objectArray->count());
58
        $objectArray->offsetUnset(2);
59
        $this->assertEquals(3, $objectArray->count());
60
61
        $this->assertTrue($objectArray->offsetExists(1));
62
        $this->assertFalse($objectArray->offsetExists(2));
63
64
        $this->assertTrue($objectArray->valid());
65
66
        $this->assertEquals(0, $objectArray->key());
67
        $objectArray->next();
68
        $this->assertEquals(1, $objectArray->key());
69
    }
70
71
    /**
72
     * @dataProvider provideDifferentIterables
73
     * @param iterable $iterable
74
     * @return void
75
     */
76 View Code Duplication
    public function testAddingWrongTypeViaConstructor(iterable $iterable): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $this->expectException(InvalidArgumentException::class);
79
80
        $object = new DateTimeImmutable();
81
        $iterable[] = $object;
82
        new ObjectArray(DateTime::class, $iterable);
83
    }
84
85
    /**
86
     * @return void
87
     */
88 View Code Duplication
    public function testAddingWrongTypeViaOffsetSet(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        $this->expectException(InvalidArgumentException::class);
91
92
        $object = new DateTimeImmutable();
93
        $objectArray = new ObjectArray(DateTime::class);
94
        $objectArray->offsetSet(0, $object);
95
    }
96
97
    /**
98
     * @return void
99
     */
100 View Code Duplication
    public function testAddingWrongTypeViaAdd(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $this->expectException(InvalidArgumentException::class);
103
104
        $object = new DateTimeImmutable();
105
        $objectArray = new ObjectArray(DateTime::class);
106
        $objectArray->add($object);
107
    }
108
109
    /**
110
     * @return void
111
     */
112 View Code Duplication
    public function testAddingWrongTypeViaArrayAccess(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
    {
114
        $this->expectException(InvalidArgumentException::class);
115
116
        $object = new DateTimeImmutable();
117
        $objectArray = new ObjectArray(DateTime::class);
118
        $objectArray[0] = $object;
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function provideDifferentIterables(): array
125
    {
126
        $iteratorAggregate = new class() implements IteratorAggregate, ArrayAccess {
127
            private $innerIterator;
128
            public function __construct()
129
            {
130
                $this->innerIterator = new ArrayIterator();
131
            }
132
133
            public function getIterator(): ArrayIterator
134
            {
135
                return $this->innerIterator;
136
            }
137
138
            public function offsetExists($offset): bool
139
            {
140
                return $this->innerIterator->offsetExists($offset);
141
            }
142
143
            public function offsetGet($offset)
144
            {
145
                return $this->innerIterator->offsetGet($offset);
146
            }
147
148
            public function offsetSet($offset, $value): void
149
            {
150
                $this->innerIterator->offsetSet($offset, $value);
151
            }
152
153
            public function offsetUnset($offset): void
154
            {
155
                $this->innerIterator->offsetUnset($offset);
156
            }
157
        };
158
159
        return [
160
            'plain_array' => [
161
                []
162
            ],
163
            'iterator' => [
164
                new ArrayIterator()
165
            ],
166
            'iterator-aggregate' => [
167
                $iteratorAggregate
168
            ]
169
        ];
170
    }
171
}
172