Failed Conditions
Pull Request — master (#790)
by Guilherme
05:12
created

AccountingServiceTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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