Completed
Push — master ( 24f6ce...c3efa2 )
by Guilherme
05:36
created

AccountingReportTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 108
rs 10
wmc 9
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddEntryNotLinked() 0 15 1
A testAddEntryLinked() 0 11 1
A testSorting() 0 15 1
A testSortingIgnoringInactive() 0 17 1
A testInvalidOrder() 0 7 1
A doTestSorting() 0 19 3
A getAccountingReport() 0 15 1
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
    private function doTestSorting(AccountingReport $report, array $options = [])
93
    {
94
        $options['sort'] = AccountingReport::SORT_ORDER_ASC;
95
        $asc = $report->getReport($options);
96
97
        $lastUsage = -1;
98
        foreach ($asc as $entry) {
99
            $this->assertGreaterThanOrEqual($lastUsage, $entry->getTotalUsage());
100
            $lastUsage = $entry->getTotalUsage();
101
        }
102
103
        $options['sort'] = AccountingReport::SORT_ORDER_DESC;
104
        $desc = $report->getReport($options);
105
        $lastUsage = 9999;
106
        foreach ($desc as $entry) {
107
            $this->assertLessThanOrEqual($lastUsage, $entry->getTotalUsage());
108
            $lastUsage = $entry->getTotalUsage();
109
        }
110
    }
111
112
    private function getAccountingReport()
113
    {
114
        $registryClass = 'PROCERGS\LoginCidadao\AccountingBundle\Service\SystemsRegistryService';
115
        $registry = $this->getMockBuilder($registryClass)->disableOriginalConstructor()->getMock();
116
        $registry->expects($this->any())->method('getSystemInitials')->willReturn(['XPTO']);
117
        $registry->expects($this->any())->method('getSystemOwners')->willReturn(['OWNER']);
118
119
        $linked = [
120
            321 => (new ProcergsLink())
121
                ->setSystemType(ProcergsLink::TYPE_EXTERNAL)
122
                ->setClient((new Client())->setId(321)),
123
        ];
124
125
        return new AccountingReport($registry, $linked);
126
    }
127
}
128