1<?php
2/*
3 * This file is part of the php-code-coverage package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace SebastianBergmann\CodeCoverage\Report;
12
13use SebastianBergmann\CodeCoverage\TestCase;
14use SebastianBergmann\CodeCoverage\Node\Builder;
15
16class BuilderTest extends TestCase
17{
18    protected $factory;
19
20    protected function setUp()
21    {
22        $this->factory = new Builder;
23    }
24
25    public function testSomething()
26    {
27        $root = $this->getCoverageForBankAccount()->getReport();
28
29        $expectedPath = rtrim(TEST_FILES_PATH, DIRECTORY_SEPARATOR);
30        $this->assertEquals($expectedPath, $root->getName());
31        $this->assertEquals($expectedPath, $root->getPath());
32        $this->assertEquals(10, $root->getNumExecutableLines());
33        $this->assertEquals(5, $root->getNumExecutedLines());
34        $this->assertEquals(1, $root->getNumClasses());
35        $this->assertEquals(0, $root->getNumTestedClasses());
36        $this->assertEquals(4, $root->getNumMethods());
37        $this->assertEquals(3, $root->getNumTestedMethods());
38        $this->assertEquals('0.00%', $root->getTestedClassesPercent());
39        $this->assertEquals('75.00%', $root->getTestedMethodsPercent());
40        $this->assertEquals('50.00%', $root->getLineExecutedPercent());
41        $this->assertEquals(0, $root->getNumFunctions());
42        $this->assertEquals(0, $root->getNumTestedFunctions());
43        $this->assertNull($root->getParent());
44        $this->assertEquals([], $root->getDirectories());
45        #$this->assertEquals(array(), $root->getFiles());
46        #$this->assertEquals(array(), $root->getChildNodes());
47
48        $this->assertEquals(
49            [
50                'BankAccount' => [
51                    'methods' => [
52                        'getBalance' => [
53                            'signature'       => 'getBalance()',
54                            'startLine'       => 6,
55                            'endLine'         => 9,
56                            'executableLines' => 1,
57                            'executedLines'   => 1,
58                            'ccn'             => 1,
59                            'coverage'        => 100,
60                            'crap'            => '1',
61                            'link'            => 'BankAccount.php.html#6',
62                            'methodName'      => 'getBalance',
63                            'visibility'      => 'public',
64                        ],
65                        'setBalance' => [
66                            'signature'       => 'setBalance($balance)',
67                            'startLine'       => 11,
68                            'endLine'         => 18,
69                            'executableLines' => 5,
70                            'executedLines'   => 0,
71                            'ccn'             => 2,
72                            'coverage'        => 0,
73                            'crap'            => 6,
74                            'link'            => 'BankAccount.php.html#11',
75                            'methodName'      => 'setBalance',
76                            'visibility'      => 'protected',
77                        ],
78                        'depositMoney' => [
79                            'signature'       => 'depositMoney($balance)',
80                            'startLine'       => 20,
81                            'endLine'         => 25,
82                            'executableLines' => 2,
83                            'executedLines'   => 2,
84                            'ccn'             => 1,
85                            'coverage'        => 100,
86                            'crap'            => '1',
87                            'link'            => 'BankAccount.php.html#20',
88                            'methodName'      => 'depositMoney',
89                            'visibility'      => 'public',
90                        ],
91                        'withdrawMoney' => [
92                            'signature'       => 'withdrawMoney($balance)',
93                            'startLine'       => 27,
94                            'endLine'         => 32,
95                            'executableLines' => 2,
96                            'executedLines'   => 2,
97                            'ccn'             => 1,
98                            'coverage'        => 100,
99                            'crap'            => '1',
100                            'link'            => 'BankAccount.php.html#27',
101                            'methodName'      => 'withdrawMoney',
102                            'visibility'      => 'public',
103                        ],
104                    ],
105                    'startLine'       => 2,
106                    'executableLines' => 10,
107                    'executedLines'   => 5,
108                    'ccn'             => 5,
109                    'coverage'        => 50,
110                    'crap'            => '8.12',
111                    'package'         => [
112                        'namespace'   => '',
113                        'fullPackage' => '',
114                        'category'    => '',
115                        'package'     => '',
116                        'subpackage'  => ''
117                    ],
118                    'link'      => 'BankAccount.php.html#2',
119                    'className' => 'BankAccount'
120                ]
121            ],
122            $root->getClasses()
123        );
124
125        $this->assertEquals([], $root->getFunctions());
126    }
127
128    public function testBuildDirectoryStructure()
129    {
130        $method = new \ReflectionMethod(
131            Builder::class,
132            'buildDirectoryStructure'
133        );
134
135        $method->setAccessible(true);
136
137        $this->assertEquals(
138            [
139                'src' => [
140                    'Money.php/f'    => [],
141                    'MoneyBag.php/f' => []
142                ]
143            ],
144            $method->invoke(
145                $this->factory,
146                ['src/Money.php' => [], 'src/MoneyBag.php' => []]
147            )
148        );
149    }
150
151    /**
152     * @dataProvider reducePathsProvider
153     */
154    public function testReducePaths($reducedPaths, $commonPath, $paths)
155    {
156        $method = new \ReflectionMethod(
157            Builder::class,
158            'reducePaths'
159        );
160
161        $method->setAccessible(true);
162
163        $_commonPath = $method->invokeArgs($this->factory, [&$paths]);
164
165        $this->assertEquals($reducedPaths, $paths);
166        $this->assertEquals($commonPath, $_commonPath);
167    }
168
169    public function reducePathsProvider()
170    {
171        return [
172            [
173                [
174                    'Money.php'    => [],
175                    'MoneyBag.php' => []
176                ],
177                '/home/sb/Money',
178                [
179                    '/home/sb/Money/Money.php'    => [],
180                    '/home/sb/Money/MoneyBag.php' => []
181                ]
182            ],
183            [
184                [
185                    'Money.php' => []
186                ],
187                '/home/sb/Money/',
188                [
189                    '/home/sb/Money/Money.php' => []
190                ]
191            ],
192            [
193                [],
194                '.',
195                []
196            ],
197            [
198                [
199                    'Money.php'          => [],
200                    'MoneyBag.php'       => [],
201                    'Cash.phar/Cash.php' => [],
202                ],
203                '/home/sb/Money',
204                [
205                    '/home/sb/Money/Money.php'                 => [],
206                    '/home/sb/Money/MoneyBag.php'              => [],
207                    'phar:///home/sb/Money/Cash.phar/Cash.php' => [],
208                ],
209            ],
210        ];
211    }
212}
213