Completed
Push — master ( c3efa2...a812ec )
by Guilherme
13s
created

testConstructorWithRequestStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OAuthBundle\Tests\Helper;
12
13
use LoginCidadao\OAuthBundle\Helper\ScopeFinderHelper;
14
use Symfony\Component\HttpFoundation\ParameterBag;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
18
class ScopeFinderHelperTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function testGetScopeFromRequest()
21
    {
22
        $request = $this->getRequest();
23
        $request->request = new ParameterBag(['scope' => 'openid name']);
24
25
        $helper = new ScopeFinderHelper($request);
26
        $scope = $helper->getScope();
27
        $this->checkScope($scope);
28
    }
29
30
    public function testGetScopeFromQuery()
31
    {
32
        $request = $this->getRequest();
33
        $request->request = new ParameterBag();
34
        $request->query = new ParameterBag(['scope' => 'openid name']);
35
36
        $helper = new ScopeFinderHelper($request);
37
        $scope = $helper->getScope();
38
        $this->checkScope($scope);
39
    }
40
41
    public function testGetScopeFromForm()
42
    {
43
        $formName = 'some_name';
44
45
        $request = $this->getRequest();
46
        $request->request = new ParameterBag(["{$formName}" => ['scope' => 'openid name']]);
47
        $request->query = new ParameterBag();
48
49
        $form = $this->getMock('Symfony\Component\Form\FormInterface');
50
        $form->expects($this->once())->method('getName')->willReturn($formName);
51
52
        $helper = new ScopeFinderHelper($request, $form);
53
        $scope = $helper->getScope();
54
        $this->checkScope($scope);
55
    }
56
57
    public function testScopeNotFound()
58
    {
59
        $request = $this->getRequest();
60
        $request->request = new ParameterBag();
61
        $request->query = new ParameterBag();
62
63
        $helper = new ScopeFinderHelper($request);
64
        $scope = $helper->getScope();
65
        $this->assertNull($scope);
66
    }
67
68
    public function testInvalidConstructorCall()
69
    {
70
        $this->setExpectedException('\InvalidArgumentException');
71
        new ScopeFinderHelper('error');
72
    }
73
74
    public function testConstructorWithRequestStack()
75
    {
76
        $request = $this->getRequest();
77
        $request->request = new ParameterBag(['scope' => 'openid name']);
78
79
        $stack = new RequestStack();
80
        $stack->push($request);
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->getRequest() on line 76 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Symfony\Component\HttpFo...on\RequestStack::push() does only seem to accept object<Symfony\Component\HttpFoundation\Request>, 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...
81
82
        $helper = new ScopeFinderHelper($stack);
83
        $scope = $helper->getScope();
84
        $this->checkScope($scope);
85
    }
86
87
    /**
88
     * @return Request|\PHPUnit_Framework_MockObject_MockObject
89
     */
90
    private function getRequest()
91
    {
92
        $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
93
            ->disableOriginalConstructor()->getMock();
94
95
        return $request;
96
    }
97
98
    private function checkScope($scope)
99
    {
100
        $this->assertCount(2, $scope);
101
        $this->assertContains('openid', $scope);
102
        $this->assertContains('name', $scope);
103
    }
104
}
105