Passed
Push — issue#767 ( 25f84e...39899e )
by Guilherme
07:11
created

ClientManagerTest::testGetClientByNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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