|
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\AccountingBundle\Tests\Model; |
|
12
|
|
|
|
|
13
|
|
|
use PROCERGS\LoginCidadao\AccountingBundle\Entity\ProcergsLink; |
|
14
|
|
|
use PROCERGS\LoginCidadao\AccountingBundle\Model\AccountingReportEntry; |
|
15
|
|
|
use PROCERGS\LoginCidadao\AccountingBundle\Model\GcsInterface; |
|
16
|
|
|
|
|
17
|
|
|
class GcsInterfaceTest extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testGcsInterface() |
|
20
|
|
|
{ |
|
21
|
|
|
$today = (new \DateTime())->format('dmY'); |
|
22
|
|
|
$interfaceName = 'MY_INTERFACE'; |
|
23
|
|
|
$start = \DateTime::createFromFormat('Y-m-d', '2017-01-01'); |
|
24
|
|
|
$config = []; |
|
25
|
|
|
|
|
26
|
|
|
$clients = [ |
|
27
|
|
|
(new AccountingReportEntry()) |
|
28
|
|
|
->setSystemType(ProcergsLink::TYPE_INTERNAL) |
|
29
|
|
|
->setProcergsInitials(['XPTO']) |
|
30
|
|
|
->setAccessTokens(111) |
|
31
|
|
|
->setApiUsage(111) |
|
32
|
|
|
->setProcergsOwner(['SOME_OWNER']), |
|
33
|
|
|
(new AccountingReportEntry()) |
|
34
|
|
|
->setSystemType(ProcergsLink::TYPE_INTERNAL) |
|
35
|
|
|
->setProcergsInitials(['XPTO']) |
|
36
|
|
|
->setAccessTokens(222) |
|
37
|
|
|
->setApiUsage(222) |
|
38
|
|
|
->setProcergsOwner(['SOME_OWNER']), |
|
39
|
|
|
(new AccountingReportEntry()) |
|
40
|
|
|
->setSystemType(ProcergsLink::TYPE_EXTERNAL) |
|
41
|
|
|
->setAccessTokens(333) |
|
42
|
|
|
->setApiUsage(333), |
|
43
|
|
|
(new AccountingReportEntry()) |
|
44
|
|
|
->setSystemType(ProcergsLink::TYPE_INTERNAL) |
|
45
|
|
|
->setAccessTokens(444) |
|
46
|
|
|
->setApiUsage(444), |
|
47
|
|
|
(new AccountingReportEntry()) |
|
48
|
|
|
->setSystemType('invalid') |
|
49
|
|
|
->setAccessTokens(444) |
|
50
|
|
|
->setApiUsage(444), |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
$expectedBody = implode(PHP_EOL, [ |
|
54
|
|
|
'2;SOME_OWNER;XPTO;'.(111 + 111 + 222 + 222), |
|
55
|
|
|
'2;EXTERNAL;EXTERNAL;'.(333 + 333), |
|
56
|
|
|
'2;;;'.(444 + 444), |
|
57
|
|
|
'2;;;'.(444 + 444), |
|
58
|
|
|
]); |
|
59
|
|
|
|
|
60
|
|
|
$gcsInterface = new GcsInterface($interfaceName, $start, $config); |
|
|
|
|
|
|
61
|
|
|
foreach ($clients as $client) { |
|
62
|
|
|
$gcsInterface->addClient($client); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$header = "1;{$interfaceName};012017;{$today}"; |
|
66
|
|
|
$tail = "9;4"; |
|
67
|
|
|
|
|
68
|
|
|
$this->assertEquals($header, $gcsInterface->getHeader()); |
|
69
|
|
|
$this->assertEquals($expectedBody, $gcsInterface->getBody()); |
|
70
|
|
|
$this->assertEquals($tail, $gcsInterface->getTail()); |
|
71
|
|
|
$this->assertEquals($header.PHP_EOL.$expectedBody.PHP_EOL.$tail, $gcsInterface->__toString()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testConfig() |
|
75
|
|
|
{ |
|
76
|
|
|
$today = (new \DateTime())->format('dmY'); |
|
77
|
|
|
$interfaceName = 'MY_INTERFACE'; |
|
78
|
|
|
$start = \DateTime::createFromFormat('Y-m-d', '2017-01-01'); |
|
79
|
|
|
$config = [ |
|
80
|
|
|
'ignore_externals' => true, |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
$clients = [ |
|
84
|
|
|
(new AccountingReportEntry()) |
|
85
|
|
|
->setSystemType(ProcergsLink::TYPE_EXTERNAL) |
|
86
|
|
|
->setAccessTokens(333) |
|
87
|
|
|
->setApiUsage(333), |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
$gcsInterface = new GcsInterface($interfaceName, $start, $config); |
|
|
|
|
|
|
91
|
|
|
foreach ($clients as $client) { |
|
92
|
|
|
$gcsInterface->addClient($client); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$header = "1;{$interfaceName};012017;{$today}"; |
|
96
|
|
|
$body = ''; |
|
97
|
|
|
$tail = "9;0"; |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals($header, $gcsInterface->getHeader()); |
|
100
|
|
|
$this->assertEquals($body, $gcsInterface->getBody()); |
|
101
|
|
|
$this->assertEquals($tail, $gcsInterface->getTail()); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for type mismatches where the missing type is
false. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTimeobject or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalsebefore passing on the value to another function or method that may not be able to handle afalse.