SessionAttributeNotFoundExceptionTest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A testFromInvalidExpirationTime() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PSR7CsrfTest\Exception;
6
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\ServerRequestInterface;
9
use PSR7Csrf\Exception\ExceptionInterface;
10
use PSR7Csrf\Exception\SessionAttributeNotFoundException;
11
use UnexpectedValueException;
12
13
/**
14
 * @covers \PSR7Csrf\Exception\SessionAttributeNotFoundException
15
 */
16
final class SessionAttributeNotFoundExceptionTest extends TestCase
17
{
18
    public function testFromInvalidExpirationTime()
19
    {
20
        /* @var $request ServerRequestInterface|\PHPUnit_Framework_MockObject_MockObject */
21
        $request = $this->createMock(ServerRequestInterface::class);
22
23
        $request->expects(self::any())->method('getAttributes')->willReturn(['foo' => 'bar', 'baz' => 'tab']);
24
25
        $exception = SessionAttributeNotFoundException::fromAttributeNameAndRequest('foo', $request);
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->createMock(\Psr\H...equestInterface::class) on line 21 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PSR7Csrf\Exception\Sessi...tributeNameAndRequest() 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...
26
27
        self::assertInstanceOf(SessionAttributeNotFoundException::class, $exception);
28
        self::assertInstanceOf(UnexpectedValueException::class, $exception);
29
        self::assertInstanceOf(ExceptionInterface::class, $exception);
30
        self::assertSame(
31
            'Provided request contains no matching session attribute "foo", attributes ["foo","baz"] exist',
32
            $exception->getMessage()
33
        );
34
    }
35
}
36