Failed Conditions
Push — issue#666 ( 4966a1...aff657 )
by Guilherme
08:23
created

ClientCredentialsTest::getEntityManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 1
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
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimRepository;
18
use LoginCidadao\RemoteClaimsBundle\Model\TagUri;
19
20
class ClientCredentialsTest extends \PHPUnit_Framework_TestCase
21
{
22
    public function testCheckClientCredentials()
23
    {
24
        $clientId = '123_randomId';
25
        $clientSecret = 'client_secret';
26
27
        $client = $this->getClient(123, 'randomId', $clientSecret);
28
29
        $em = $this->getEntityManagerFind($client, 'findOneBy');
30
31
        $clientCredentials = new ClientCredentials($em);
32
        $result = $clientCredentials->checkClientCredentials($clientId, $clientSecret);
33
34
        $this->assertTrue($result);
35
    }
36
37
    public function testCheckInvalidClientCredentials()
38
    {
39
        $clientId = '123';
40
        $clientSecret = 'client_secret';
41
42
        $client = $this->getClient(123, 'randomId', $clientSecret);
43
44
        $em = $this->getEntityManagerFind($client, 'find');
45
46
        $clientCredentials = new ClientCredentials($em);
47
        $result = $clientCredentials->checkClientCredentials($clientId, 'wrong');
48
49
        $this->assertFalse($result);
50
    }
51
52
    public function testCheckNonExistentClient()
53
    {
54
        $em = $this->getEntityManagerFind(null, 'find');
55
56
        $clientCredentials = new ClientCredentials($em);
57
        $result = $clientCredentials->checkClientCredentials(123, 'wrong');
58
59
        $this->assertFalse($result);
60
    }
61
62
    public function testGetClientDetails()
63
    {
64
        $id = 123;
65
        $randomId = 'randomId';
66
67
        $client = $this->getClient($id, $randomId, 'client_secret');
68
69
        $em = $this->getEntityManagerFind($client, 'findOneBy');
70
        $clientCredentials = new ClientCredentials($em);
71
72
        $details = $clientCredentials->getClientDetails("{$id}_{$randomId}");
73
74
        $this->assertNotEmpty($details);
75
        $this->assertCount(3, $details);
76
        $this->assertArrayHasKey('redirect_uri', $details);
77
        $this->assertArrayHasKey('client_id', $details);
78
        $this->assertArrayHasKey('grant_types', $details);
79
        $this->assertEquals($client->getPublicId(), $details['client_id']);
80
        $this->assertEquals($client->getAllowedGrantTypes(), $details['grant_types']);
81
        $this->assertContains($client->getRedirectUris()[0], $details['redirect_uri']);
82
    }
83
84
    public function testGetClientNotFoundDetails()
85
    {
86
        $id = 123;
87
        $randomId = 'randomId';
88
89
        $em = $this->getEntityManagerFind(null, 'findOneBy');
90
        $clientCredentials = new ClientCredentials($em);
91
92
        $details = $clientCredentials->getClientDetails("{$id}_{$randomId}");
93
94
        $this->assertFalse($details);
95
    }
96
97
    public function testIsPublicClient()
98
    {
99
        $id = 123;
100
        $randomId = 'randomId';
101
102
        $client = $this->getClient($id, $randomId, 'client_secret');
103
104
        $em = $this->getEntityManagerFind($client, 'findOneBy');
105
        $clientCredentials = new ClientCredentials($em);
106
107
        $result = $clientCredentials->isPublicClient("{$id}_{$randomId}");
108
109
        $this->assertFalse($result);
110
    }
111
112
    public function testIsPublicClientNotFound()
113
    {
114
        $id = 123;
115
        $randomId = 'randomId';
116
117
        $em = $this->getEntityManagerFind(null, 'findOneBy');
118
        $clientCredentials = new ClientCredentials($em);
119
120
        $result = $clientCredentials->isPublicClient("{$id}_{$randomId}");
121
122
        $this->assertFalse($result);
123
    }
124
125
    public function testGetClientScopeWithRemoteClaim()
126
    {
127
        $id = 123;
128
        $randomId = 'randomId';
129
        $expectedRemoteClaim = 'tag:example.com,2017:my_claim';
130
131
        $client = $this->getClient($id, $randomId, 'client_secret');
132
133
        $em = $this->getEntityManagerFind($client, 'findOneBy', null, $expectedRemoteClaim);
134
        $clientCredentials = new ClientCredentials($em);
135
136
        $scopes = explode(' ', $clientCredentials->getClientScope("{$id}_{$randomId}"));
137
138
        $this->assertContains('name', $scopes);
139
        $this->assertContains('openid', $scopes);
140
        $this->assertContains($expectedRemoteClaim, $scopes);
141
    }
142
143
    public function testGetClientScopeWithoutRemoteClaim()
144
    {
145
        $id = 123;
146
        $randomId = 'randomId';
147
148
        $client = $this->getClient($id, $randomId, 'client_secret');
149
150
        $em = $this->getEntityManagerFind($client, 'findOneBy');
151
        $clientCredentials = new ClientCredentials($em);
152
153
        $scopes = explode(' ', $clientCredentials->getClientScope("{$id}_{$randomId}"));
154
155
        $this->assertContains('name', $scopes);
156
        $this->assertContains('openid', $scopes);
157
    }
158
159
    public function testGetClientNotFoundScope()
160
    {
161
        $id = 123;
162
        $randomId = 'randomId';
163
164
        $em = $this->getEntityManagerFind(null, 'findOneBy');
165
        $clientCredentials = new ClientCredentials($em);
166
167
        $scopes = $clientCredentials->getClientScope("{$id}_{$randomId}");
168
169
        $this->assertFalse($scopes);
170
    }
171
172
    /**
173
     * @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
174
     */
175
    private function getEntityManager()
176
    {
177
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
178
179
        return $em;
180
    }
181
182
    private function getEntityManagerFind($client, $findMethod, $em = null, $expectedRemoteClaim = 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...
183
    {
184
        $clientRepo = $this->getClientRepository();
185
        $clientRepo->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...
186
187
        $remoteClaimRepo = $this->getRemoteClaimRepository();
188
189
        $em = $em ?: $this->getEntityManager();
190
        $em->expects($this->atMost(2))->method('getRepository')
191
            ->willReturnCallback(function ($entity) use ($clientRepo, $remoteClaimRepo, $expectedRemoteClaim) {
192
                switch ($entity) {
193
                    case 'LoginCidadaoOAuthBundle:Client':
194
                        return $clientRepo;
195
                    case 'LoginCidadaoRemoteClaimsBundle:RemoteClaim':
196
                        $remoteClaims = [];
197
                        if ($expectedRemoteClaim !== null) {
198
                            $expectedRemoteClaim = TagUri::createFromString($expectedRemoteClaim);
199
                            $remoteClaim = $this->getMock('LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface');
200
                            $remoteClaim->expects($this->any())->method('getName')->willReturn($expectedRemoteClaim);
201
                            $remoteClaims[] = $remoteClaim;
202
                        }
203
                        $remoteClaimRepo->expects($this->once())->method('findAll')->willReturn($remoteClaims);
204
205
                        return $remoteClaimRepo;
206
                    default:
207
                        return null;
208
                }
209
            });
210
211
        return $em;
212
    }
213
214
    /**
215
     * @return ClientRepository|\PHPUnit_Framework_MockObject_MockObject
216
     */
217
    private function getClientRepository()
218
    {
219
        $repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\ClientRepository')
220
            ->disableOriginalConstructor()->getMock();
221
222
        return $repo;
223
    }
224
225
    /**
226
     * @return RemoteClaimRepository|\PHPUnit_Framework_MockObject_MockObject
227
     */
228
    private function getRemoteClaimRepository()
229
    {
230
        $repo = $this->getMockBuilder('LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimRepository')
231
            ->disableOriginalConstructor()->getMock();
232
233
        return $repo;
234
    }
235
236
    private function getClient($id, $randomId, $secret)
237
    {
238
        $client = new Client();
239
        $client->setId($id);
240
        $client->setRandomId($randomId);
241
        $client->setSecret($secret);
242
        $client->setRedirectUris(['https://redirect.uri']);
243
        $client->setAllowedGrantTypes(['authorization_code']);
244
        $client->setAllowedScopes(['name', 'openid']);
245
246
        return $client;
247
    }
248
}
249