Completed
Pull Request — master (#82)
by Dominik
41:16 queued 06:21
created

SessionAdapterTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 102
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testToArray() 0 13 1
A testGet() 0 10 1
A testHas() 0 10 1
A testSet() 0 9 1
A testUnset() 0 9 1
A testClear() 0 9 1
A testHasChanged() 0 10 1
A testRegenerate() 0 11 1
A testIsRegenerated() 0 10 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\Zend;
22
23
use Lcobucci\Clock\FrozenClock;
24
use PHPUnit\Framework\MockObject\MockObject;
25
use PHPUnit\Framework\TestCase;
26
use PSR7Sessions\Storageless\Session\SessionInterface;
27
use PSR7Sessions\Storageless\Session\Zend\SessionAdapter;
28
29
/**
30
 * @covers \PSR7Sessions\Storageless\Session\Zend\SessionAdapter
31
 */
32
final class SessionAdapterTest extends TestCase
33
{
34
    public function testToArray() : void
35
    {
36
        $object      = new \stdClass();
37
        $object->key = 'value';
38
39
        /** @var SessionInterface|MockObject $session */
40
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
41
        $session->expects(self::once())->method('jsonSerialize')->with()->willReturn($object);
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...
42
43
        $sessionAdapter = new SessionAdapter($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...nAdapter::__construct() does only seem to accept object<PSR7Sessions\Stor...ssion\SessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
44
45
        self::assertSame(['key' => 'value'], $sessionAdapter->toArray());
46
    }
47
48
    public function testGet() : void
49
    {
50
        /** @var SessionInterface|MockObject $session */
51
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
52
        $session->expects(self::once())->method('get')->with('key', null)->willReturn('value');
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...
53
54
        $sessionAdapter = new SessionAdapter($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...nAdapter::__construct() does only seem to accept object<PSR7Sessions\Stor...ssion\SessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55
56
        self::assertSame('value', $sessionAdapter->get('key'));
57
    }
58
59
    public function testHas() : void
60
    {
61
        /** @var SessionInterface|MockObject $session */
62
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
63
        $session->expects(self::once())->method('has')->with('key')->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...
64
65
        $sessionAdapter = new SessionAdapter($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...nAdapter::__construct() does only seem to accept object<PSR7Sessions\Stor...ssion\SessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
66
67
        self::assertTrue($sessionAdapter->has('key'));
68
    }
69
70
    public function testSet() : void
71
    {
72
        /** @var SessionInterface|MockObject $session */
73
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
74
        $session->expects(self::once())->method('set')->with('key', 'value');
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...
75
76
        $sessionAdapter = new SessionAdapter($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...nAdapter::__construct() does only seem to accept object<PSR7Sessions\Stor...ssion\SessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
77
        $sessionAdapter->set('key', 'value');
78
    }
79
80
    public function testUnset() : void
81
    {
82
        /** @var SessionInterface|MockObject $session */
83
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
84
        $session->expects(self::once())->method('remove')->with('key');
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...
85
86
        $sessionAdapter = new SessionAdapter($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...nAdapter::__construct() does only seem to accept object<PSR7Sessions\Stor...ssion\SessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
87
        $sessionAdapter->unset('key');
88
    }
89
90
    public function testClear() : void
91
    {
92
        /** @var SessionInterface|MockObject $session */
93
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
94
        $session->expects(self::once())->method('clear')->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...
95
96
        $sessionAdapter = new SessionAdapter($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...nAdapter::__construct() does only seem to accept object<PSR7Sessions\Stor...ssion\SessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
97
        $sessionAdapter->clear();
98
    }
99
100
    public function testHasChanged() : void
101
    {
102
        /** @var SessionInterface|MockObject $session */
103
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
104
        $session->expects(self::once())->method('hasChanged')->with()->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...
105
106
        $sessionAdapter = new SessionAdapter($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...nAdapter::__construct() does only seem to accept object<PSR7Sessions\Stor...ssion\SessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
108
        self::assertTrue($sessionAdapter->hasChanged());
109
    }
110
111
    public function testRegenerate() : void
112
    {
113
        $clock = new FrozenClock(new \DateTimeImmutable());
114
115
        /** @var SessionInterface|MockObject $session */
116
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
117
        $session->expects(self::once())->method('set')->with('_regenerated', $clock->now()->getTimestamp());
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...
118
119
        $sessionAdapter = new SessionAdapter($session, $clock);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...nAdapter::__construct() does only seem to accept object<PSR7Sessions\Stor...ssion\SessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
120
        $sessionAdapter->regenerate();
121
    }
122
123
    public function testIsRegenerated() : void
124
    {
125
        /** @var SessionInterface|MockObject $session */
126
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
127
        $session->expects(self::once())->method('has')->with('_regenerated')->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...
128
129
        $sessionAdapter = new SessionAdapter($session);
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...nAdapter::__construct() does only seem to accept object<PSR7Sessions\Stor...ssion\SessionInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
130
131
        self::assertTrue($sessionAdapter->isRegenerated());
132
    }
133
}
134