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

SessionPersistenceTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testInitializeSessionFromRequestWithMissingPsr7SessionAttribute() 0 20 1
A testInitializeSessionFromRequestWithPsr7SessionAttribute() 0 17 1
A testPersistSession() 0 14 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 PHPUnit\Framework\MockObject\MockObject;
24
use PHPUnit\Framework\TestCase;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use PSR7Sessions\Storageless\Http\SessionMiddleware;
28
use PSR7Sessions\Storageless\Session\SessionInterface;
29
use PSR7Sessions\Storageless\Session\Zend\SessionPersistence;
30
use Zend\Expressive\Session\SessionInterface as ZendSessionInterface;
31
use function sprintf;
32
33
/**
34
 * @covers \PSR7Sessions\Storageless\Session\Zend\SessionPersistence
35
 */
36
final class SessionPersistenceTest extends TestCase
37
{
38
    public function testInitializeSessionFromRequestWithMissingPsr7SessionAttribute() : void
39
    {
40
        $this->expectException(\UnexpectedValueException::class);
41
        $this->expectExceptionMessage(sprintf(
42
            'Please add the following middleware "%s" before execute this method "%s::initializeSessionFromRequest"',
43
            SessionMiddleware::class,
44
            SessionPersistence::class
45
        ));
46
47
        /** @var ServerRequestInterface|MockObject $request */
48
        $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
49
        $request
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Psr\Http\Message\ServerRequestInterface.

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...
50
            ->expects(self::once())
51
            ->method('getAttribute')
52
            ->with(SessionMiddleware::SESSION_ATTRIBUTE, null)
53
            ->willReturn(null);
54
55
        $persistence = new SessionPersistence();
56
        $persistence->initializeSessionFromRequest($request);
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...izeSessionFromRequest() does only seem to accept object<Psr\Http\Message\ServerRequestInterface>, 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...
57
    }
58
59
    public function testInitializeSessionFromRequestWithPsr7SessionAttribute() : void
60
    {
61
        /** @var SessionInterface|MockObject $session */
62
        $session = $this->getMockBuilder(SessionInterface::class)->getMock();
63
        $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...
64
65
        /** @var ServerRequestInterface|MockObject $request */
66
        $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
67
        $request
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Psr\Http\Message\ServerRequestInterface.

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...
68
            ->expects(self::once())
69
            ->method('getAttribute')
70
            ->with(SessionMiddleware::SESSION_ATTRIBUTE, null)
71
            ->willReturn($session);
72
73
        $persistence = new SessionPersistence();
74
        $persistence->initializeSessionFromRequest($request);
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...izeSessionFromRequest() does only seem to accept object<Psr\Http\Message\ServerRequestInterface>, 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...
75
    }
76
77
    public function testPersistSession() : void
78
    {
79
        /** @var ZendSessionInterface|MockObject $zendSession */
80
        $zendSession = $this->getMockBuilder(ZendSessionInterface::class)->getMock();
81
        $zendSession->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 Zend\Expressive\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...
82
83
        /** @var ResponseInterface|MockObject $response */
84
        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
85
        $response->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 Psr\Http\Message\ResponseInterface.

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...
86
87
        $persistence = new SessionPersistence();
88
89
        self::assertSame($response, $persistence->persistSession($zendSession, $response));
0 ignored issues
show
Bug introduced by
It seems like $zendSession can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...tence::persistSession() does only seem to accept object<Zend\Expressive\Session\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...
Bug introduced by
It seems like $response can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, PSR7Sessions\Storageless...tence::persistSession() does only seem to accept object<Psr\Http\Message\ResponseInterface>, 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...
90
    }
91
}
92