File: CsvTest.php

package info (click to toggle)
phploc 7.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 556 kB
  • sloc: php: 2,383; xml: 121; makefile: 22
file content (155 lines) | stat: -rw-r--r-- 5,067 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php declare(strict_types=1);
/*
 * This file is part of PHPLOC.
 *
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace SebastianBergmann\PHPLOC;

use function count;
use function explode;
use function ob_end_clean;
use function ob_get_clean;
use function ob_start;
use function trim;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

/**
 * @covers \SebastianBergmann\PHPLOC\Log\Csv
 */
final class CsvTest extends TestCase
{
    /**
     * @var \SebastianBergmann\PHPLOC\Log\Csv
     */
    private $single;

    private $sample_row = [
        'directories'                 => 1,
        'files'                       => 2,
        'loc'                         => 3,
        'ccnByLloc'                   => 4,
        'cloc'                        => 5,
        'ncloc'                       => 6,
        'lloc'                        => 7,
        'llocGlobal'                  => 8,
        'namespaces'                  => 9,
        'interfaces'                  => 10,
        'traits'                      => 11,
        'classes'                     => 12,
        'abstractClasses'             => 13,
        'concreteClasses'             => 14,
        'finalClasses'                => 8,
        'nonFinalClasses'             => 6,
        'llocClasses'                 => 15,
        'methods'                     => 16,
        'nonStaticMethods'            => 17,
        'staticMethods'               => 18,
        'publicMethods'               => 19,
        'nonPublicMethods'            => 20,
        'protectedMethods'            => 14,
        'privateMethods'              => 6,
        'methodCcnAvg'                => 21,
        'functions'                   => 22,
        'namedFunctions'              => 23,
        'anonymousFunctions'          => 24,
        'llocFunctions'               => 25,
        'llocByNof'                   => 26,
        'constants'                   => 27,
        'globalConstants'             => 28,
        'classConstants'              => 29,
        'publicClassConstants'        => 15,
        'nonPublicClassConstants'     => 14,
        'attributeAccesses'           => 30,
        'instanceAttributeAccesses'   => 31,
        'staticAttributeAccesses'     => 32,
        'methodCalls'                 => 33,
        'instanceMethodCalls'         => 34,
        'staticMethodCalls'           => 35,
        'globalAccesses'              => 36,
        'globalVariableAccesses'      => 37,
        'superGlobalVariableAccesses' => 38,
        'globalConstantAccesses'      => 39,
        'testClasses'                 => 40,
        'testMethods'                 => 41,
        'classCcnAvg'                 => 42,
        'classLlocAvg'                => 43,
        'methodLlocAvg'               => 44,
        'averageMethodsPerClass'      => 5,
    ];

    protected function setUp(): void
    {
        $this->single = new \SebastianBergmann\PHPLOC\Log\Csv;
    }

    public function testPrintedResultContainsHeadings(): void
    {
        ob_start();

        $this->single->printResult('php://output', $this->sample_row);
        $output = ob_get_clean();

        $this->assertMatchesRegularExpression('#Directories,Files.+$#is', $output, 'Printed result does not contain a heading line');
    }

    public function testPrintedResultContainsData(): void
    {
        ob_start();

        $this->single->printResult('php://output', $this->sample_row);
        $output = ob_get_clean();

        $this->assertMatchesRegularExpression('#"1","2".+$#is', $output, 'Printed result does not contain a value line');
    }

    public function testPrintedResultContainsEqualNumHeadingsAndValues(): void
    {
        ob_start();

        $this->single->printResult('php://output', $this->sample_row);
        $output = ob_get_clean();

        $rows     = explode("\n", $output);
        $headings = explode(',', $rows[0]);
        $vals     = explode(',', $rows[1]);

        $this->assertEquals(
            count($headings),
            count($vals),
            'Printed result does not contain same number of headings and values'
        );
    }

    public function testExactlyTwoRowsArePrinted(): void
    {
        ob_start();

        $this->single->printResult('php://output', $this->sample_row);
        $output = ob_get_clean();

        $rows = explode("\n", trim($output));
        $this->assertEquals(2, count($rows), 'Printed result contained more or less than expected 2 rows');
    }

    public function testPrintPartialRow(): void
    {
        $this->expectException(InvalidArgumentException::class);

        $count = $this->sample_row;
        unset($count['llocByNof']);

        try {
            ob_start();
            $this->single->printResult('php://output', $count);
        } finally {
            ob_end_clean();
        }

        $this->fail('No exception was raised for malformed input var');
    }
}