Completed
Push — issue#785 ( 9195c5...2b733c )
by Guilherme
04:54
created

AccessTokenTest::getClientManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 7
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\Tests\OpenIDBundle\Storage;
12
13
use Doctrine\ORM\EntityManager;
14
use Doctrine\ORM\EntityRepository;
15
use LoginCidadao\CoreBundle\Entity\Person;
16
use LoginCidadao\CoreBundle\Entity\PersonRepository;
17
use LoginCidadao\OAuthBundle\Entity\Client;
18
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata;
19
use LoginCidadao\OpenIDBundle\Manager\ClientManager;
20
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService;
21
use LoginCidadao\OpenIDBundle\Storage\AccessToken;
22
23
class AccessTokenTest extends \PHPUnit_Framework_TestCase
24
{
25
26
    public function testGetAccessToken()
27
    {
28
        $clientId = 'client_id';
29
        $token = 'my.access.token';
30
        $expires = time();
31
        $scope = 'scope1 scope2';
32
        $idToken = 'id-token-here';
33
34
        $clientMetadata = new ClientMetadata();
35
36
        $client = new Client();
37
        $client->setId('client');
38
        $client->setRandomId('id');
39
        $client->setMetadata($clientMetadata);
40
41
        $person = new Person();
42
        $accessToken = new \LoginCidadao\OAuthBundle\Entity\AccessToken();
43
        $accessToken->setClient($client);
44
        $accessToken->setUser($person);
45
        $accessToken->setExpiresAt($expires);
46
        $accessToken->setScope($scope);
47
        $accessToken->setIdToken($idToken);
48
49
        $repo = $this->getMockBuilder(EntityRepository::class)->disableOriginalConstructor()->getMock();
50
        $repo->expects($this->once())
51
            ->method('findOneBy')->with(['token' => $token])
52
            ->willReturn($accessToken);
53
54
        $em = $this->getEntityManager();
55
        $em->expects($this->once())
56
            ->method('getRepository')->with('LoginCidadaoOAuthBundle:AccessToken')
57
            ->willReturn($repo);
58
59
        $subIdService = $this->getSubjectIdentifierService();
60
        $subIdService->expects($this->once())
61
            ->method('getSubjectIdentifier')->with($person, $clientMetadata)
62
            ->willReturn('subId');
63
64
        $accessTokenStorage = new AccessToken($em);
65
        $accessTokenStorage->setSubjectIdentifierService($subIdService);
66
67
        $this->assertSame([
68
            'client_id' => $clientId,
69
            'user_id' => 'subId',
70
            'expires' => $expires,
71
            'scope' => $scope,
72
            'id_token' => $idToken,
73
        ], $accessTokenStorage->getAccessToken($token));
74
    }
75
76
    public function testGetAccessTokenNotFound()
77
    {
78
        $token = 'my.access.token';
79
        $repo = $this->getMockBuilder(EntityRepository::class)->disableOriginalConstructor()->getMock();
80
        $repo->expects($this->once())
81
            ->method('findOneBy')->with(['token' => $token])
82
            ->willReturn(null);
83
84
        $em = $this->getEntityManager();
85
        $em->expects($this->once())
86
            ->method('getRepository')->with('LoginCidadaoOAuthBundle:AccessToken')
87
            ->willReturn($repo);
88
89
        $accessTokenStorage = new AccessToken($em);
90
        $this->assertNull($accessTokenStorage->getAccessToken($token));
91
    }
92
93
    /**
94
     * @throws \Doctrine\ORM\OptimisticLockException
95
     */
96
    public function testSetAccessToken()
97
    {
98
        $clientId = 'client_id';
99
        $userId = 'subId';
100
        $token = 'my.access.token';
101
        $expires = time();
102
        $scope = 'scope1 scope2';
103
        $idToken = 'id-token-here';
104
        $client = new Client();
105
106
        $person = new Person();
107
        $personRepo = $this->getPersonRepository();
108
        $personRepo->expects($this->once())
109
            ->method('find')->with($userId)
110
            ->willReturn($person);
111
112
        $subIdService = $this->getSubjectIdentifierService();
113
        $subIdService->expects($this->once())
114
            ->method('getPerson')->with($userId, $client)
115
            ->willReturn(null);
116
117
        $em = $this->getEntityManager();
118
        $em->expects($this->once())->method('flush');
119
        $em->expects($this->once())->method('persist')
120
            ->with($this->isInstanceOf(\LoginCidadao\OAuthBundle\Entity\AccessToken::class));
121
        $em->expects($this->once())
122
            ->method('getRepository')->with('LoginCidadaoCoreBundle:Person')
123
            ->willReturn($personRepo);
124
125
        $clientManager = $this->getClientManager();
126
        $clientManager->expects($this->once())
127
            ->method('getClientById')->with($clientId)
128
            ->willReturn($client);
129
130
        $accessTokenStorage = new AccessToken($em);
131
        $accessTokenStorage->setSubjectIdentifierService($subIdService);
132
        $accessTokenStorage->setClientManager($clientManager);
133
        $accessTokenStorage->setAccessToken($token, $clientId, $userId, $expires, $scope, $idToken);
134
    }
135
136
    /**
137
     * @throws \Doctrine\ORM\OptimisticLockException
138
     */
139
    public function testSetAccessTokenClientCredentials()
140
    {
141
        $clientId = 'client_id';
142
        $userId = null;
143
        $token = 'my.access.token';
144
        $expires = time();
145
        $scope = 'scope1 scope2';
146
        $idToken = 'id-token-here';
147
        $client = new Client();
148
149
        $clientManager = $this->getClientManager();
150
        $clientManager->expects($this->once())
151
            ->method('getClientById')->with($clientId)
152
            ->willReturn($client);
153
154
        $accessTokenStorage = new AccessToken($this->getEntityManager());
155
        $accessTokenStorage->setClientManager($clientManager);
156
        $this->assertNull($accessTokenStorage->setAccessToken($token, $clientId, $userId, $expires, $scope, $idToken));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $accessTokenStorage->set...ires, $scope, $idToken) targeting LoginCidadao\OpenIDBundl...Token::setAccessToken() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
157
    }
158
159
    /**
160
     * @return EntityManager|\PHPUnit_Framework_MockObject_MockObject
161
     */
162
    private function getEntityManager()
163
    {
164
        return $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock();
165
    }
166
167
    /**
168
     * @return PersonRepository|\PHPUnit_Framework_MockObject_MockObject
169
     */
170
    private function getPersonRepository()
171
    {
172
        return $this->getMockBuilder(PersonRepository::class)
173
            ->disableOriginalConstructor()->getMock();
174
    }
175
176
    /**
177
     * @return SubjectIdentifierService|\PHPUnit_Framework_MockObject_MockObject
178
     */
179
    private function getSubjectIdentifierService()
180
    {
181
        return $this->getMockBuilder(SubjectIdentifierService::class)
182
            ->disableOriginalConstructor()->getMock();
183
    }
184
185
    /**
186
     * @return ClientManager|\PHPUnit_Framework_MockObject_MockObject
187
     */
188
    private function getClientManager()
189
    {
190
        /** @var ClientManager|\PHPUnit_Framework_MockObject_MockObject $clientManager */
191
        $clientManager = $this->getMockBuilder(ClientManager::class)
192
            ->disableOriginalConstructor()->getMock();
193
194
        return $clientManager;
195
    }
196
}
197