doyolabs /
code-coverage
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the doyo/code-coverage project. |
||
| 5 | * |
||
| 6 | * (c) Anthonius Munthi <https://itstoni.com> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | declare(strict_types=1); |
||
| 13 | |||
| 14 | namespace Doyo\Bridge\CodeCoverage; |
||
| 15 | |||
| 16 | use SebastianBergmann\CodeCoverage\CodeCoverage; |
||
|
0 ignored issues
–
show
|
|||
| 17 | use SebastianBergmann\CodeCoverage\Driver\Driver; |
||
| 18 | use SebastianBergmann\CodeCoverage\Filter; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Provide bridge to PHP Code Coverage. |
||
| 22 | */ |
||
| 23 | class Processor implements ProcessorInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var CodeCoverage |
||
| 27 | */ |
||
| 28 | private $codeCoverage; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var TestCase[] |
||
| 32 | */ |
||
| 33 | private $testCases = []; |
||
| 34 | |||
| 35 | private $completed = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Driver |
||
| 39 | */ |
||
| 40 | private $driver; |
||
|
0 ignored issues
–
show
|
|||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Filter |
||
| 44 | */ |
||
| 45 | private $filter; |
||
|
0 ignored issues
–
show
|
|||
| 46 | |||
| 47 | /** |
||
| 48 | * @var TestCase |
||
| 49 | */ |
||
| 50 | private $currentTestCase; |
||
| 51 | |||
| 52 | 22 | public function __construct(CodeCoverage $codeCoverage) |
|
| 53 | { |
||
| 54 | 22 | $this->codeCoverage = $codeCoverage; |
|
| 55 | } |
||
| 56 | |||
| 57 | 7 | public function setCurrentTestCase(TestCase $testCase) |
|
| 58 | { |
||
| 59 | 7 | $this->currentTestCase = $testCase; |
|
| 60 | } |
||
| 61 | |||
| 62 | 116 | public function getCurrentTestCase() |
|
| 63 | { |
||
| 64 | 116 | return $this->currentTestCase; |
|
| 65 | } |
||
| 66 | |||
| 67 | 6 | public function start(TestCase $testCase, $clear = false) |
|
| 68 | { |
||
| 69 | 6 | $this->setCurrentTestCase($testCase); |
|
| 70 | 6 | $this->addTestCase($testCase); |
|
| 71 | 5 | $this->codeCoverage->start($testCase->getName(), $clear); |
|
| 72 | } |
||
| 73 | |||
| 74 | 116 | public function stop(bool $append = true, $linesToBeCovered = [], array $linesToBeUsed = [], bool $ignoreForceCoversAnnotation = false): array |
|
| 75 | { |
||
| 76 | 116 | return $this->codeCoverage->stop($append, $linesToBeCovered, $linesToBeUsed, $ignoreForceCoversAnnotation); |
|
| 77 | } |
||
| 78 | |||
| 79 | 1 | public function merge($processor) |
|
| 80 | { |
||
| 81 | 1 | $codeCoverage = $processor; |
|
| 82 | 1 | if ($processor instanceof self) { |
|
| 83 | 1 | $codeCoverage = $processor->getCodeCoverage(); |
|
| 84 | } |
||
| 85 | 1 | $this->getCodeCoverage()->merge($codeCoverage); |
|
| 86 | } |
||
| 87 | |||
| 88 | 3 | public function clear() |
|
| 89 | { |
||
| 90 | 3 | $this->codeCoverage->clear(); |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return CodeCoverage |
||
| 95 | */ |
||
| 96 | 4 | public function getCodeCoverage() |
|
| 97 | { |
||
| 98 | 4 | return $this->codeCoverage; |
|
| 99 | } |
||
| 100 | |||
| 101 | 7 | public function addTestCase(TestCase $testCase) |
|
| 102 | { |
||
| 103 | 7 | $this->testCases[$testCase->getName()] = $testCase; |
|
| 104 | } |
||
| 105 | |||
| 106 | 2 | public function complete() |
|
| 107 | { |
||
| 108 | 2 | $coverage = $this->codeCoverage; |
|
| 109 | 2 | $testCases = $this->testCases; |
|
| 110 | 2 | $tests = $coverage->getTests(); |
|
| 111 | |||
| 112 | 2 | foreach ($testCases as $testCase) { |
|
| 113 | 2 | $name = $testCase->getName(); |
|
| 114 | 2 | $tests[$name]['status'] = $testCase->getResult(); |
|
| 115 | } |
||
| 116 | |||
| 117 | 2 | $coverage->setTests($tests); |
|
| 118 | 2 | $this->completed = true; |
|
| 119 | } |
||
| 120 | } |
||
| 121 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: