Failed Conditions
Push — issue#702 ( d312bf...92afd8 )
by Guilherme
06:53
created

ClientManagerTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 192
rs 10
c 0
b 0
f 0
wmc 14
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\Manager;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Entity\Person;
15
use LoginCidadao\CoreBundle\Entity\PersonRepository;
16
use LoginCidadao\OAuthBundle\Entity\Client;
17
use LoginCidadao\OAuthBundle\Entity\ClientRepository;
18
use LoginCidadao\OAuthBundle\Model\ClientInterface;
19
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata;
20
use LoginCidadao\OpenIDBundle\Manager\ClientManager;
21
use PHPUnit\Framework\TestCase;
22
use Symfony\Component\EventDispatcher\EventDispatcher;
23
24
class ClientManagerTest extends TestCase
25
{
26
    public function testGetClientById()
27
    {
28
        $id = 123;
29
30
        $repo = $this->getClientRepository();
31
        $repo->expects($this->once())->method('find')->with($id)->willReturn(new Client());
32
33
        $em = $this->getEntityManager();
34
        $em->expects($this->once())->method('getRepository')->with('LoginCidadaoOAuthBundle:Client')
35
            ->willReturn($repo);
36
37
        $manager = new ClientManager($em, $this->getDispatcher(), $this->getPersonRepository(), '');
38
        $this->assertInstanceOf('LoginCidadao\OAuthBundle\Model\ClientInterface', $manager->getClientById($id));
39
    }
40
41
    public function testGetClientByNull()
42
    {
43
        $em = $this->getEntityManager();
44
        $em->expects($this->never())->method('getRepository');
45
46
        $manager = new ClientManager($em, $this->getDispatcher(), $this->getPersonRepository(), '');
47
        $this->assertNull($manager->getClientById(null));
48
    }
49
50
    public function testGetClientByPublicId()
51
    {
52
        $id = '123_randomIdHere';
53
54
        $repo = $this->getClientRepository();
55
        $repo->expects($this->once())->method('findOneBy')
56
            ->with(['id' => 123, 'randomId' => 'randomIdHere'])
57
            ->willReturn(new Client());
58
59
        $em = $this->getEntityManager();
60
        $em->expects($this->once())->method('getRepository')->with('LoginCidadaoOAuthBundle:Client')
61
            ->willReturn($repo);
62
63
        $manager = new ClientManager($em, $this->getDispatcher(), $this->getPersonRepository(), '');
64
        $manager->getClientById($id);
65
    }
66
67
    /**
68
     * @throws \Doctrine\DBAL\Exception\UniqueConstraintViolationException
69
     */
70
    public function testRegisterEmpty()
71
    {
72
        $em = $this->emExpectRegistration($this->getEntityManager());
73
74
        $manager = new ClientManager($em, $this->getDispatcher(), $this->getPersonRepository(), 'scope1 scope2');
75
        $metadata = $manager->populateNewMetadata(new ClientMetadata());
76
77
        $registeredClient = $manager->register($metadata);
78
79
        $this->assertInstanceOf('LoginCidadao\OAuthBundle\Model\ClientInterface', $registeredClient);
80
        $this->assertSame('clientID', $registeredClient->getId());
81
    }
82
83
    /**
84
     * @throws \Doctrine\DBAL\Exception\UniqueConstraintViolationException
85
     */
86
    public function testRegisterWithClient()
87
    {
88
        $client = new Client();
89
        $metadata = (new ClientMetadata())
90
            ->setClient($client);
91
92
        $em = $this->emExpectRegistration($this->getEntityManager());
93
94
        $manager = new ClientManager($em, $this->getDispatcher(), $this->getPersonRepository(), 'scope1 scope2');
95
96
        $registeredClient = $manager->register($metadata);
97
98
        $this->assertInstanceOf('LoginCidadao\OAuthBundle\Model\ClientInterface', $registeredClient);
99
        $this->assertSame('clientID', $registeredClient->getId());
100
        $this->assertSame($client, $registeredClient);
101
    }
102
103
    /**
104
     * @throws \Doctrine\DBAL\Exception\UniqueConstraintViolationException
105
     */
106
    public function testRegisterWithContacts()
107
    {
108
        $emails = ['[email protected]', '[email protected]', '[email protected]'];
109
        $metadata = (new ClientMetadata())
110
            ->setContacts($emails);
111
        $users = [
112
            (new Person())
113
                ->setEmail('[email protected]')
114
                ->setEmailConfirmedAt(new \DateTime()),
115
            (new Person())
116
                ->setEmail('[email protected]'),
117
        ];
118
119
        $em = $this->emExpectRegistration($this->getEntityManager());
120
121
        $personRepo = $this->getPersonRepository();
122
        $personRepo->expects($this->once())
123
            ->method('findBy')->with(['email' => $emails])
124
            ->willReturn($users);
125
126
        $manager = new ClientManager($em, $this->getDispatcher(), $personRepo, 'scope1 scope2');
127
        $metadata = $manager->populateNewMetadata($metadata);
128
129
        $registeredClient = $manager->register($metadata);
130
131
        $this->assertInstanceOf('LoginCidadao\OAuthBundle\Model\ClientInterface', $registeredClient);
132
        $this->assertSame('clientID', $registeredClient->getId());
133
        $this->assertCount(1, $registeredClient->getOwners());
134
    }
135
136
    public function testPopulateNewMetadata()
137
    {
138
        $client = (new Client())
139
            ->setRedirectUris(['https://example.com']);
140
        $metadata = (new ClientMetadata())
141
            ->setClient($client);
142
143
        $em = $this->getEntityManager();
144
145
        $manager = new ClientManager($em, $this->getDispatcher(), $this->getPersonRepository(),
146
            'scope1 scope2');
147
        $metadata = $manager->populateNewMetadata($metadata);
148
        $this->assertSame('example.com', $metadata->getClient()->getName());
149
    }
150
151
    /**
152
     * @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
153
     */
154
    private function getEntityManager()
155
    {
156
        $em = $this->createMock('Doctrine\ORM\EntityManagerInterface');
157
158
        return $em;
159
    }
160
161
    /**
162
     * @param EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject $em
163
     * @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
164
     */
165
    private function emExpectRegistration($em)
166
    {
167
        $allowedParams = $this->logicalOr(
168
            $this->isInstanceOf('LoginCidadao\OAuthBundle\Model\ClientInterface'),
169
            $this->isInstanceOf('LoginCidadao\OpenIDBundle\Entity\ClientMetadata')
170
        );
171
        $em->expects($this->exactly(2))
172
            ->method('persist')->with($allowedParams)
173
            ->willReturnCallback(function ($entity) {
174
                if ($entity instanceof ClientInterface) {
175
                    $entity->setId('clientID');
176
                }
177
                if ($entity instanceof ClientMetadata) {
178
                    $entity->setId('metadataID');
179
                }
180
181
                return $entity;
182
            });
183
        $em->expects($this->once())->method('flush');
184
185
        return $em;
186
    }
187
188
    /**
189
     * @return ClientRepository|\PHPUnit_Framework_MockObject_MockObject
190
     */
191
    private function getClientRepository()
192
    {
193
        $repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\ClientRepository')
194
            ->disableOriginalConstructor()->getMock();
195
196
        return $repo;
197
    }
198
199
    /**
200
     * @return EventDispatcher|\PHPUnit_Framework_MockObject_MockObject
201
     */
202
    private function getDispatcher()
203
    {
204
        $dispatcher = $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
205
206
        return $dispatcher;
207
    }
208
209
    /**
210
     * @return PersonRepository|\PHPUnit_Framework_MockObject_MockObject
211
     */
212
    private function getPersonRepository()
213
    {
214
        return $this->getMockBuilder('LoginCidadao\CoreBundle\Entity\PersonRepository')
215
            ->disableOriginalConstructor()->getMock();
216
    }
217
}
218