ArrayAccessTraitTest::offsetUnset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace SubjectivePHPTest\Spl\Traits;
3
4
/**
5
 * @coversDefaultClass SubjectivePHP\Spl\Traits\ArrayAccessTrait
6
 */
7
class ArrayAccessTraitTest extends \PHPUnit\Framework\TestCase
8
{
9
    /**
10
     * Verify basic behavior of offsetSet().
11
     *
12
     * @test
13
     * @covers ::offsetSet
14
     *
15
     * @return void
16
     */
17
    public function offsetSet()
18
    {
19
        $object = new SimpleObject();
20
        $object['foo'] = 'bar';
21
        $object[] = 123;
22
23
        $this->assertSame(
24
            [
25
                'foo' => 'bar',
26
                0 => 123,
27
            ],
28
            $object->getContainer()
29
        );
30
    }
31
32
    /**
33
     * Verify behavior of offsetSet() with key which is not a string or an integer.
34
     *
35
     * @test
36
     * @covers ::offsetSet
37
     * @expectedException \InvalidArgumentException
38
     * @expectedExceptionMessage $offset must be an integer or string
39
     *
40
     * @return void
41
     */
42
    public function offsetSetInvalidKey()
43
    {
44
        $object = new SimpleObject();
45
        /** @scrutinizer ignore-call */ $object->offsetSet(new \StdClass(), 'weeee!');
0 ignored issues
show
Documentation introduced by
new \StdClass() is of type object<stdClass>, but the function expects a string|integer.

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...
46
    }
47
48
    /**
49
     * Verify basic behavior of offsetExists().
50
     *
51
     * @test
52
     * @covers ::offsetExists
53
     *
54
     * @return void
55
     */
56
    public function offsetExists()
57
    {
58
        $object = new SimpleObject(['foo' => 'bar']);
59
        $this->assertTrue($object->offsetExists('foo'));
60
    }
61
62
    /**
63
     * Verify behavior of offsetExists() with key that is not an integer or string.
64
     *
65
     * @test
66
     * @covers ::offsetExists
67
     *
68
     * @return void
69
     */
70
    public function offsetExistsInvalidKey()
71
    {
72
        $object = new SimpleObject(['foo' => 'bar']);
73
        $this->assertFalse($object->offsetExists(true));
74
    }
75
76
    /**
77
     * Verify basic behavior of offsetUnset().
78
     *
79
     * @test
80
     * @covers ::offsetUnset
81
     *
82
     * @return void
83
     */
84
    public function offsetUnset()
85
    {
86
        $object = new SimpleObject(['foo' => 'bar']);
87
        $this->assertSame(['foo' => 'bar'], $object->getContainer());
88
        unset($object['foo']);
89
        $this->assertSame([], $object->getContainer());
90
    }
91
92
    /**
93
     * Verify behavior of offsetGet() with key that is not an integer or string.
94
     *
95
     * @test
96
     * @covers ::offsetGet
97
     *
98
     * @return void
99
     */
100
    public function offsetGet()
101
    {
102
        $object = new SimpleObject(['foo' => 'bar']);
103
        $this->assertSame('bar', $object['foo']);
104
    }
105
106
    /**
107
     * Verify behavior of offsetGet() with key which is not a string or an integer.
108
     *
109
     * @test
110
     * @covers ::offsetGet
111
     * @expectedException \InvalidArgumentException
112
     * @expectedExceptionMessage $offset must be an integer or string
113
     *
114
     * @return void
115
     */
116
    public function offsetGetInvalidKey()
117
    {
118
        $object = new SimpleObject();
119
        /** @scrutinizer ignore-call */ $object->offsetGet(new \StdClass());
0 ignored issues
show
Documentation introduced by
new \StdClass() is of type object<stdClass>, but the function expects a integer|string.

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...
120
    }
121
}
122