Skip to content

Commit

Permalink
Add additional tests for text report
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug authored and sebastianbergmann committed May 19, 2020
1 parent a707e74 commit 227fa31
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
118 changes: 118 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 7 additions & 0 deletions tests/_files/BankAccount-text-summary.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


Code Coverage Report Summary:
Classes: 0.00% (0/1)
Methods: 75.00% (3/4)
Lines: 50.00% (5/10)

14 changes: 14 additions & 0 deletions tests/_files/NamespacedBankAccount-text.txt
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 39 additions & 0 deletions tests/_files/NamespacedBankAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace SomeNamespace;
/** @package OldStylePackageName */
class BankAccount
{
use BankAccountTrait;
}

trait BankAccountTrait {
protected $balance = 0;

public function getBalance()
{
return $this->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();
}
}
1 change: 1 addition & 0 deletions tests/tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
20 changes: 20 additions & 0 deletions tests/tests/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 227fa31

Please sign in to comment.