Completed
Push — master ( 19e3bd...22f213 )
by Guilherme
17:20
created

NfgSoapTest::testNotMobilePhone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
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 PROCERGS\LoginCidadao\NfgBundle\Tests\Service;
12
13
use libphonenumber\PhoneNumberFormat;
14
use libphonenumber\PhoneNumberUtil;
15
use PROCERGS\LoginCidadao\NfgBundle\Security\Credentials;
16
use PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoap;
17
18
/**
19
 * @codeCoverageIgnore
20
 */
21
class NfgSoapTest extends \PHPUnit_Framework_TestCase
22
{
23
    public function testValidGetAccessID()
24
    {
25
        $client = $this->getSoapClientMock();
26
        $credentials = new Credentials('org', 'user', 'pass');
27
        $nfgSoap = new NfgSoap($client, $credentials);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getSoapClientMock() on line 25 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PROCERGS\LoginCidadao\Nf...\NfgSoap::__construct() does only seem to accept object<SoapClient>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
28
29
        $accessId = $nfgSoap->getAccessID();
30
        $this->assertEquals('ok', $accessId);
31
    }
32
33
    public function testInvalidCredentialsGetAccessID()
34
    {
35
        $this->setExpectedException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException');
36
37
        $client = $this->getSoapClientMock();
38
        $credentials = new Credentials('invalid', 'invalid', 'invalid');
39
        $nfgSoap = new NfgSoap($client, $credentials);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getSoapClientMock() on line 37 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PROCERGS\LoginCidadao\Nf...\NfgSoap::__construct() does only seem to accept object<SoapClient>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
40
        $nfgSoap->getAccessID();
41
    }
42
43
    public function testGetFullUserInfo()
44
    {
45
        $voterRegistration = '1234';
46
        $userInfo = [
47
            'CodCpf' => '5745778407',
48
            'NomeConsumidor' => 'John Doe',
49
            'DtNasc' => '1970-01-01T00:00:00',
50
            'EmailPrinc' => '[email protected]',
51
            'NroFoneContato' => '51992345678',
52
            'CodSitTitulo' => '1',
53
        ];
54
55
        $client = $this->getSoapClientMock($userInfo);
56
        $credentials = new Credentials('org', 'user', 'pass');
57
        $nfgSoap = new NfgSoap($client, $credentials);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getSoapClientMock($userInfo) on line 55 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PROCERGS\LoginCidadao\Nf...\NfgSoap::__construct() does only seem to accept object<SoapClient>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
58
59
        $nfgProfile = $nfgSoap->getUserInfo('stub', $voterRegistration);
60
61
        $phoneUtil = PhoneNumberUtil::getInstance();
62
        $this->assertEquals($userInfo['NomeConsumidor'], $nfgProfile->getName());
63
        $this->assertEquals($userInfo['EmailPrinc'], $nfgProfile->getEmail());
64
        $this->assertInstanceOf('\DateTime', $nfgProfile->getBirthdate());
65
        $this->assertEquals($userInfo['DtNasc'], $nfgProfile->getBirthdate()->format('Y-m-d\TH:i:s'));
66
        $this->assertNotNull($nfgProfile->getMobile());
67
        $this->assertEquals(
68
            "+55{$userInfo['NroFoneContato']}",
69
            $phoneUtil->format($nfgProfile->getMobile(), PhoneNumberFormat::E164)
70
        );
71
        $this->assertEquals($userInfo['CodSitTitulo'], $nfgProfile->getVoterRegistrationSit());
72
        $this->assertEquals($voterRegistration, $nfgProfile->getVoterRegistration());
73
    }
74
75
    public function testInvalidPhone()
76
    {
77
        $voterRegistration = '1234';
78
        $userInfo = [
79
            'CodCpf' => '5745778407',
80
            'NomeConsumidor' => 'John Doe',
81
            'DtNasc' => '1970-01-01T00:00:00',
82
            'EmailPrinc' => '[email protected]',
83
            'NroFoneContato' => 'aa',
84
            'CodSitTitulo' => '1',
85
        ];
86
87
        $client = $this->getSoapClientMock($userInfo);
88
        $credentials = new Credentials('org', 'user', 'pass');
89
        $nfgSoap = new NfgSoap($client, $credentials);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getSoapClientMock($userInfo) on line 87 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PROCERGS\LoginCidadao\Nf...\NfgSoap::__construct() does only seem to accept object<SoapClient>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
90
91
        $nfgProfile = $nfgSoap->getUserInfo('stub', $voterRegistration);
92
93
        $this->assertEquals($userInfo['NomeConsumidor'], $nfgProfile->getName());
94
        $this->assertEquals($userInfo['EmailPrinc'], $nfgProfile->getEmail());
95
        $this->assertInstanceOf('\DateTime', $nfgProfile->getBirthdate());
96
        $this->assertEquals($userInfo['DtNasc'], $nfgProfile->getBirthdate()->format('Y-m-d\TH:i:s'));
97
        $this->assertNull($nfgProfile->getMobile());
98
        $this->assertEquals($userInfo['CodSitTitulo'], $nfgProfile->getVoterRegistrationSit());
99
        $this->assertEquals($voterRegistration, $nfgProfile->getVoterRegistration());
100
    }
101
102
    public function testErrorResponse()
103
    {
104
        $this->setExpectedException('RuntimeException');
105
        $userInfo = [
106
            'CodSitRetorno' => '500',
107
            'MsgRetorno' => 'Some error',
108
        ];
109
110
        $client = $this->getSoapClientMock($userInfo);
111
        $credentials = new Credentials('org', 'user', 'pass');
112
        $nfgSoap = new NfgSoap($client, $credentials);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getSoapClientMock($userInfo) on line 110 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PROCERGS\LoginCidadao\Nf...\NfgSoap::__construct() does only seem to accept object<SoapClient>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
113
114
        $nfgSoap->getUserInfo('stub');
115
    }
116
117
    public function testPhoneMissing()
118
    {
119
        $voterRegistration = '1234';
120
        $userInfo = [
121
            'CodCpf' => '5745778407',
122
            'NomeConsumidor' => 'John Doe',
123
            'DtNasc' => '1970-01-01T00:00:00',
124
            'EmailPrinc' => '[email protected]',
125
            'NroFoneContato' => null,
126
            'CodSitTitulo' => '1',
127
        ];
128
129
        $client = $this->getSoapClientMock($userInfo);
130
        $credentials = new Credentials('org', 'user', 'pass');
131
        $nfgSoap = new NfgSoap($client, $credentials);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getSoapClientMock($userInfo) on line 129 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PROCERGS\LoginCidadao\Nf...\NfgSoap::__construct() does only seem to accept object<SoapClient>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
132
133
        $nfgProfile = $nfgSoap->getUserInfo('stub', $voterRegistration);
134
135
        $this->assertEquals($userInfo['NomeConsumidor'], $nfgProfile->getName());
136
        $this->assertEquals($userInfo['EmailPrinc'], $nfgProfile->getEmail());
137
        $this->assertInstanceOf('\DateTime', $nfgProfile->getBirthdate());
138
        $this->assertEquals($userInfo['DtNasc'], $nfgProfile->getBirthdate()->format('Y-m-d\TH:i:s'));
139
        $this->assertNull($nfgProfile->getMobile());
140
        $this->assertEquals($userInfo['CodSitTitulo'], $nfgProfile->getVoterRegistrationSit());
141
        $this->assertEquals($voterRegistration, $nfgProfile->getVoterRegistration());
142
    }
143
144
    public function testMinimalInfo()
145
    {
146
        $userInfo = [
147
            'CodCpf' => true,
148
            'NomeConsumidor' => null,
149
            'DtNasc' => null,
150
            'EmailPrinc' => null,
151
            'NroFoneContato' => null,
152
            'CodSitTitulo' => '0',
153
        ];
154
155
        $client = $this->getSoapClientMock($userInfo);
156
        $credentials = new Credentials('org', 'user', 'pass');
157
        $nfgSoap = new NfgSoap($client, $credentials);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getSoapClientMock($userInfo) on line 155 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PROCERGS\LoginCidadao\Nf...\NfgSoap::__construct() does only seem to accept object<SoapClient>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
158
159
        $nfgProfile = $nfgSoap->getUserInfo('stub');
160
161
        $this->assertNull($nfgProfile->getName());
162
        $this->assertNull($nfgProfile->getEmail());
163
        $this->assertNull($nfgProfile->getBirthdate());
164
        $this->assertNull($nfgProfile->getMobile());
165
        $this->assertEquals($userInfo['CodSitTitulo'], $nfgProfile->getVoterRegistrationSit());
166
    }
167
168
    public function testNotMobilePhone()
169
    {
170
        $client = $this->getSoapClientMock(['NroFoneContato' => '5133333333']);
171
        $credentials = new Credentials('org', 'user', 'pass');
172
        $nfgSoap = new NfgSoap($client, $credentials);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getSoapClientMock...tato' => '5133333333')) on line 170 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PROCERGS\LoginCidadao\Nf...\NfgSoap::__construct() does only seem to accept object<SoapClient>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
173
174
        $nfgProfile = $nfgSoap->getUserInfo('stub');
175
176
        $this->assertNull($nfgProfile->getMobile());
177
    }
178
179
    public function testOldMobilePhone()
180
    {
181
        $client = $this->getSoapClientMock(['NroFoneContato' => '5188888888']);
182
        $credentials = new Credentials('org', 'user', 'pass');
183
        $nfgSoap = new NfgSoap($client, $credentials);
0 ignored issues
show
Bug introduced by
It seems like $client defined by $this->getSoapClientMock...tato' => '5188888888')) on line 181 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, PROCERGS\LoginCidadao\Nf...\NfgSoap::__construct() does only seem to accept object<SoapClient>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
184
185
        $nfgProfile = $nfgSoap->getUserInfo('stub');
186
187
        $this->assertEquals('51988888888', $nfgProfile->getMobile()->getNationalNumber());
188
    }
189
190
    /**
191
     * @param array $info
192
     * @return \PHPUnit_Framework_MockObject_MockObject|\SoapClient
193
     */
194
    private function getSoapClientMock(array $info = [])
195
    {
196
        $client = $this->getMock(
197
            '\SoapClient',
198
            ['ObterAccessID', 'ConsultaCadastro'],
199
            ['https://dum.my/service.wsdl'],
200
            '',
201
            false,
202
            false,
203
            false,
204
            true,
205
            false
206
        );
207
        $client->expects($this->any())
208
            ->method('ObterAccessID')
209
            ->willReturnCallback(function ($data) {
210
                if ($data['organizacao'] === 'org'
211
                    && $data['usuario'] === 'user'
212
                    && $data['senha'] === 'pass'
213
                ) {
214
                    $response = '{"ObterAccessIDResult":"ok"}';
215
                } else {
216
                    $response = '{"ObterAccessIDResult":"error "}';
217
                }
218
219
                return json_decode($response);
220
            });
221
222
        $xml = $this->getUserInfoXmlResponse($info);
223
        $client->expects($this->any())
224
            ->method('ConsultaCadastro')
225
            ->willReturnCallback(function () use ($xml) {
226
                $response = new \stdClass();
227
                $response->ConsultaCadastroResult = $xml;
228
229
                return $response;
230
            });
231
232
        return $client;
233
    }
234
235
    /**
236
     * @param array $info expected keys are:
237
     *      CodSitRetorno
238
     *      CodNivelAcesso
239
     *      CodCpf
240
     *      NomeConsumidor
241
     *      DtNasc
242
     *      EmailPrinc
243
     *      NroFoneContato
244
     *      CodSitTitulo
245
     *      MsgRetorno
246
     * @return string
247
     */
248
    private function getUserInfoXmlResponse($info)
249
    {
250
        $default = [
251
            'CodSitRetorno' => '1',
252
            'CodNivelAcesso' => '2',
253
            'CodCpf' => '5745778407',
254
            'NomeConsumidor' => 'John Doe',
255
            'DtNasc' => '1970-01-01T00:00:00',
256
            'EmailPrinc' => '[email protected]',
257
            'NroFoneContato' => '51992345678',
258
            'CodSitTitulo' => '0',
259
            'MsgRetorno' => 'Sucesso.',
260
        ];
261
        $info = array_filter(
262
            array_merge($default, $info),
263
            function ($value) {
264
                return $value !== null;
265
            }
266
        );
267
268
        $xml = '<?xml version="1.0"?><LoginCidadaoServiceED xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
269
270
        foreach ($info as $key => $value) {
271
            $xml .= "<$key>$value</$key>";
272
        }
273
        $xml .= '</LoginCidadaoServiceED>';
274
275
        return $xml;
276
    }
277
}
278