Passed
Pull Request — master (#82)
by Dominik
31:06
created

SessionAdapterTest::testRegenerate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 PHPUnit\Framework\MockObject\MockObject;
24
use PHPUnit\Framework\TestCase;
25
use PSR7Sessions\Storageless\Session\SessionInterface;
26
use PSR7Sessions\Storageless\Session\Zend\SessionAdapter;
27
28
/**
29
 * @covers \PSR7Sessions\Storageless\Session\Zend\SessionAdapter
30
 */
31
final class SessionAdapterTest extends TestCase
32
{
33
    public function testToArray() : void
34
    {
35
        $object      = new \stdClass();
36
        $object->key = 'value';
37
38
        /** @var SessionInterface|MockObject $session */
39
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
40
        $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...
41
42
        $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...
43
44
        self::assertSame(['key' => 'value'], $sessionAdapter->toArray());
45
    }
46
47
    public function testGet() : void
48
    {
49
        /** @var SessionInterface|MockObject $session */
50
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
51
        $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...
52
53
        $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...
54
55
        self::assertSame('value', $sessionAdapter->get('key'));
56
    }
57
58
    public function testHas() : void
59
    {
60
        /** @var SessionInterface|MockObject $session */
61
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
62
        $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...
63
64
        $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...
65
66
        self::assertTrue($sessionAdapter->has('key'));
67
    }
68
69
    public function testSet() : void
70
    {
71
        /** @var SessionInterface|MockObject $session */
72
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
73
        $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...
74
75
        $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...
76
        $sessionAdapter->set('key', 'value');
77
    }
78
79
    public function testUnset() : void
80
    {
81
        /** @var SessionInterface|MockObject $session */
82
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
83
        $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...
84
85
        $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...
86
        $sessionAdapter->unset('key');
87
    }
88
89
    public function testClear() : void
90
    {
91
        /** @var SessionInterface|MockObject $session */
92
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
93
        $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...
94
95
        $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...
96
        $sessionAdapter->clear();
97
    }
98
99
    public function testHasChanged() : void
100
    {
101
        /** @var SessionInterface|MockObject $session */
102
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
103
        $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...
104
105
        $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...
106
107
        self::assertTrue($sessionAdapter->hasChanged());
108
    }
109
110
    public function testRegenerate() : void
111
    {
112
        $this->expectException(\BadMethodCallException::class);
113
        $this->expectExceptionMessage(
114
            'Method "PSR7Sessions\Storageless\Session\Zend\SessionAdapter::regenerate" not implemented'
115
        );
116
117
        /** @var SessionInterface|MockObject $session */
118
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
119
        $session->expects(self::never())->method(self::anything());
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...
120
121
        $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...
122
        $sessionAdapter->regenerate();
123
    }
124
125
    public function testIsRegenerated() : void
126
    {
127
        /** @var SessionInterface|MockObject $session */
128
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
129
        $session->expects(self::never())->method(self::anything());
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...
130
131
        $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...
132
133
        self::assertFalse($sessionAdapter->isRegenerated());
134
    }
135
}
136