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

ClientCredentialsTest::testGetClientScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 14
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\OpenIDBundle\Tests\Storage;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\OAuthBundle\Entity\Client;
15
use LoginCidadao\OAuthBundle\Entity\ClientRepository;
16
use LoginCidadao\OpenIDBundle\Storage\ClientCredentials;
17
18
class ClientCredentialsTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function testCheckClientCredentials()
21
    {
22
        $clientId = '123_randomId';
23
        $clientSecret = 'client_secret';
24
25
        $client = $this->getClient(123, 'randomId', $clientSecret);
26
27
        $em = $this->getEntityManagerFind($client, 'findOneBy');
28
29
        $clientCredentials = new ClientCredentials($em);
30
        $result = $clientCredentials->checkClientCredentials($clientId, $clientSecret);
31
32
        $this->assertTrue($result);
33
    }
34
35
    public function testCheckInvalidClientCredentials()
36
    {
37
        $clientId = '123';
38
        $clientSecret = 'client_secret';
39
40
        $client = $this->getClient(123, 'randomId', $clientSecret);
41
42
        $em = $this->getEntityManagerFind($client, 'find');
43
44
        $clientCredentials = new ClientCredentials($em);
45
        $result = $clientCredentials->checkClientCredentials($clientId, 'wrong');
46
47
        $this->assertFalse($result);
48
    }
49
50
    public function testCheckNonExistentClient()
51
    {
52
        $em = $this->getEntityManagerFind(null, 'find');
53
54
        $clientCredentials = new ClientCredentials($em);
55
        $result = $clientCredentials->checkClientCredentials(123, 'wrong');
56
57
        $this->assertFalse($result);
58
    }
59
60
    public function testGetClientDetails()
61
    {
62
        $id = 123;
63
        $randomId = 'randomId';
64
65
        $client = $this->getClient($id, $randomId, 'client_secret');
66
67
        $em = $this->getEntityManagerFind($client, 'findOneBy');
68
        $clientCredentials = new ClientCredentials($em);
69
70
        $details = $clientCredentials->getClientDetails("{$id}_{$randomId}");
71
72
        $this->assertNotEmpty($details);
73
        $this->assertCount(3, $details);
74
        $this->assertArrayHasKey('redirect_uri', $details);
75
        $this->assertArrayHasKey('client_id', $details);
76
        $this->assertArrayHasKey('grant_types', $details);
77
        $this->assertEquals($client->getPublicId(), $details['client_id']);
78
        $this->assertEquals($client->getAllowedGrantTypes(), $details['grant_types']);
79
        $this->assertContains($client->getRedirectUris()[0], $details['redirect_uri']);
80
    }
81
82
    public function testGetClientNotFoundDetails()
83
    {
84
        $id = 123;
85
        $randomId = 'randomId';
86
87
        $em = $this->getEntityManagerFind(null, 'findOneBy');
88
        $clientCredentials = new ClientCredentials($em);
89
90
        $details = $clientCredentials->getClientDetails("{$id}_{$randomId}");
91
92
        $this->assertFalse($details);
93
    }
94
95
    public function testIsPublicClient()
96
    {
97
        $id = 123;
98
        $randomId = 'randomId';
99
100
        $client = $this->getClient($id, $randomId, 'client_secret');
101
102
        $em = $this->getEntityManagerFind($client, 'findOneBy');
103
        $clientCredentials = new ClientCredentials($em);
104
105
        $result = $clientCredentials->isPublicClient("{$id}_{$randomId}");
106
107
        $this->assertFalse($result);
108
    }
109
110
    public function testIsPublicClientNotFound()
111
    {
112
        $id = 123;
113
        $randomId = 'randomId';
114
115
        $em = $this->getEntityManagerFind(null, 'findOneBy');
116
        $clientCredentials = new ClientCredentials($em);
117
118
        $result = $clientCredentials->isPublicClient("{$id}_{$randomId}");
119
120
        $this->assertFalse($result);
121
    }
122
123
    public function testGetClientScope()
124
    {
125
        $id = 123;
126
        $randomId = 'randomId';
127
128
        $client = $this->getClient($id, $randomId, 'client_secret');
129
130
        $em = $this->getEntityManagerFind($client, 'findOneBy');
131
        $clientCredentials = new ClientCredentials($em);
132
133
        $scopes = $clientCredentials->getClientScope("{$id}_{$randomId}");
134
135
        $this->assertEquals('name openid', $scopes);
136
    }
137
138
    public function testGetClientNotFoundScope()
139
    {
140
        $id = 123;
141
        $randomId = 'randomId';
142
143
        $em = $this->getEntityManagerFind(null, 'findOneBy');
144
        $clientCredentials = new ClientCredentials($em);
145
146
        $scopes = $clientCredentials->getClientScope("{$id}_{$randomId}");
147
148
        $this->assertFalse($scopes);
149
    }
150
151
    /**
152
     * @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
153
     */
154
    private function getEntityManager()
155
    {
156
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
157
158
        return $em;
159
    }
160
161
    private function getEntityManagerFind($client, $findMethod, $em = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
162
    {
163
        $repo = $this->getClientRepository();
164
        $repo->expects($this->once())->method($findMethod)->willReturn($client);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in LoginCidadao\OAuthBundle\Entity\ClientRepository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
165
166
        $em = $em ?: $this->getEntityManager();
167
        $em->expects($this->once())->method('getRepository')
168
            ->with('LoginCidadaoOAuthBundle:Client')->willReturn($repo);
169
170
        return $em;
171
    }
172
173
    /**
174
     * @return ClientRepository|\PHPUnit_Framework_MockObject_MockObject
175
     */
176
    private function getClientRepository()
177
    {
178
        $repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\ClientRepository')
179
            ->disableOriginalConstructor()->getMock();
180
181
        return $repo;
182
    }
183
184
    private function getClient($id, $randomId, $secret)
185
    {
186
        $client = new Client();
187
        $client->setId($id);
188
        $client->setRandomId($randomId);
189
        $client->setSecret($secret);
190
        $client->setRedirectUris(['https://redirect.uri']);
191
        $client->setAllowedGrantTypes(['authorization_code']);
192
        $client->setAllowedScopes(['name', 'openid']);
193
194
        return $client;
195
    }
196
}
197