Completed
Pull Request — develop (#365)
by
unknown
23:46 queued 12:17
created

CookieFieldStrategyTest::stringExtractProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * check if reading from cookie works
4
 */
5
6
namespace Graviton\SecurityBundle\Authentication\Strategies;
7
8
use Graviton\TestBundle\Test\WebTestCase;
9
use Symfony\Component\BrowserKit\Cookie;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
12
/**
13
 * Class CookieFieldStrategyTest
14
 *
15
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
16
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
17
 * @link     http://swisscom.ch
18
 */
19
class CookieFieldStrategyTest extends WebTestCase
20
{
21
    protected $strategy;
22
    protected $client;
23
    protected $propertyKey;
24
25
    /**
26
     * UnitTest Starts this on reach test
27
     * @return void
28
     */
29
    public function setUp()
30
    {
31
        parent::setUp();
32
33
        $this->client = static::createClient();
34
        $this->propertyKey = $this->client->getKernel()
35
            ->getContainer()->getParameter('graviton.security.authentication.strategy_key');
36
        $this->strategy = new CookieFieldStrategy(
37
            $this->propertyKey
38
        );
39
40
    }
41
42
    /**
43
     * @covers       \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::apply
44
     * @covers       \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo
45
     * @covers       \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField
46
     *
47
     * @dataProvider stringProvider
48
     *
49
     * @param string $fieldValue value to check
50
     *
51
     * @return void
52
     */
53
    public function testApply($fieldValue)
54
    {
55
        $cookie = new Cookie(
56
            $this->propertyKey,
57
            $fieldValue,
58
            time() + 3600 * 24 * 7,
59
            '/',
60
            null,
61
            false,
62
            false
63
        );
64
        $this->client->getCookieJar()->set($cookie);
65
        $this->client->request(
66
            'GET', //method
67
            '/', //uri
68
            array(), //parameters
69
            array(), //files
70
            array() //server
71
        );
72
73
        $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...
74
    }
75
76
    /**
77
     * @return array<string>
78
     */
79 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...
80
    {
81
        return array(
82
            'plain string, no special chars' => array('exampleAuthenticationHeader'),
83
            'string with special chars'      => array("$-_.+!*'(),{}|\\^~[]`<>#%;/?:@&=."),
84
            'string with octal chars'        => array("a: \141, A: \101"),
85
            'string with hex chars'          => array("a: \x61, A: \x41")
86
        );
87
    }
88
89
    /**
90
     * Todo, find a way to have also to client id set in request stack.
91
     *
92
     * @covers       \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::apply
93
     * @covers       \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo
94
     * @covers       \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField
95
     * @covers       \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::setDynamicParameters
96
     *
97
     * @dataProvider stringExtractProvider
98
     *
99
     * @param string $fieldValue value to check
100
     *
101
     * @return void
102
     */
103
    public function testApplyExtract($fieldValue)
104
    {
105
        $cookie = new Cookie(
106
            $this->propertyKey,
107
            $fieldValue,
108
            time() + 3600 * 24 * 7,
109
            '/',
110
            null,
111
            false,
112
            false
113
        );
114
        $this->client->getCookieJar()->set($cookie);
115
        $this->client->request(
116
            'GET', //method
117
            '/', //uri
118
            array(), //parameters
119
            array(), //files
120
            array() //server
121
        );
122
123
        $this->strategy->setDynamicParameters(new RequestStack(), 'username', false, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

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...
124
        $username = $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...
125
        $this->assertSame('testUser', $username);
126
    }
127
128
    /**
129
     * @return array<string>
130
     */
131
    public function stringExtractProvider()
132
    {
133
        return array(
134
            'testing extract username'       => array("username=testUser,core_client_id=someId123"),
135
            'testing extract rev username'   => array("core_client_id=someId123,username=testUser"),
136
        );
137
    }
138
}
139