Failed Conditions
Push — issue#774 ( 057f04...ee9377 )
by Guilherme
05:15
created

testFetchSubjectIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 20
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\Service;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Entity\Person;
15
use LoginCidadao\CoreBundle\Model\PersonInterface;
16
use LoginCidadao\OAuthBundle\Entity\Client;
17
use LoginCidadao\OAuthBundle\Model\ClientInterface;
18
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata;
19
use LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier;
20
use LoginCidadao\OpenIDBundle\Entity\SubjectIdentifierRepository;
21
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService;
22
23
class SubjectIdentifierServiceTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27
     */
28
    private function getEntityManager()
29
    {
30
        return $this->getMock('Doctrine\ORM\EntityManagerInterface');
31
    }
32
33
    /**
34
     * @return ClientInterface|\PHPUnit_Framework_MockObject_MockObject
35
     */
36
    private function getClient()
37
    {
38
        /** @var ClientInterface|\PHPUnit_Framework_MockObject_MockObject $client */
39
        $client = $this->getMock('LoginCidadao\OAuthBundle\Model\ClientInterface');
40
41
        return $client;
42
    }
43
44
    /**
45
     * @return SubjectIdentifierRepository|\PHPUnit_Framework_MockObject_MockObject
46
     */
47
    private function getRepo()
48
    {
49
        $repo = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifierRepository')
50
            ->disableOriginalConstructor()
51
            ->getMock();
52
53
        return $repo;
54
    }
55
56
    /**
57
     * @param mixed|null $id
58
     * @return PersonInterface|\PHPUnit_Framework_MockObject_MockObject
59
     */
60
    private function getPerson($id = null)
61
    {
62
        $person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
63
        if ($id) {
64
            $person->expects($this->once())->method('getId')->willReturn($id);
65
        }
66
67
        return $person;
68
    }
69
70
    /**
71
     * @param null|string $type
72
     * @param ClientInterface|null $client
73
     * @return ClientMetadata|\PHPUnit_Framework_MockObject_MockObject
74
     */
75
    private function getClientMetadata($type = null, ClientInterface $client = null)
76
    {
77
        $metadata = $this->getMock('LoginCidadao\OpenIDBundle\Entity\ClientMetadata');
78
79
        if ($client) {
80
            $metadata->expects($this->any())->method('getClient')->willReturn($client);
81
        }
82
83
        if (!$type) {
84
            return $metadata;
85
        }
86
87
        $metadata->expects($this->once())->method('getSubjectType')->willReturn($type);
88
        if ($type === 'pairwise') {
89
            $metadata->expects($this->once())->method('getSectorIdentifier')
90
                ->willReturn('https://example.com/sector.identifier');
91
        }
92
93
        return $metadata;
94
    }
95
96
    public function testPairwiseSubjectIdentifier()
97
    {
98
        $id = 123456;
99
        $secret = 'my.secret';
100
101
        $repo = $this->getRepo();
102
        $person = $this->getPerson($id);
103
        $metadata = $this->getClientMetadata('pairwise');
104
105
        $service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret);
106
        $sub = $service->getSubjectIdentifier($person, $metadata);
107
108
        $this->assertNotNull($sub);
109
        $this->assertNotEquals($id, $sub);
110
    }
111
112
    public function testPublicSubjectIdentifier()
113
    {
114
        $id = 654321;
115
        $secret = 'my.secret';
116
117
        $repo = $this->getRepo();
118
        $person = $this->getPerson($id);
119
        $metadata = $this->getClientMetadata('public');
120
121
        $service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret);
122
        $sub = $service->getSubjectIdentifier($person, $metadata);
123
124
        $this->assertNotNull($sub);
125
        $this->assertEquals($id, $sub);
126
    }
127
128
    public function testPublicSubjectIdentifierNullMetadata()
129
    {
130
        $id = 654321;
131
        $secret = 'my.secret';
132
133
        $repo = $this->getRepo();
134
        $person = $this->getPerson($id);
135
136
        $service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret);
137
        $sub = $service->getSubjectIdentifier($person, null);
138
139
        $this->assertNotNull($sub);
140
        $this->assertEquals($id, $sub);
141
    }
142
143
    public function testFetchSubjectIdentifier()
