Completed
Push — 6.13 ( 254dd2...055733 )
by André
13:14
created

CsrfTokenManagerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 35.48 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 22
loc 62
rs 10
c 1
b 0
f 0
wmc 5
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testHasTokenForHttp() 11 11 1
A testHasTokenForHttps() 11 11 1
A createCsrfTokenManager() 0 18 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File containing the RestLogoutHandlerTest class.
5
 *
6
 * @copyright Copyright (C) 1999-2014 eZ Systems AS. All rights reserved.
7
 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
8
 */
9
namespace eZ\Publish\Core\REST\Server\Tests\Security;
10
11
use eZ\Publish\Core\Base\Tests\PHPUnit5CompatTrait;
12
use eZ\Publish\Core\REST\Server\Security\CsrfTokenManager;
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
17
use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;
18
19
class CsrfTokenManagerTest extends TestCase
20
{
21
    use PHPUnit5CompatTrait;
22
23
    const CSRF_TOKEN_INTENTION = 'csrf';
24
25
    /** @var \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface */
26
    private $tokenStorage;
27
    /** @var \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\HttpFoundation\RequestStack */
28
    private $requestStack;
29
30
    protected function setUp()
31
    {
32
        parent::setUp();
33
34
        $this->tokenStorage = $this->createMock(TokenStorageInterface::class);
35
        $this->requestStack = $this->createMock(RequestStack::class);
36
    }
37
38 View Code Duplication
    public function testHasTokenForHttp()
39
    {
40
        $csrfTokenManager = $this->createCsrfTokenManager(false);
41
42
        $this->tokenStorage
43
            ->expects($this->once())
44
            ->method('hasToken')
45
            ->with(self::CSRF_TOKEN_INTENTION);
46
47
        $csrfTokenManager->hasToken(self::CSRF_TOKEN_INTENTION);
48
    }
49
50 View Code Duplication
    public function testHasTokenForHttps()
51
    {
52
        $csrfTokenManager = $this->createCsrfTokenManager(true);
53
54
        $this->tokenStorage
55
            ->expects($this->once())
56
            ->method('hasToken')
57
            ->with('https-' . self::CSRF_TOKEN_INTENTION);
58
59
        $csrfTokenManager->hasToken(self::CSRF_TOKEN_INTENTION);
60
    }
61
62
    private function createCsrfTokenManager($https = false)
63
    {
64
        $request = new Request();
65
        if ($https) {
66
            $request->server->set('HTTPS', 'ON');
67
        }
68
69
        $this->requestStack
70
            ->expects($this->once())
71
            ->method('getMasterRequest')
72
            ->willReturn($request);
73
74
        return new CsrfTokenManager(
75
            $this->createMock(TokenGeneratorInterface::class),
76
            $this->tokenStorage,
77
            $this->requestStack
78
        );
79
    }
80
}
81