|
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); |
|
|
|
|
|
|
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
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.