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);