Passed
Push — master ( 7438ce...69adc8 )
by Guilherme
01:29 queued 11s
created

NfgSoapTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 255
rs 10
c 0
b 0
f 0
wmc 15
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 PROCERGS\LoginCidadao\NfgBundle\Tests\Service;
12
13
use libphonenumber\PhoneNumberFormat;
14
use libphonenumber\PhoneNumberUtil;
15
use PHPUnit\Framework\TestCase;
16
use PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException;
17
use PROCERGS\LoginCidadao\NfgBundle\Security\Credentials;
18
use PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoap;
19
20
/**
21
 * @codeCoverageIgnore
22
 */
23
class NfgSoapTest extends TestCase
24
{
25
    public function testValidGetAccessID()
26
    {
27
        $client = $this->getSoapClientMock();
28
        $credentials = new Credentials('org', 'user', 'pass');
29
        $nfgSoap = new NfgSoap($client, $credentials);
30
31
        $accessId = $nfgSoap->getAccessID();
32
        $this->assertEquals('ok', $accessId);
33
    }
34
35
    public function testInvalidCredentialsGetAccessID()
36
    {
37
        $this->expectException(NfgServiceUnavailableException::class);
38
39
        $client = $this->getSoapClientMock();
40
        $credentials = new Credentials('invalid', 'invalid', 'invalid');
41
        $nfgSoap = new NfgSoap($client, $credentials);
42
        $nfgSoap->getAccessID();
43
    }
44
45
    public function testGetFullUserInfo()
46
    {
47
        $voterRegistration = '1234';
48
        $userInfo = [
49
            'CodCpf' => '5745778407',
50
            'NomeConsumidor' => 'John Doe',
51
            'DtNasc' => '1970-01-01T00:00:00',
52
            'EmailPrinc' => '[email protected]',
53
            'NroFoneContato' => '51992345678',
54
            'CodSitTitulo' => '1',
55
        ];
56
57
        $client = $this->getSoapClientMock($userInfo);
58
        $credentials = new Credentials('org', 'user', 'pass');
59
        $nfgSoap = new NfgSoap($client, $credentials);
60
61
        $nfgProfile = $nfgSoap->getUserInfo('stub', $voterRegistration);
62
63
        $phoneUtil = PhoneNumberUtil::getInstance();
64
        $this->assertEquals($userInfo['NomeConsumidor'], $nfgProfile->getName());
65
        $this->assertEquals($userInfo['EmailPrinc'], $nfgProfile->getEmail());
66
        $this->assertInstanceOf('\DateTime', $nfgProfile->getBirthdate());
67
        $this->assertEquals($userInfo['DtNasc'], $nfgProfile->getBirthdate()->format('Y-m-d\TH:i:s'));
68
        $this->assertNotNull($nfgProfile->getMobile());
69
        $this->assertEquals(
70
            "+55{$userInfo['NroFoneContato']}",
71
            $phoneUtil->format($nfgProfile->getMobile(), PhoneNumberFormat::E164)
72
        );
73
        $this->assertEquals($userInfo['CodSitTitulo'], $nfgProfile->getVoterRegistrationSit());
74
        $this->assertEquals($voterRegistration, $nfgProfile->getVoterRegistration());
75
    }
76
77
    public function testInvalidPhone()
78
    {
79
        $voterRegistration = '1234';
80
        $userInfo = [
81
            'CodCpf' => '5745778407',
82
            'NomeConsumidor' => 'John Doe',
83
            'DtNasc' => '1970-01-01T00:00:00',
84
            'EmailPrinc' => '[email protected]',
85
            'NroFoneContato' => 'aa',
86
            'CodSitTitulo' => '1',
87
        ];
88
89
        $client = $this->getSoapClientMock($userInfo);
90
        $credentials = new Credentials('org', 'user', 'pass');
91
        $nfgSoap = new NfgSoap($client, $credentials);
92
93
        $nfgProfile = $nfgSoap->getUserInfo('stub', $voterRegistration);
94
95
        $this->assertEquals($userInfo['NomeConsumidor'], $nfgProfile->getName());
96
        $this->assertEquals($userInfo['EmailPrinc'], $nfgProfile->getEmail());
97
        $this->assertInstanceOf('\DateTime', $nfgProfile->getBirthdate());
98
        $this->assertEquals($userInfo['DtNasc'], $nfgProfile->getBirthdate()->format('Y-m-d\TH:i:s'));
99
        $this->assertNull($nfgProfile->getMobile());
100
        $this->assertEquals($userInfo['CodSitTitulo'], $nfgProfile->getVoterRegistrationSit());
101
        $this->assertEquals($voterRegistration, $nfgProfile->getVoterRegistration());
102
    }
103
104
    public function testErrorResponse()
105
    {
106
        $this->expectException(\RuntimeException::class);
107
        $userInfo = [
108
            'CodSitRetorno' => '500',
109
            'MsgRetorno' => 'Some error',
110
        ];
111
112
        $client = $this->getSoapClientMock($userInfo);
113
        $credentials = new Credentials('org', 'user', 'pass');
114
        $nfgSoap = new NfgSoap($client, $credentials);
115
116
        $nfgSoap->getUserInfo('stub');
117
    }
118
119
    public function testPhoneMissing()
120
    {
121
        $voterRegistration = '1234';
122
        $userInfo = [
123
            'CodCpf' => '5745778407',
124
            'NomeConsumidor' => 'John Doe',
125
            'DtNasc' => '1970-01-01T00:00:00',
126
            'EmailPrinc' => '[email protected]',
127
            'NroFoneContato' => null,
128
            'CodSitTitulo' => '1',
129
        ];
130
131
        $client = $this->getSoapClientMock($userInfo);
132
        $credentials = new Credentials('org', 'user', 'pass');
133
        $nfgSoap = new NfgSoap($client, $credentials);
134
135
        $nfgProfile = $nfgSoap->getUserInfo('stub', $voterRegistration);
136
137
        $this->assertEquals($userInfo['NomeConsumidor'], $nfgProfile->getName());
138
        $this->assertEquals($userInfo['EmailPrinc'], $nfgProfile->getEmail());
139
        $this->assertInstanceOf('\DateTime', $nfgProfile->getBirthdate());
140
        $this->assertEquals($userInfo['DtNasc'], $nfgProfile->getBirthdate()->format('Y-m-d\TH:i:s'));
141
        $this->assertNull($nfgProfile->getMobile());
142
        $this->assertEquals($userInfo['CodSitTitulo'], $nfgProfile->getVoterRegistrationSit());
143
        $this->assertEquals($voterRegistration, $nfgProfile->getVoterRegistration());
144
    }
145
146
    public function testMinimalInfo()
147
    {
148
        $userInfo = [
149
            'CodCpf' => true,
150
            'NomeConsumidor' => null,
151
            'DtNasc' => null,
152
            'EmailPrinc' => null,
153
            'NroFoneContato' => null,
154
            'CodSitTitulo' => '0',
155
        ];
156
157
        $client = $this->getSoapClientMock($userInfo);
158
        $credentials = new Credentials('org', 'user', 'pass');
159
        $nfgSoap = new NfgSoap($client, $credentials);
160
161
        $nfgProfile = $nfgSoap->getUserInfo('stub');
162
163
        $this->assertNull($nfgProfile->getName());
164
        $this->assertNull($nfgProfile->getEmail());
165
        $this->assertNull($nfgProfile->getBirthdate());
166
        $this->assertNull($nfgProfile->getMobile());
167
        $this->assertEquals($userInfo['CodSitTitulo'], $nfgProfile->getVoterRegistrationSit());
168
    }
169
170
    public function testNotMobilePhone()
171
    {
172
        $client = $this->getSoapClientMock(['NroFoneContato' => '5133333333']);
173
        $credentials = new Credentials('org', 'user', 'pass');
174
        $nfgSoap = new NfgSoap($client, $credentials);
175
176
        $nfgProfile = $nfgSoap->getUserInfo('stub');
177
178
        $this->assertNull($nfgProfile->getMobile());
179
    }
180
181
    public function testOldMobilePhone()
182
    {
183
        $client = $this->getSoapClientMock(['NroFoneContato' => '5188888888']);
184
        $credentials = new Credentials('org', 'user', 'pass');
185
        $nfgSoap = new NfgSoap($client, $credentials);
186
187
        $nfgProfile = $nfgSoap->getUserInfo('stub');
188
189
        $this->assertEquals('51988888888', $nfgProfile->getMobile()->getNationalNumber());
190
    }
191
192
    /**
193
     * @param array $info
194
     * @return \PHPUnit_Framework_MockObject_MockObject|\SoapClient
195
     */
196
    private function getSoapClientMock(array $info = [])
197
    {
198
        $client = $this->getMockBuilder(\SoapClient::class)
199
            ->setMethods(['ObterAccessID', 'ConsultaCadastro'])
200
            ->setConstructorArgs(['https://dum.my/service.wsdl'])
201
            ->disableOriginalConstructor()
202
            ->getMock();
203
        $client->expects($this->any())
204
            ->method('ObterAccessID')
205
            ->willReturnCallback(function ($data) {
206
                if ($data['organizacao'] === 'org'
207
                    && $data['usuario'] === 'user'
208
                    && $data['senha'] === 'pass'
209
                ) {
210
                    $response = '{"ObterAccessIDResult":"ok"}';
211
                } else {
212
                    $response = '{"ObterAccessIDResult":"error "}';
213
                }
214
215
                return json_decode($response);
216
            });
217
218
        $xml = $this->getUserInfoXmlResponse($info);
219
        $client->expects($this->any())
220
            ->method('ConsultaCadastro')
221
            ->willReturnCallback(function () use ($xml) {
222
                $response = new \stdClass();
223
                $response->ConsultaCadastroResult = $xml;
224
225
                return $response;
226
            });
227
228
        return $client;
229
    }
230
231
    /**
232
     * @param array $info expected keys are:
233
     *      CodSitRetorno
234
     *      CodNivelAcesso
235
     *      CodCpf
236
     *      NomeConsumidor
237
     *      DtNasc
238
     *      EmailPrinc
239
     *      NroFoneContato
240
     *      CodSitTitulo
241
     *      MsgRetorno
242
     * @return string
243
     */
244
    private function getUserInfoXmlResponse($info)
245
    {
246
        $default = [
247
            'CodSitRetorno' => '1',
248
            'CodNivelAcesso' => '2',
249
            'CodCpf' => '5745778407',
250
            'NomeConsumidor' => 'John Doe',
251
            'DtNasc' => '1970-01-01T00:00:00',
252
            'EmailPrinc' => '[email protected]',
253
            'NroFoneContato' => '51992345678',
254
            'CodSitTitulo' => '0',
255
            'MsgRetorno' => 'Sucesso.',
256
        ];
257
        $info = array_filter(
258
            array_merge($default, $info),
259
            function ($value) {
260
                return $value !== null;
261
            }
262
        );
263
264
        $xml = '<?xml version="1.0"?><LoginCidadaoServiceED xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
265
266
        foreach ($info as $key => $value) {
267
            $xml .= "<$key>$value</$key>";
268
        }
269
        $xml .= '</LoginCidadaoServiceED>';
270
271
        return $xml;
272
    }
273
}
274