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

GcsInterfaceTest::testGcsInterface()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 44
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 54
rs 9.6716

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 PROCERGS\LoginCidadao\AccountingBundle\Entity\ProcergsLink;
14
use PROCERGS\LoginCidadao\AccountingBundle\Model\AccountingReportEntry;
15
use PROCERGS\LoginCidadao\AccountingBundle\Model\GcsInterface;
16
17
class GcsInterfaceTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function testGcsInterface()
20
    {
21
        $today = (new \DateTime())->format('dmY');
22
        $interfaceName = 'MY_INTERFACE';
23
        $start = \DateTime::createFromFormat('Y-m-d', '2017-01-01');
24
        $config = [];
25
26
        $clients = [
27
            (new AccountingReportEntry())
28
                ->setSystemType(ProcergsLink::TYPE_INTERNAL)
29
                ->setProcergsInitials(['XPTO'])
30
                ->setAccessTokens(111)
31
                ->setApiUsage(111)
32
                ->setProcergsOwner(['SOME_OWNER']),
33
            (new AccountingReportEntry())
34
                ->setSystemType(ProcergsLink::TYPE_INTERNAL)
35
                ->setProcergsInitials(['XPTO'])
36
                ->setAccessTokens(222)
37
                ->setApiUsage(222)
38
                ->setProcergsOwner(['SOME_OWNER']),
39
            (new AccountingReportEntry())
40
                ->setSystemType(ProcergsLink::TYPE_EXTERNAL)
41
                ->setAccessTokens(333)
42
                ->setApiUsage(333),
43
            (new AccountingReportEntry())
44
                ->setSystemType(ProcergsLink::TYPE_INTERNAL)
45
                ->setAccessTokens(444)
46
                ->setApiUsage(444),
47
            (new AccountingReportEntry())
48
                ->setSystemType('invalid')
49
                ->setAccessTokens(444)
50
                ->setApiUsage(444),
51
        ];
52
53
        $expectedBody = implode(PHP_EOL, [
54
            '2;SOME_OWNER;XPTO;'.(111 + 111 + 222 + 222),
55
            '2;EXTERNAL;EXTERNAL;'.(333 + 333),
56
            '2;;;'.(444 + 444),
57
            '2;;;'.(444 + 444),
58
        ]);
59
60
        $gcsInterface = new GcsInterface($interfaceName, $start, $config);
0 ignored issues
show
Security Bug introduced by
It seems like $start defined by \DateTime::createFromFor...('Y-m-d', '2017-01-01') on line 23 can also be of type false; however, PROCERGS\LoginCidadao\Ac...nterface::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
61
        foreach ($clients as $client) {
62
            $gcsInterface->addClient($client);
63
        }
64
65
        $header = "1;{$interfaceName};012017;{$today}";
66
        $tail = "9;4";
67
68
        $this->assertEquals($header, $gcsInterface->getHeader());
69
        $this->assertEquals($expectedBody, $gcsInterface->getBody());
70
        $this->assertEquals($tail, $gcsInterface->getTail());
71
        $this->assertEquals($header.PHP_EOL.$expectedBody.PHP_EOL.$tail, $gcsInterface->__toString());
72
    }
73
74
    public function testConfig()
75
    {
76
        $today = (new \DateTime())->format('dmY');
77
        $interfaceName = 'MY_INTERFACE';
78
        $start = \DateTime::createFromFormat('Y-m-d', '2017-01-01');
79
        $config = [
80
            'ignore_externals' => true,
81
        ];
82
83
        $clients = [
84
            (new AccountingReportEntry())
85
                ->setSystemType(ProcergsLink::TYPE_EXTERNAL)
86
                ->setAccessTokens(333)
87
                ->setApiUsage(333),
88
        ];
89
90
        $gcsInterface = new GcsInterface($interfaceName, $start, $config);
0 ignored issues
show
Security Bug introduced by
It seems like $start defined by \DateTime::createFromFor...('Y-m-d', '2017-01-01') on line 78 can also be of type false; however, PROCERGS\LoginCidadao\Ac...nterface::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
91
        foreach ($clients as $client) {
92
            $gcsInterface->addClient($client);
93
        }
94
95
        $header = "1;{$interfaceName};012017;{$today}";
96
        $body = '';
97
        $tail = "9;0";
98
99
        $this->assertEquals($header, $gcsInterface->getHeader());
100
        $this->assertEquals($body, $gcsInterface->getBody());
101
        $this->assertEquals($tail, $gcsInterface->getTail());
102
    }
103
}
104