Completed
Push — master ( e05cd4...bb1ac9 )
by Luís
10s
created

LazySessionTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 147
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testIsALazySession() 0 4 1
A testLazyNonInitializedSessionIsAlwaysNotChanged() 0 6 1
A testHasChanged() 0 12 1
A testHas() 0 12 1
A testGet() 0 14 1
A testRemove() 0 9 1
A testClear() 0 9 1
A testSet() 0 13 1
A testIsEmpty() 0 10 1
A testJsonSerialize() 0 10 1
A wrappedSessionWillBeLoaded() 0 4 1
A forceWrappedSessionInitialization() 0 5 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\Session\LazySession;
25
use PSR7Sessions\Storageless\Session\SessionInterface;
26
27
/**
28
 * @covers \PSR7Sessions\Storageless\Session\LazySession
29
 */
30
final class LazySessionTest extends PHPUnit_Framework_TestCase
31
{
32
    /**
33
     * @var SessionInterface|\PHPUnit_Framework_MockObject_MockObject
34
     */
35
    private $wrappedSession;
36
37
    /**
38
     * @var callable|\PHPUnit_Framework_MockObject_MockObject
39
     */
40
    private $sessionLoader;
41
42
    /**
43
     * @var LazySession
44
     */
45
    private $lazySession;
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    protected function setUp()
51
    {
52
        $this->wrappedSession = $this->createMock(SessionInterface::class);
53
        $this->sessionLoader  = $this->getMockBuilder(\stdClass::class)->setMethods(['__invoke'])->getMock();
54
        $this->lazySession    = LazySession::fromContainerBuildingCallback($this->sessionLoader);
55
    }
56
57
    public function testIsALazySession()
58
    {
59
        self::assertInstanceOf(LazySession::class, $this->lazySession);
60
    }
61
62
    public function testLazyNonInitializedSessionIsAlwaysNotChanged()
63
    {
64
        $this->sessionLoader->expects(self::never())->method('__invoke');
0 ignored issues
show
Bug introduced by
The method expects cannot be called on $this->sessionLoader (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
65
66
        self::assertFalse($this->lazySession->hasChanged());
67
    }
68
69
    public function testHasChanged()
70
    {
71
        $this->wrappedSessionWillBeLoaded();
72
73
        $this->wrappedSession->expects(self::at(1))->method('hasChanged')->willReturn(true);
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...
74
        $this->wrappedSession->expects(self::at(2))->method('hasChanged')->willReturn(false);
75
76
        $this->forceWrappedSessionInitialization();
77
78
        self::assertTrue($this->lazySession->hasChanged());
79
        self::assertFalse($this->lazySession->hasChanged());
80
    }
81
82
    public function testHas()
83
    {
84
        $this->wrappedSessionWillBeLoaded();
85
86
        $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...
87
            ['foo', false],
88
            ['bar', true],
89
        ]);
90
91
        self::assertFalse($this->lazySession->has('foo'));
92
        self::assertTrue($this->lazySession->has('bar'));
93
    }
94
95
    public function testGet()
96
    {
97
        $this->wrappedSessionWillBeLoaded();
98
99
        $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...
100
            ['foo', null, 'bar'],
101
            ['baz', null, 'tab'],
102
            ['baz', 'default', 'taz'],
103
        ]);
104
105
        self::assertSame('bar', $this->lazySession->get('foo'));
106
        self::assertSame('tab', $this->lazySession->get('baz'));
107
        self::assertSame('taz', $this->lazySession->get('baz', 'default'));
108
    }
109
110
    public function testRemove()
111
    {
112
        $this->wrappedSessionWillBeLoaded();
113
114
        $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...
115
116
        $this->lazySession->remove('foo');
117
        $this->lazySession->remove('bar');
118
    }
119
120
    public function testClear()
121
    {
122
        $this->wrappedSessionWillBeLoaded();
123
124
        $this->wrappedSession->expects(self::exactly(2))->method('clear');
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...
125
126
        $this->lazySession->clear();
127
        $this->lazySession->clear();
128
    }
129
130
    public function testSet()
131
    {
132
        $this->wrappedSessionWillBeLoaded();
133
134
        $this
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...
135
            ->wrappedSession
136
            ->expects(self::exactly(2))
137
            ->method('set')
138
            ->with(self::logicalOr('foo', 'baz'), self::logicalOr('bar', 'tab'));
139
140
        $this->lazySession->set('foo', 'bar');
141
        $this->lazySession->set('baz', 'tab');
142
    }
143
144
    public function testIsEmpty()
145
    {
146
        $this->wrappedSessionWillBeLoaded();
147
148
        $this->wrappedSession->expects(self::at(0))->method('isEmpty')->willReturn(true);
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...
149
        $this->wrappedSession->expects(self::at(1))->method('isEmpty')->willReturn(false);
150
151
        self::assertTrue($this->lazySession->isEmpty());
152
        self::assertFalse($this->lazySession->isEmpty());
153
    }
154
155
    public function testJsonSerialize()
156
    {
157
        $this->wrappedSessionWillBeLoaded();
158
159
        $this->wrappedSession->expects(self::at(0))->method('jsonSerialize')->willReturn('foo');
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...
160
        $this->wrappedSession->expects(self::at(1))->method('jsonSerialize')->willReturn('bar');
161
162
        self::assertSame('foo', $this->lazySession->jsonSerialize());
163
        self::assertSame('bar', $this->lazySession->jsonSerialize());
164
    }
165
166
    private function wrappedSessionWillBeLoaded()
167
    {
168
        $this->sessionLoader->expects(self::once())->method('__invoke')->willReturn($this->wrappedSession);
0 ignored issues
show
Bug introduced by
The method expects cannot be called on $this->sessionLoader (of type callable).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
169
    }
170
171
    private function forceWrappedSessionInitialization()
172
    {
173
        // no-op operation that is known to trigger session lazy-loading
174
        $this->lazySession->remove(uniqid('nonExisting', true));
175
    }
176
}
177