Failed Conditions
Push — issue#767 ( 20b3b1 )
by Guilherme
10:48
created

ClientManagerTest::emExpectRegistration()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 1
nop 1
dl 0
loc 21
rs 9.3142
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')->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
        $manager->getClientById($id);
38
    }
39
40
    public function testGetClientByPublicId()
41
    {
42
        $id = '123_randomIdHere';
43
44
        $repo = $this->getClientRepository();
45
        $repo->expects($this->once())->method('findOneBy')
46
            ->with(['id' => 123, 'randomId' => 'randomIdHere'])
47
            ->willReturn(new Client());
48
49
        $em = $this->getEntityManager();
50
        $em->expects($this->once())->method('getRepository')->with('LoginCidadaoOAuthBundle:Client')
51
            ->willReturn($repo);
52
53
        $manager = new ClientManager($em, $this->getDispatcher(), $this->getPersonRepository(), '');
54
        $manager->getClientById($id);
55
    }
56
57
    public function testRegisterEmpty()
58
    {
59
        $em = $this->emExpectRegistration($this->getEntityManager());
60
61
        $manager = new ClientManager($em, $this->getDispatcher(), $this->getPersonRepository(), 'scope1 scope2');
62
63
        $registeredClient = $manager->register(new ClientMetadata());
64
65
        $this->assertInstanceOf('LoginCidadao\OAuthBundle\Model\ClientInterface', $registeredClient);
66
        $this->assertSame('clientID', $registeredClient->getId());
67
    }
68
69
    public function testRegisterWithClient()
70
    {
71
        $client = new Client();
72
        $metadata = (new ClientMetadata())
73
            ->setClient($client);
74
75
        $em = $this->emExpectRegistration($this->getEntityManager());
76
77
        $manager = new ClientManager($em, $this->getDispatcher(), $this->getPersonRepository(), 'scope1 scope2');
78
79
        $registeredClient = $manager->register($metadata);
80
81
        $this->assertInstanceOf('LoginCidadao\OAuthBundle\Model\ClientInterface', $registeredClient);
82
        $this->assertSame('clientID', $registeredClient->getId());
83
        $this->assertSame($client, $registeredClient);
84
    }
85
86
    public function testRegisterWithContacts()
87
    {
88
        $emails = ['[email protected]', '[email protected]', '[email protected]'];
89
        $metadata = (new ClientMetadata())
90
            ->setContacts($emails);
91
        $users = [
92
            (new Person())
93
                ->setEmail('[email protected]')
94
                ->setEmailConfirmedAt(new \DateTime()),
95
            (new Person())
96
                ->setEmail('[email protected]'),
97
        ];
98
99
        $em = $this->emExpectRegistration($this->getEntityManager());
100
101
        $personRepo = $this->getPersonRepository();
102
        $personRepo->expects($this->once())
103
            ->method('findBy')->with(['email' => $emails])
104
            ->willReturn($users);
105
106
        $manager = new ClientManager($em, $this->getDispatcher(), $personRepo, 'scope1 scope2');
107
108
        $registeredClient = $manager->register($metadata);
109
110
        $this->assertInstanceOf('LoginCidadao\OAuthBundle\Model\ClientInterface', $registeredClient);
111
        $this->assertSame('clientID', $registeredClient->getId());
112
        $this->assertCount(1, $registeredClient->getOwners());
113
    }
114
115
    /**
116
     * @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
117
     */
118
    private function getEntityManager()
119
    {
120
        $em = $this->getMock('Doctrine\ORM\EntityManagerInterface');
121
122
        return $em;
123
    }
124
125
    /**
126
     * @param EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject $em
127
     * @return EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
128
     */
129
    private function emExpectRegistration($em)
130
    {
131
        $allowedParams = $this->logicalOr(
132
            $this->isInstanceOf('LoginCidadao\OAuthBundle\Model\ClientInterface'),
133
            $this->isInstanceOf('LoginCidadao\OpenIDBundle\Entity\ClientMetadata')
134
        );
135
        $em->expects($this->exactly(2))
136
            ->method('persist')->with($allowedParams)
137
            ->willReturnCallback(function ($entity) {
138
                if ($entity instanceof ClientInterface) {
139
                    $entity->setId('clientID');
140
                }
141
                if ($entity instanceof ClientMetadata) {
142
                    $entity->setId('metadataID');
143
                }
144
145
                return $entity;
146
            });
147
        $em->expects($this->once())->method('flush');
148
149
        return $em;
150
    }
151
152
    /**
153
     * @return ClientRepository|\PHPUnit_Framework_MockObject_MockObject
154
     */
155
    private function getClientRepository()
156
    {
157
        $repo = $this->getMockBuilder('LoginCidadao\OAuthBundle\Entity\ClientRepository')
158
            ->disableOriginalConstructor()->getMock();
159
160
        return $repo;
161
    }
162
163
    /**
164
     * @return EventDispatcher|\PHPUnit_Framework_MockObject_MockObject
165
     */
166
    private function getDispatcher()
167
    {
168
        $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
169
170
        return $dispatcher;
171
    }
172
173
    /**
174
     * @return PersonRepository|\PHPUnit_Framework_MockObject_MockObject
175
     */
176
    private function getPersonRepository()
177
    {
178
        return $this->getMockBuilder('LoginCidadao\CoreBundle\Entity\PersonRepository')
179
            ->disableOriginalConstructor()->getMock();
180
    }
181
}
182