Completed
Push — feature/evo-2707-username-code... ( 23197c...5baf72 )
by Bastian
09:27
created

CookieFieldStrategyTest::testApplyExtract()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 22
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 22
rs 9.2
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
/**
3
 * Fetching authentication information from Cookie header.
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Strategies;
7
8
use Graviton\TestBundle\Test\WebTestCase;
9
use Symfony\Component\BrowserKit\Cookie;
10
11
/**
12
 * Class CookieFieldStrategyTest
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class CookieFieldStrategyTest extends WebTestCase
19
{
20
    protected $strategy;
21
    protected $client;
22
    protected $propertyKey;
23
24
    /**
25
     * UnitTest Starts this on reach test
26
     * @return void
27
     */
28
    public function setUp()
29
    {
30
        parent::setUp();
31
32
        /** @var \Symfony\Bundle\FrameworkBundle\Client client */
33
        $this->client = static::createClient();
34
        $this->propertyKey = $this->client->getKernel()->getContainer()->getParameter('graviton.security.authentication.strategy_key');
35
        $this->strategy = new CookieFieldStrategy(
36
            $this->propertyKey
37
        );
38
39
    }
40
41
    /**
42
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::apply
43
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo
44
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField
45
     *
46
     * @dataProvider stringProvider
47
     *
48
     * @param string $fieldValue value to check
49
     *
50
     * @return void
51
     */
52 View Code Duplication
    public function testApply($fieldValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $cookie = new Cookie(
55
            $this->propertyKey,
56
            $fieldValue,
57
            time() + 3600 * 24 * 7,
58
            '/',
59
            null,
60
            false,
61
            false
62
        );
63
        $this->client->getCookieJar()->set($cookie);
64
        $this->client->request(
65
            'GET', //method
66
            '/', //uri
67
            array(), //parameters
68
            array(), //files
69
            array() //server
70
        );
71
72
        $this->assertSame($fieldValue, $this->strategy->apply($this->client->getRequest()));
0 ignored issues
show
Documentation introduced by
$this->client->getRequest() is of type object<Symfony\Component\BrowserKit\Request>, but the function expects a object<Symfony\Component\HttpFoundation\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
    }
74
75
    /**
76
     * @return array<string>
77
     */
78 View Code Duplication
    public function stringProvider()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        return array(
81
            'plain string, no special chars' => array('exampleAuthenticationHeader'),
82
            'string with special chars'      => array("$-_.+!*'(),{}|\\^~[]`<>#%;/?:@&=."),
83
            'string with octal chars'        => array("a: \141, A: \101"),
84
            'string with hex chars'          => array("a: \x61, A: \x41")
85
        );
86
    }
87
88
    /**
89
     * Todo, find a way to have also to client id set in request stack.
90
     *
91
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::apply
92
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo
93
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField
94
     * @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::setDynamicParameters
95
     *
96
     * @dataProvider stringExtractProvider
97
     *
98
     * @param string $fieldValue value to check
99
     *
100
     * @return void
101
     */
102 View Code Duplication
    public function testApplyExtract($fieldValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $cookie = new Cookie(
105
            $this->propertyKey,
106
            $fieldValue,
107
            time() + 3600 * 24 * 7,
108
            '/',
109
            null,
110
            false,
111
            false
112
        );
113
        $this->client->getCookieJar()->set($cookie);
114
        $this->client->request(
115
            'GET', //method
116
            '/', //uri
117
            array(), //parameters
118
            array(), //files
119
            array() //server
120
        );
121
122
        $this->assertSame('testUser', $this->strategy->apply($this->client->getRequest()));
0 ignored issues
show
Documentation introduced by
$this->client->getRequest() is of type object<Symfony\Component\BrowserKit\Request>, but the function expects a object<Symfony\Component\HttpFoundation\Request>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123
    }
124
125
    /**
126
     * @return array<string>
127
     */
128
    public function stringExtractProvider()
129
    {
130
        return array(
131
            'testing extract username' => array("username=testUser,finnova_id=someId123"),
132
            'testing extract rev username' => array("finnova_id=someId123,username=testUser"),
133
        );
134
    }
135
}
136