CsrfTokenTest::testValidateNotValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace OpCacheGUI\Unit\Security;
4
5
use OpCacheGUI\Security\CsrfToken;
6
use OpCacheGUI\Security\Generator;
7
use OpCacheGUI\Storage\KeyValuePair;
8
use PHPUnit\Framework\TestCase;
9
10
class CsrfTokenTest extends TestCase
11
{
12
    /**
13
     * @covers OpCacheGUI\Security\CsrfToken::__construct
14
     * @covers OpCacheGUI\Security\CsrfToken::get
15
     */
16
    public function testGetWhenAlreadyStored()
17
    {
18
        /** @var KeyValuePair|\PHPUnit_Framework_MockObject_MockObject $storage */
19
        $storage = $this->createMock(KeyValuePair::class);
20
        $storage->method('isKeyValid')->willReturn(true);
21
        $storage->method('get')->will($this->returnArgument(0));
22
23
        $token = new CsrfToken($storage);
0 ignored issues
show
Bug introduced by
It seems like $storage defined by $this->createMock(\OpCac...ge\KeyValuePair::class) on line 19 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, OpCacheGUI\Security\CsrfToken::__construct() does only seem to accept object<OpCacheGUI\Storage\KeyValuePair>, 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...
24
25
        $this->assertSame('csrfToken', $token->get());
26
    }
27
28
    /**
29
     * @covers OpCacheGUI\Security\CsrfToken::__construct
30
     * @covers OpCacheGUI\Security\CsrfToken::get
31
     * @covers OpCacheGUI\Security\CsrfToken::generate
32
     */
33
    public function testGetWhenNotStored()
34
    {
35
        /** @var KeyValuePair|\PHPUnit_Framework_MockObject_MockObject $storage */
36
        $storage = $this->createMock(KeyValuePair::class);
37
        $storage->method('isKeyValid')->willReturn(false);
38
        $storage->method('get')->will($this->returnArgument(0));
39
40
        $generator = $this->createMock(Generator::class);
41
        $generator->method('generate')->willReturn('12345678901234567890123456789012345678901234567890123456');
42
43
        $token = new CsrfToken($storage);
0 ignored issues
show
Bug introduced by
It seems like $storage defined by $this->createMock(\OpCac...ge\KeyValuePair::class) on line 36 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, OpCacheGUI\Security\CsrfToken::__construct() does only seem to accept object<OpCacheGUI\Storage\KeyValuePair>, 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
        $this->assertSame('csrfToken', $token->get());
46
    }
47
48
    /**
49
     * @covers OpCacheGUI\Security\CsrfToken::__construct
50
     * @covers OpCacheGUI\Security\CsrfToken::validate
51
     * @covers OpCacheGUI\Security\CsrfToken::get
52
     */
53
    public function testValidateValid()
54
    {
55
        /** @var KeyValuePair|\PHPUnit_Framework_MockObject_MockObject $storage */
56
        $storage = $this->createMock(KeyValuePair::class);
57
        $storage->method('isKeyValid')->willReturn(true);
58
        $storage->method('get')->will($this->returnArgument(0));
59
60
        $token = new CsrfToken($storage);
0 ignored issues
show
Bug introduced by
It seems like $storage defined by $this->createMock(\OpCac...ge\KeyValuePair::class) on line 56 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, OpCacheGUI\Security\CsrfToken::__construct() does only seem to accept object<OpCacheGUI\Storage\KeyValuePair>, 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...
61
62
        $this->assertTrue($token->validate('csrfToken'));
63
    }
64
65
    /**
66
     * @covers OpCacheGUI\Security\CsrfToken::__construct
67
     * @covers OpCacheGUI\Security\CsrfToken::validate
68
     * @covers OpCacheGUI\Security\CsrfToken::get
69
     */
70
    public function testValidateNotValid()
71
    {
72
        /** @var KeyValuePair|\PHPUnit_Framework_MockObject_MockObject $storage */
73
        $storage = $this->createMock(KeyValuePair::class);
74
        $storage->method('isKeyValid')->willReturn(true);
75
        $storage->method('get')->will($this->returnArgument(0));
76
77
        $token = new CsrfToken($storage);
0 ignored issues
show
Bug introduced by
It seems like $storage defined by $this->createMock(\OpCac...ge\KeyValuePair::class) on line 73 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, OpCacheGUI\Security\CsrfToken::__construct() does only seem to accept object<OpCacheGUI\Storage\KeyValuePair>, 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...
78
79
        $this->assertFalse($token->validate('notvalid'));
80
    }
81
}
82