From 88a580df977807409a3737d8ff8336df8b585c73 Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Tue, 12 May 2020 02:05:51 +0100 Subject: [PATCH 1/2] Improve consistency in handling of namespaces --- src/Node/File.php | 4 ++++ src/Report/Text.php | 12 +++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Node/File.php b/src/Node/File.php index 5268edc5a..e6e87ba53 100644 --- a/src/Node/File.php +++ b/src/Node/File.php @@ -518,6 +518,10 @@ private function processTraits(\PHP_Token_Stream $tokens): void $link = $this->getId() . '.html#'; foreach ($traits as $traitName => $trait) { + if (!empty($trait['package']['namespace'])) { + $traitName = $trait['package']['namespace'] . '\\' . $traitName; + } + $this->traits[$traitName] = [ 'traitName' => $traitName, 'methods' => [], diff --git a/src/Report/Text.php b/src/Report/Text.php index 1cbbe2ab5..413a3f32d 100644 --- a/src/Report/Text.php +++ b/src/Report/Text.php @@ -200,16 +200,14 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin } } - $namespace = ''; + $package = ''; - if (!empty($class['package']['namespace'])) { - $namespace = '\\' . $class['package']['namespace'] . '::'; - } elseif (!empty($class['package']['fullPackage'])) { - $namespace = '@' . $class['package']['fullPackage'] . '::'; + if (!empty($class['package']['fullPackage'])) { + $package = '@' . $class['package']['fullPackage'] . '::'; } - $classCoverage[$namespace . $className] = [ - 'namespace' => $namespace, + $classCoverage[$package . $className] = [ + 'namespace' => $class['package']['namespace'], 'className ' => $className, 'methodsCovered' => $coveredMethods, 'methodCount' => $classMethods, From edab3617a567ab2e60073218a1782951e4c38237 Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Mon, 18 May 2020 20:49:24 +0100 Subject: [PATCH 2/2] Add additional tests for text report --- tests/TestCase.php | 118 ++++++++++++++++++++ tests/_files/BankAccount-text-summary.txt | 7 ++ tests/_files/NamespacedBankAccount-text.txt | 14 +++ tests/_files/NamespacedBankAccount.php | 39 +++++++ tests/tests/FilterTest.php | 1 + tests/tests/TextTest.php | 20 ++++ 6 files changed, 199 insertions(+) create mode 100644 tests/_files/BankAccount-text-summary.txt create mode 100644 tests/_files/NamespacedBankAccount-text.txt create mode 100644 tests/_files/NamespacedBankAccount.php diff --git a/tests/TestCase.php b/tests/TestCase.php index c6c68df59..34300abea 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -138,6 +138,124 @@ protected function getCoverageForBankAccount(): CodeCoverage return $coverage; } + protected function getXdebugDataForNamespacedBankAccount() + { + return [ + RawCodeCoverageData::fromXdebugWithoutPathCoverage([ + TEST_FILES_PATH . 'NamespacedBankAccount.php' => [ + 14 => 1, + 15 => -2, + 19 => -1, + 20 => -1, + 21 => -1, + 22 => -1, + 24 => -1, + 28 => -1, + 30 => -1, + 31 => -2, + 35 => -1, + 37 => -1, + 38 => -2, + ], + ]), + RawCodeCoverageData::fromXdebugWithoutPathCoverage([ + TEST_FILES_PATH . 'NamespacedBankAccount.php' => [ + 14 => 1, + 19 => 1, + 22 => 1, + 35 => 1, + ], + ]), + RawCodeCoverageData::fromXdebugWithoutPathCoverage([ + TEST_FILES_PATH . 'NamespacedBankAccount.php' => [ + 14 => 1, + 19 => 1, + 22 => 1, + 28 => 1, + ], + ]), + RawCodeCoverageData::fromXdebugWithoutPathCoverage([ + TEST_FILES_PATH . 'NamespacedBankAccount.php' => [ + 14 => 1, + 19 => 1, + 20 => 1, + 21 => 1, + 24 => 1, + 28 => 1, + 30 => 1, + 35 => 1, + 37 => 1, + ], + ]), + ]; + } + + protected function getCoverageForNamespacedBankAccount(): CodeCoverage + { + $data = $this->getXdebugDataForNamespacedBankAccount(); + + $stub = $this->createMock(Driver::class); + + $stub->expects($this->any()) + ->method('stop') + ->will($this->onConsecutiveCalls( + $data[0], + $data[1], + $data[2], + $data[3] + )); + + $filter = new Filter; + $filter->addFileToWhitelist(TEST_FILES_PATH . 'NamespacedBankAccount.php'); + + $coverage = new CodeCoverage($stub, $filter); + + $coverage->start( + new \BankAccountTest('testBalanceIsInitiallyZero'), + true + ); + + $coverage->stop( + true, + [TEST_FILES_PATH . 'NamespacedBankAccount.php' => \range(12, 15)] + ); + + $coverage->start( + new \BankAccountTest('testBalanceCannotBecomeNegative') + ); + + $coverage->stop( + true, + [TEST_FILES_PATH . 'NamespacedBankAccount.php' => \range(33, 38)] + ); + + $coverage->start( + new \BankAccountTest('testBalanceCannotBecomeNegative2') + ); + + $coverage->stop( + true, + [TEST_FILES_PATH . 'NamespacedBankAccount.php' => \range(26, 31)] + ); + + $coverage->start( + new \BankAccountTest('testDepositWithdrawMoney') + ); + + $coverage->stop( + true, + [ + TEST_FILES_PATH . 'NamespacedBankAccount.php' => \array_merge( + \range(12, 15), + \range(26, 31), + \range(33, 38) + ), + ] + ); + + return $coverage; + } + protected function getCoverageForBankAccountForFirstTwoTests(): CodeCoverage { $data = $this->getXdebugDataForBankAccount(); diff --git a/tests/_files/BankAccount-text-summary.txt b/tests/_files/BankAccount-text-summary.txt new file mode 100644 index 000000000..c0fb9cc7d --- /dev/null +++ b/tests/_files/BankAccount-text-summary.txt @@ -0,0 +1,7 @@ + + +Code Coverage Report Summary: + Classes: 0.00% (0/1) + Methods: 75.00% (3/4) + Lines: 50.00% (5/10) + diff --git a/tests/_files/NamespacedBankAccount-text.txt b/tests/_files/NamespacedBankAccount-text.txt new file mode 100644 index 000000000..fa153bd8f --- /dev/null +++ b/tests/_files/NamespacedBankAccount-text.txt @@ -0,0 +1,14 @@ + + +Code Coverage Report: + %s + + Summary: + Classes: 0.00% (0/1) + Methods: 75.00% (3/4) + Lines: 50.00% (5/10) + +@OldStylePackageName::SomeNamespace\BankAccount + Methods: ( 0/ 0) Lines: ( 0/ 0) +SomeNamespace\BankAccountTrait + Methods: 75.00% ( 3/ 4) Lines: 50.00% ( 5/ 10) diff --git a/tests/_files/NamespacedBankAccount.php b/tests/_files/NamespacedBankAccount.php new file mode 100644 index 000000000..9a14e2421 --- /dev/null +++ b/tests/_files/NamespacedBankAccount.php @@ -0,0 +1,39 @@ +balance; + } + + protected function setBalance($balance) + { + if ($balance >= 0) { + $this->balance = $balance; + } else { + throw new \RuntimeException; + } + } + + public function depositMoney($balance) + { + $this->setBalance($this->getBalance() + $balance); + + return $this->getBalance(); + } + + public function withdrawMoney($balance) + { + $this->setBalance($this->getBalance() - $balance); + + return $this->getBalance(); + } +} diff --git a/tests/tests/FilterTest.php b/tests/tests/FilterTest.php index 807adbb80..ed8e55af3 100644 --- a/tests/tests/FilterTest.php +++ b/tests/tests/FilterTest.php @@ -64,6 +64,7 @@ protected function setUp(): void TEST_FILES_PATH . 'NamespaceCoverageProtectedTest.php', TEST_FILES_PATH . 'NamespaceCoveragePublicTest.php', TEST_FILES_PATH . 'NamespaceCoveredClass.php', + TEST_FILES_PATH . 'NamespacedBankAccount.php', TEST_FILES_PATH . 'NotExistingCoveredElementTest.php', TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php', TEST_FILES_PATH . 'source_with_ignore.php', diff --git a/tests/tests/TextTest.php b/tests/tests/TextTest.php index ce0846fa7..f3b8d5c2b 100644 --- a/tests/tests/TextTest.php +++ b/tests/tests/TextTest.php @@ -26,6 +26,26 @@ public function testTextForBankAccountTest(): void ); } + public function testTextOnlySummaryForBankAccountTest(): void + { + $text = new Text(50, 90, false, true); + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'BankAccount-text-summary.txt', + \str_replace(\PHP_EOL, "\n", $text->process($this->getCoverageForBankAccount())) + ); + } + + public function testTextForNamespacedBankAccountTest(): void + { + $text = new Text(50, 90, true, false); + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'NamespacedBankAccount-text.txt', + \str_replace(\PHP_EOL, "\n", $text->process($this->getCoverageForNamespacedBankAccount())) + ); + } + public function testTextForFileWithIgnoredLines(): void { $text = new Text(50, 90, false, false);