Completed
Pull Request — master (#58)
by Thijs
02:34
created

ArrayAccessorTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 76
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testOffsetExists() 0 12 1
A testOffsetUnset() 0 7 1
A testOffsetGet() 0 14 1
A testOffsetSet() 0 10 1
A testGetSessionInterface() 0 4 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
declare(strict_types = 1);
20
21
namespace PSR7SessionsTest\Storageless\Session;
22
23
use PHPUnit_Framework_TestCase;
24
use PSR7Sessions\Storageless\Helper\ArrayAccessor;
25
use PSR7Sessions\Storageless\Session\SessionInterface;
26
27
/**
28
 * @covers \PSR7Sessions\Storageless\Helper\ArrayAccessor
29
 */
30
final class ArrayAccessorTest extends PHPUnit_Framework_TestCase
31
{
32
    /**
33
     * @var SessionInterface|\PHPUnit_Framework_MockObject_MockObject
34
     */
35
    private $wrappedSession;
36
37
    /**
38
     * @var ArrayAccessor
39
     */
40
    private $arrayAccess;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function setUp()
46
    {
47
        $this->wrappedSession = $this->createMock(SessionInterface::class);
48
        $this->arrayAccess = new ArrayAccessor($this->wrappedSession);
49
    }
50
51
    public function testOffsetExists()
52
    {
53
        $this->wrappedSession->expects(self::exactly(2))->method('has')->willReturnMap(
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in PSR7Sessions\Storageless\Session\SessionInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54
            [
55
                ['foo', false],
56
                ['bar', true],
57
            ]
58
        );
59
60
        self::assertFalse($this->arrayAccess->offsetExists('foo'));
61
        self::assertTrue($this->arrayAccess->offsetExists('bar'));
62
    }
63
64
    /**
65
     *
66
     */
67
    public function testOffsetUnset()
68
    {
69
        $this->wrappedSession->expects(self::exactly(2))->method('remove')->with(self::logicalOr('foo', 'bar'));
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in PSR7Sessions\Storageless\Session\SessionInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
71
        $this->arrayAccess->offsetUnset('foo');
72
        $this->arrayAccess->offsetUnset('bar');
73
    }
74
75
    public function testOffsetGet()
76
    {
77
        $this->wrappedSession->expects(self::exactly(3))->method('get')->willReturnMap(
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in PSR7Sessions\Storageless\Session\SessionInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
78
            [
79
                ['foo', null, 'bar'],
80
                ['baz', null, 'tab'],
81
                ['baz', 'default', 'taz'],
82
            ]
83
        );
84
85
        self::assertSame('bar', $this->arrayAccess->offsetGet('foo'));
86
        self::assertSame('tab', $this->arrayAccess->offsetGet('baz'));
87
        self::assertSame('taz', $this->arrayAccess->offsetGet('baz', 'default'));
88
    }
89
90
    public function testOffsetSet()
91
    {
92
        $this->wrappedSession->expects(self::exactly(2))->method('set')->with(
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in PSR7Sessions\Storageless\Session\SessionInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
93
            self::logicalOr('foo', 'baz'),
94
            self::logicalOr('bar', 'tab')
95
        );
96
97
        $this->arrayAccess->offsetSet('foo', 'bar');
98
        $this->arrayAccess->offsetSet('baz', 'tab');
99
    }
100
101
    public function testGetSessionInterface()
102
    {
103
        self::assertEquals($this->wrappedSession, $this->arrayAccess->getSession());
104
    }
105
}
106