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

AccountingReportTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 11
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 LoginCidadao\OAuthBundle\Entity\Client;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use PHPUnit\Framework\TestCase;
16
use PROCERGS\LoginCidadao\AccountingBundle\Entity\ProcergsLink;
17
use PROCERGS\LoginCidadao\AccountingBundle\Model\AccountingReport;
18
use PROCERGS\LoginCidadao\AccountingBundle\Service\SystemsRegistryService;
19
20
/**
21
 * @codeCoverageIgnore
22
 */
23
class AccountingReportTest extends TestCase
24
{
25
    public function testAddEntryNotLinked()
26
    {
27
        $client = (new Client())->setId(123);
28
29
        $report = $this->getAccountingReport();
30
        $report->addEntry($client, 111);
31
        $report->addEntry($client, null, 222);
32
        $entry = $report->getReport()[$client->getId()];
33
34
        $this->assertEquals($client, $entry->getClient());
35
        $this->assertEquals(333, $entry->getTotalUsage());
36
        $this->assertEquals(ProcergsLink::TYPE_INTERNAL, $entry->getSystemType());
37
        $this->assertContains('XPTO', $entry->getProcergsInitials());
38
        $this->assertContains('OWNER', $entry->getProcergsOwner());
39
    }
40
41
    public function testAddEntryLinked()
42
    {
43
        $client = (new Client())->setId(321);
44
45
        $report = $this->getAccountingReport();
46
        $report->addEntry($client, 111);
47
        $report->addEntry($client, null, 222);
48
        $entry = $report->getReport()[$client->getId()];
49
50
        $this->assertEquals(ProcergsLink::TYPE_EXTERNAL, $entry->getSystemType());
51
    }
52
53
    public function testSorting()
54
    {
55
        $client1 = (new Client())->setId(123);
56
        $client2 = (new Client())->setId(456);
57
        $client3 = (new Client())->setId(789);
58
        $client4 = (new Client())->setId(987);
59
60
        $report = $this->getAccountingReport();
61
        $report->addEntry($client1, 111);
62
        $report->addEntry($client2);
63
        $report->addEntry($client3, null, 222);
64
        $report->addEntry($client4, null, 222);
65
66
        $this->doTestSorting($report);
67
    }
68
69
    public function testSortingIgnoringInactive()
70
    {
71
        $client1 = (new Client())->setId(123);
72
        $client2 = (new Client())->setId(456);
73
        $client3 = (new Client())->setId(789);
74
75
        $report = $this->getAccountingReport();
76
        $report->addEntry($client1, 111);
77
        $report->addEntry($client2);
78
        $report->addEntry($client3, null, 222);
79
80
        $options = ['include_inactive' => false];
81
        $this->doTestSorting($report, $options);
82
83
        $entries = $report->getReport($options);
84
        $this->assertArrayNotHasKey($client2->getId(), $entries);
85
    }
86
87
    public function testInvalidOrder()
88
    {
89
        $this->expectException(\InvalidArgumentException::class);
90
91
        $report = $this->getAccountingReport();
92
        $report->getReport(['sort' => 'INVALID']);
93
    }
94
95
    public function testLazyLoadInitials()
96
    {
97
        $getInitialsCalls = 0;
98
        $getOwnerCalls = 0;
99
100
        $registry = $this->getMockBuilder(SystemsRegistryService::class)->disableOriginalConstructor()->getMock();
101
        $registry->expects($this->exactly(2))->method('getSystemInitials')
102
            ->willReturnCallback(function () use (&$getInitialsCalls) {
103
                $getInitialsCalls++;
104
105
                return ['XPTO'];
106
            });
107
        $registry->expects($this->exactly(2))->method('getSystemOwners')
108
            ->willReturnCallback(function () use (&$getOwnerCalls) {
109
                $getOwnerCalls++;
110
111
                return ['OWNER'];
112
            });
113
114
        $client1 = (new Client())->setId(123);
115
        $client2 = (new Client())->setId(456);
116
        $client3 = (new Client())->setId(789);
117
118
        $report = $this->getAccountingReport($registry);
119
        $report->addEntry($client1, 10, 20, true);
120
        $report->addEntry($client2, 30, 40, true);
121
        $report->addEntry($client3, 0, 0, true);
122
123
        $report->getReport(['include_inactive' => false]);
124
        $this->assertEquals(2, $getInitialsCalls);
125
        $this->assertEquals(2, $getOwnerCalls);
126
    }
127
128
    private function doTestSorting(AccountingReport $report, array $options = [])
129
    {
130
        $options['sort'] = AccountingReport::SORT_ORDER_ASC;
131
        $asc = $report->getReport($options);
132
133
        $lastUsage = -1;
134
        foreach ($asc as $entry) {
135
            $this->assertGreaterThanOrEqual($lastUsage, $entry->getTotalUsage());
136
            $lastUsage = $entry->getTotalUsage();
137
        }
138
139
        $options['sort'] = AccountingReport::SORT_ORDER_DESC;
140
        $desc = $report->getReport($options);
141
        $lastUsage = 9999;
142
        foreach ($desc as $entry) {
143
            $this->assertLessThanOrEqual($lastUsage, $entry->getTotalUsage());
144
            $lastUsage = $entry->getTotalUsage();
145
        }
146
    }
147
148
    private function getAccountingReport($registry = null)
149
    {
150
        if ($registry === null) {
151
            /** @var SystemsRegistryService|MockObject $registry */
152
            $registry = $this->getMockBuilder(SystemsRegistryService::class)->disableOriginalConstructor()->getMock();
153
            $registry->expects($this->any())->method('getSystemInitials')->willReturn(['XPTO']);
154
            $registry->expects($this->any())->method('getSystemOwners')->willReturn(['OWNER']);
155
        }
156
157
        $linked = [
158
            321 => (new ProcergsLink())
159
                ->setSystemType(ProcergsLink::TYPE_EXTERNAL)
160
                ->setClient((new Client())->setId(321)),
161
        ];
162
163
        return new AccountingReport($registry, $linked, new \DateTime());
164
    }
165
}
166