|
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\Service; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\OAuthBundle\Entity\Client; |
|
14
|
|
|
use PROCERGS\LoginCidadao\AccountingBundle\Entity\ProcergsLink; |
|
15
|
|
|
use PROCERGS\LoginCidadao\AccountingBundle\Service\AccountingService; |
|
16
|
|
|
|
|
17
|
|
|
class AccountingServiceTest extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testGetAccounting() |
|
20
|
|
|
{ |
|
21
|
|
|
$client1 = (new Client())->setId(1); |
|
22
|
|
|
$client2 = (new Client())->setId(2); |
|
23
|
|
|
|
|
24
|
|
|
$clients = [ |
|
25
|
|
|
$client1, |
|
26
|
|
|
$client2, |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
$clientRepo = $this->getClientRepo(); |
|
30
|
|
|
$clientRepo->expects($this->once())->method('getAccessTokenAccounting')->willReturn([ |
|
31
|
|
|
['id' => 1, 'access_tokens' => 111], |
|
32
|
|
|
['id' => 2, 'access_tokens' => 111], |
|
33
|
|
|
]); |
|
34
|
|
|
$clientRepo->expects($this->once())->method('getActionLogAccounting')->willReturn([ |
|
35
|
|
|
['id' => 1, 'api_usage' => 222], |
|
36
|
|
|
['id' => 2, 'api_usage' => 222], |
|
37
|
|
|
]); |
|
38
|
|
|
$clientRepo->expects($this->once())->method('findBy') |
|
39
|
|
|
->willReturnCallback(function ($criteria) use ($clients) { |
|
40
|
|
|
$this->assertArrayHasKey('id', $criteria); |
|
41
|
|
|
$ids = $criteria['id']; |
|
42
|
|
|
$this->assertContains(1, $ids); |
|
43
|
|
|
$this->assertContains(2, $ids); |
|
44
|
|
|
$this->assertCount(2, $ids); |
|
45
|
|
|
|
|
46
|
|
|
return $clients; |
|
47
|
|
|
}); |
|
48
|
|
|
|
|
49
|
|
|
$registry = $this->getRegistry(); |
|
50
|
|
|
$registry->expects($this->once())->method('fetchLinked') |
|
51
|
|
|
->willReturn([]); |
|
52
|
|
|
|
|
53
|
|
|
$service = $this->getAccountingService($registry, $clientRepo); |
|
54
|
|
|
$report = $service->getAccounting(new \DateTime(), new \DateTime()); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertInstanceOf('PROCERGS\LoginCidadao\AccountingBundle\Model\AccountingReport', $report); |
|
57
|
|
|
$this->assertCount(2, $report->getReport()); |
|
58
|
|
|
foreach ($report->getReport() as $entry) { |
|
59
|
|
|
$this->assertContains($entry->getClient(), $clients); |
|
60
|
|
|
$this->assertEquals(333, $entry->getTotalUsage()); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testGetEmptyAccounting() |
|
65
|
|
|
{ |
|
66
|
|
|
$clientRepo = $this->getClientRepo(); |
|
67
|
|
|
$clientRepo->expects($this->once())->method('getAccessTokenAccounting')->willReturn([]); |
|
68
|
|
|
$clientRepo->expects($this->once())->method('getActionLogAccounting')->willReturn([]); |
|
69
|
|
|
$clientRepo->expects($this->once())->method('findBy')->willReturn([]); |
|
70
|
|
|
|
|
71
|
|
|
$registry = $this->getRegistry(); |
|
72
|
|
|
$registry->expects($this->once())->method('fetchLinked')->willReturn([]); |
|
73
|
|
|
|
|
74
|
|
|
$service = $this->getAccountingService($registry, $clientRepo); |
|
75
|
|
|
$report = $service->getAccounting(new \DateTime(), new \DateTime()); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertInstanceOf('PROCERGS\LoginCidadao\AccountingBundle\Model\AccountingReport', $report); |
|
78
|
|
|
$this->assertEmpty($report->getReport()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testGetGcsInterface() |
|
82
|
|
|
{ |
|
83
|
|
|
$interfaceName = 'My_Interface'; |
|
84
|
|
|
|
|
85
|
|
|
$client1 = (new Client())->setId(1); |
|
86
|
|
|
$client2 = (new Client())->setId(2); |
|
87
|
|
|
|
|
88
|
|
|
$clients = [ |
|
89
|
|
|
$client1, |
|
90
|
|
|
$client2, |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
$clientRepo = $this->getClientRepo(); |
|
94
|
|
|
$clientRepo->expects($this->once())->method('getAccessTokenAccounting')->willReturn([ |
|
95
|
|
|
['id' => 1, 'access_tokens' => 111], |
|
96
|
|
|
['id' => 2, 'access_tokens' => 111], |
|
97
|
|
|
]); |
|
98
|
|
|
$clientRepo->expects($this->once())->method('getActionLogAccounting')->willReturn([ |
|
99
|
|
|
['id' => 1, 'api_usage' => 222], |
|
100
|
|
|
['id' => 2, 'api_usage' => 222], |
|
101
|
|
|
]); |
|
102
|
|
|
$clientRepo->expects($this->once())->method('findBy')->willReturn($clients); |
|
103
|
|
|
|
|
104
|
|
|
$registry = $this->getRegistry(); |
|
105
|
|
|
$registry->expects($this->once())->method('fetchLinked') |
|
106
|
|
|
->willReturn([ |
|
107
|
|
|
$client1->getId() => (new ProcergsLink()) |
|
108
|
|
|
->setClient($client1) |
|
109
|
|
|
->setSystemType(ProcergsLink::TYPE_INTERNAL), |
|
110
|
|
|
]); |
|
111
|
|
|
|
|
112
|
|
|
$service = $this->getAccountingService($registry, $clientRepo); |
|
113
|
|
|
$report = $service->getGcsInterface($interfaceName, new \DateTime(), new \DateTime()); |
|
114
|
|
|
$lines = explode(PHP_EOL, $report); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertContains($interfaceName, $lines[0]); |
|
117
|
|
|
$this->assertEquals('9;2', end($lines)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function getAccountingService( |
|
121
|
|
|
$registry, |
|
122
|
|
|
$clientRepo, |
|
123
|
|
|
$linkRepo = null |
|
124
|
|
|
) { |
|
125
|
|
|
if (!$linkRepo) { |
|
126
|
|
|
$linkRepo = $this->getMockBuilder('PROCERGS\LoginCidadao\AccountingBundle\Entity\ProcergsLinkRepository') |
|
127
|
|
|
->disableOriginalConstructor()->getMock(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return new AccountingService($registry, $clientRepo, $linkRepo); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
private function getRegistry() |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
|
|
return $this->getMockBuilder('PROCERGS\LoginCidadao\AccountingBundle\Service\SystemsRegistryService') |
|
136
|
|
|
->disableOriginalConstructor()->getMock(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
private function getClientRepo() |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
return $this->getRepo('LoginCidadao\OAuthBundle\Entity\ClientRepository'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
private function getRepo($className) |
|
|
|
|
|
|
145
|
|
|
{ |
|
146
|
|
|
$repo = $this->getMockBuilder($className) |
|
147
|
|
|
->disableOriginalConstructor()->getMock(); |
|
148
|
|
|
|
|
149
|
|
|
return $repo; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.