Skip to content

Improve consistency in handling of namespaces #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Node/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
Expand Down
12 changes: 5 additions & 7 deletions src/Report/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
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