Failed Conditions
Push — master ( 9bceb4...42a7b7 )
by Guilherme
02:51 queued 10s
created

ScopeFinderHelperTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 8
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 PHPUnit\Framework\TestCase;
15
use Symfony\Component\HttpFoundation\ParameterBag;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
19
class ScopeFinderHelperTest extends TestCase
20
{
21
    public function testGetScopeFromRequest()
22
    {
23
        $request = $this->getRequest();
24
        $request->request = new ParameterBag(['scope' => 'openid name']);
25
26
        $helper = new ScopeFinderHelper($request);
27
        $scope = $helper->getScope();
28
        $this->checkScope($scope);
29
    }
30
31
    public function testGetScopeFromQuery()
32
    {
33
        $request = $this->getRequest();
34
        $request->request = new ParameterBag();
35
        $request->query = new ParameterBag(['scope' => 'openid name']);
36
37
        $helper = new ScopeFinderHelper($request);
38
        $scope = $helper->getScope();
39
        $this->checkScope($scope);
40
    }
41
42
    public function testGetScopeFromForm()
43
    {
44
        $formName = 'some_name';
45
46
        $request = $this->getRequest();
47
        $request->request = new ParameterBag(["{$formName}" => ['scope' => 'openid name']]);
48
        $request->query = new ParameterBag();
49
50
        $form = $this->createMock('Symfony\Component\Form\FormInterface');
51
        $form->expects($this->once())->method('getName')->willReturn($formName);
52
53
        $helper = new ScopeFinderHelper($request, $form);
54
        $scope = $helper->getScope();
55
        $this->checkScope($scope);
56
    }
57
58
    public function testScopeNotFound()
59
    {
60
        $request = $this->getRequest();
61
        $request->request = new ParameterBag();
62
        $request->query = new ParameterBag();
63
64
        $helper = new ScopeFinderHelper($request);
65
        $scope = $helper->getScope();
66
        $this->assertNull($scope);
67
    }
68
69
    public function testInvalidConstructorCall()
70
    {
71
        $this->expectException('\InvalidArgumentException');
72
        new ScopeFinderHelper('error');
73
    }
74
75
    public function testConstructorWithRequestStack()
76
    {
77
        $request = $this->getRequest();
78
        $request->request = new ParameterBag(['scope' => 'openid name']);
79
80
        $stack = new RequestStack();
81
        $stack->push($request);
82
83
        $helper = new ScopeFinderHelper($stack);
84
        $scope = $helper->getScope();
85
        $this->checkScope($scope);
86
    }
87
88
    /**
89
     * @return Request|\PHPUnit_Framework_MockObject_MockObject
90
     */
91
    private function getRequest()
92
    {
93
        $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
94
            ->disableOriginalConstructor()->getMock();
95
96
        return $request;
97
    }
98
99
    private function checkScope($scope)
100
    {
101
        $this->assertCount(2, $scope);
102
        $this->assertContains('openid', $scope);
103
        $this->assertContains('name', $scope);
104
    }
105
}
106