Failed Conditions
Push — issue#752 ( ae1705...8960ee )
by Guilherme
08:24
created

AccountingReportTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 142
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

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