|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of phpunit/php-code-coverage. |
| 4 | + * |
| 5 | + * (c) Sebastian Bergmann <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | +namespace SebastianBergmann\CodeCoverage\StaticAnalysis; |
| 11 | + |
| 12 | +use function range; |
| 13 | +use PHPUnit\Framework\Attributes\Ticket; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +abstract class FileAnalyserTestCase extends TestCase |
| 17 | +{ |
| 18 | + public function testGetLinesToBeIgnored(): void |
| 19 | + { |
| 20 | + $this->assertSame( |
| 21 | + [ |
| 22 | + 3, |
| 23 | + 4, |
| 24 | + 5, |
| 25 | + 11, |
| 26 | + 12, |
| 27 | + 13, |
| 28 | + 14, |
| 29 | + 15, |
| 30 | + 16, |
| 31 | + 18, |
| 32 | + 23, |
| 33 | + 24, |
| 34 | + 25, |
| 35 | + 30, |
| 36 | + 33, |
| 37 | + 38, |
| 38 | + 39, |
| 39 | + 40, |
| 40 | + 41, |
| 41 | + 42, |
| 42 | + ], |
| 43 | + $this->analyser()->ignoredLinesFor( |
| 44 | + TEST_FILES_PATH . 'source_with_ignore.php', |
| 45 | + ), |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public function testGetLinesToBeIgnored2(): void |
| 50 | + { |
| 51 | + $this->assertSame( |
| 52 | + [], |
| 53 | + $this->analyser()->ignoredLinesFor( |
| 54 | + TEST_FILES_PATH . 'source_without_ignore.php', |
| 55 | + ), |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public function testGetLinesToBeIgnored3(): void |
| 60 | + { |
| 61 | + $this->assertSame( |
| 62 | + [ |
| 63 | + 3, |
| 64 | + ], |
| 65 | + $this->analyser()->ignoredLinesFor( |
| 66 | + TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php', |
| 67 | + ), |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + #[Ticket('https://github.com/sebastianbergmann/php-code-coverage/issues/793')] |
| 72 | + #[Ticket('https://github.com/sebastianbergmann/php-code-coverage/issues/794')] |
| 73 | + public function testLineWithFullyQualifiedClassNameConstantIsNotIgnored(): void |
| 74 | + { |
| 75 | + $this->assertSame( |
| 76 | + [ |
| 77 | + 2, |
| 78 | + ], |
| 79 | + $this->analyser()->ignoredLinesFor( |
| 80 | + TEST_FILES_PATH . 'source_with_class_and_fqcn_constant.php', |
| 81 | + ), |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + public function testGetLinesToBeIgnoredOneLineAnnotations(): void |
| 86 | + { |
| 87 | + $this->assertSame( |
| 88 | + [ |
| 89 | + 4, |
| 90 | + 9, |
| 91 | + 29, |
| 92 | + 31, |
| 93 | + 32, |
| 94 | + 33, |
| 95 | + ], |
| 96 | + $this->analyser()->ignoredLinesFor( |
| 97 | + TEST_FILES_PATH . 'source_with_oneline_annotations.php', |
| 98 | + ), |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + public function testGetLinesToBeIgnoredWhenIgnoreIsDisabled(): void |
| 103 | + { |
| 104 | + $this->assertSame( |
| 105 | + [ |
| 106 | + 11, |
| 107 | + 18, |
| 108 | + 33, |
| 109 | + ], |
| 110 | + (new ParsingFileAnalyser(false, false))->ignoredLinesFor( |
| 111 | + TEST_FILES_PATH . 'source_with_ignore.php', |
| 112 | + ), |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + public function testGetLinesOfCodeForFileWithoutNewline(): void |
| 117 | + { |
| 118 | + $this->assertSame( |
| 119 | + 1, |
| 120 | + (new ParsingFileAnalyser(false, false))->linesOfCodeFor( |
| 121 | + TEST_FILES_PATH . 'source_without_newline.php', |
| 122 | + )->linesOfCode(), |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + #[Ticket('https://github.com/sebastianbergmann/php-code-coverage/issues/885')] |
| 127 | + public function testGetLinesOfCodeForFileCrLineEndings(): void |
| 128 | + { |
| 129 | + $result = (new ParsingFileAnalyser(false, false))->linesOfCodeFor( |
| 130 | + TEST_FILES_PATH . 'source_without_lf_only_cr.php', |
| 131 | + ); |
| 132 | + |
| 133 | + $this->assertSame(4, $result->linesOfCode()); |
| 134 | + $this->assertSame(2, $result->commentLinesOfCode()); |
| 135 | + $this->assertSame(2, $result->nonCommentLinesOfCode()); |
| 136 | + } |
| 137 | + |
| 138 | + public function testLinesCanBeIgnoredUsingAttribute(): void |
| 139 | + { |
| 140 | + $this->assertSame( |
| 141 | + [ |
| 142 | + 4, |
| 143 | + 5, |
| 144 | + 6, |
| 145 | + 7, |
| 146 | + 8, |
| 147 | + 9, |
| 148 | + 10, |
| 149 | + 11, |
| 150 | + 13, |
| 151 | + 15, |
| 152 | + 16, |
| 153 | + 17, |
| 154 | + 18, |
| 155 | + 19, |
| 156 | + ], |
| 157 | + $this->analyser()->ignoredLinesFor( |
| 158 | + TEST_FILES_PATH . 'source_with_ignore_attributes.php', |
| 159 | + ), |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + #[Ticket('https://github.com/sebastianbergmann/php-code-coverage/issues/1033')] |
| 164 | + public function testEnumWithEnumLevelIgnore(): void |
| 165 | + { |
| 166 | + $this->assertSame( |
| 167 | + range(5, 13), |
| 168 | + $this->analyser()->ignoredLinesFor( |
| 169 | + TEST_FILES_PATH . 'source_with_enum_and_enum_level_ignore_annotation.php', |
| 170 | + ), |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + #[Ticket('https://github.com/sebastianbergmann/php-code-coverage/issues/1033')] |
| 175 | + public function testEnumWithMethodLevelIgnore(): void |
| 176 | + { |
| 177 | + $this->assertSame( |
| 178 | + range(9, 12), |
| 179 | + $this->analyser()->ignoredLinesFor( |
| 180 | + TEST_FILES_PATH . 'source_with_enum_and_method_level_ignore_annotation.php', |
| 181 | + ), |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + public function testCodeUnitsAreFound(): void |
| 186 | + { |
| 187 | + $analyser = new ParsingFileAnalyser(true, true); |
| 188 | + $file = __DIR__ . '/../_files/source_with_interfaces_classes_traits_functions.php'; |
| 189 | + |
| 190 | + $interfaces = $analyser->interfacesIn($file); |
| 191 | + |
| 192 | + $this->assertCount(3, $interfaces); |
| 193 | + $this->assertArrayHasKey('SebastianBergmann\CodeCoverage\StaticAnalysis\A', $interfaces); |
| 194 | + $this->assertArrayHasKey('SebastianBergmann\CodeCoverage\StaticAnalysis\B', $interfaces); |
| 195 | + $this->assertArrayHasKey('SebastianBergmann\CodeCoverage\StaticAnalysis\C', $interfaces); |
| 196 | + |
| 197 | + $classes = $analyser->classesIn($file); |
| 198 | + |
| 199 | + $this->assertCount(2, $classes); |
| 200 | + $this->assertArrayHasKey('SebastianBergmann\CodeCoverage\StaticAnalysis\ParentClass', $classes); |
| 201 | + $this->assertArrayHasKey('SebastianBergmann\CodeCoverage\StaticAnalysis\ChildClass', $classes); |
| 202 | + |
| 203 | + $traits = $analyser->traitsIn($file); |
| 204 | + |
| 205 | + $this->assertCount(1, $traits); |
| 206 | + $this->assertArrayHasKey('SebastianBergmann\CodeCoverage\StaticAnalysis\T', $traits); |
| 207 | + |
| 208 | + $functions = $analyser->functionsIn($file); |
| 209 | + |
| 210 | + $this->assertCount(1, $functions); |
| 211 | + $this->assertArrayHasKey('SebastianBergmann\CodeCoverage\StaticAnalysis\f', $functions); |
| 212 | + } |
| 213 | + |
| 214 | + abstract protected function analyser(): FileAnalyser; |
| 215 | +} |
0 commit comments