144
    {
145
        $id = 123456;
146
        $secret = 'my.secret';
147
        $expectedSub = 'my_sub_id';
148
149
        $subId = $this->getMock('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier');
150
        $subId->expects($this->once())->method('getSubjectIdentifier')->willReturn($expectedSub);
151
152
        $repo = $this->getRepo();
153
        $repo->expects($this->once())->method('findOneBy')->willReturn($subId);
154
155
        $person = $this->getPerson();
156
        $metadata = $this->getClientMetadata();
157
158
        $service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret);
159
        $sub = $service->getSubjectIdentifier($person, $metadata);
160
161
        $this->assertNotNull($sub);
162
        $this->assertNotEquals($id, $sub);
163
    }
164
165
    public function testIsSubjectIdentifierPersisted()
166
    {
167
        $secret = 'my.secret';
168
169
        $subId = $this->getMock('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier');
170
        /** @var ClientInterface $client */
171
        $client = $this->getMock('LoginCidadao\OAuthBundle\Model\ClientInterface');
172
173
        $repo = $this->getRepo();
174
        $repo->expects($this->once())->method('findOneBy')->willReturn($subId);
175
176
        $service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret);
177
178
        $this->assertTrue($service->isSubjectIdentifierPersisted($this->getPerson(), $client));
179
    }
180
181
    public function testCreateOnEnforceSubjectIdentifier()
182
    {
183
        $id = 123456;
184
        $secret = 'my.secret';
185
186
        $em = $this->getEntityManager();
187
        $em->expects($this->once())->method('persist')
188
            ->with($this->isInstanceOf('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier'));
189
190
        $repo = $this->getRepo();
191
        $repo->expects($this->once())->method('findOneBy')->willReturn(null);
192
193
        $person = $this->getPerson($id);
194
        $client = $this->getClient();
195
        $metadata = $this->getClientMetadata(null, $client);
196
197
        $service = new SubjectIdentifierService($em, $repo, $secret);
198
        $sub = $service->enforceSubjectIdentifier($person, $metadata);
199
200
        $this->assertInstanceOf('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier', $sub);
201
    }
202
203
    public function testFetchOnEnforceSubjectIdentifier()
204
    {
205
        $secret = 'my.secret';
206
207
        $subId = $this->getMock('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier');
208
209
        $repo = $this->getRepo();
210
        $repo->expects($this->once())->method('findOneBy')->willReturn($subId);
211
212
        $person = $this->getPerson();
213
        $client = $this->getClient();
214
        $metadata = $this->getClientMetadata(null, $client);
215
216
        $service = new SubjectIdentifierService($this->getEntityManager(), $repo, $secret);
217
        $sub = $service->enforceSubjectIdentifier($person, $metadata);
218
219
        $this->assertInstanceOf('LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier', $sub);
220
        $this->assertSame($subId, $sub);
221
    }
222
223
    public function testGetPerson()
224
    {
225
        $person = new Person();
226
        $subId = 'my_sub_id';
227
        $client = new Client();
228
229
        $repo = $this->getRepo();
230
        $repo->expects($this->once())
231
            ->method('findOneBy')->with(['subjectIdentifier' => $subId, 'client' => $client])
232
            ->willReturn($person);
233
234
        $service = new SubjectIdentifierService($this->getEntityManager(), $repo, 'my.secret');
235
        $this->assertSame($person, $service->getPerson($subId, $client));
236
    }
237
238
    public function testConvertSubjectIdentifier()
239
    {
240
        $oldSub = '123';
241
242
        /** @var PersonInterface|\PHPUnit_Framework_MockObject_MockObject $person */
243
        $person = $this->getMock('LoginCidadao\CoreBundle\Model\PersonInterface');
244
        $person->expects($this->once())->method('getId')->willReturn($oldSub);
245
246
        $client = new Client();
247
        $metadata = new ClientMetadata();
248
        $metadata->setClient($client);
249
250
        $subjectIdentifier = (new SubjectIdentifier())
251
            ->setClient($client)
252
            ->setPerson($person)
253
            ->setSubjectIdentifier($oldSub);
254
255
        $repo = $this->getRepo();
256
        $repo->expects($this->once())
257
            ->method('findOneBy')->with(['person' => $person, 'client' => $client])
258
            ->willReturn($subjectIdentifier);
259
260
        $service = new SubjectIdentifierService($this->getEntityManager(), $repo, 'my.secret');
261
        $newSub = $service->convertSubjectIdentifier($person, $metadata);
262
263
        $this->assertSame($person, $newSub->getPerson());
264
        $this->assertSame($client, $newSub->getClient());
265
        $this->assertNotEquals($oldSub, $newSub->getSubjectIdentifier());
266
    }
267
}
268