Skip to content
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
24 changes: 23 additions & 1 deletion src/CodeCoverage/Report/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,28 @@ private function reducePaths(&$files)

ksort($files);

return substr($commonPath, 0, -1);
return $this->removeTailingDirectorySeparator($commonPath);
}

/**
* @param $commonPath
* @return bool
*/
private function isNotRoot($commonPath)
{
return strlen($commonPath) > 1;
}

/**
* @param $commonPath
* @return string
*/
private function removeTailingDirectorySeparator($commonPath)
{
if ($this->isNotRoot($commonPath)) {
$commonPath = substr($commonPath, 0, -1);
return $commonPath;
}
return $commonPath;
}
}
11 changes: 10 additions & 1 deletion src/CodeCoverage/Report/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ abstract class PHP_CodeCoverage_Report_Node implements Countable
*/
public function __construct($name, PHP_CodeCoverage_Report_Node $parent = null)
{
if (substr($name, -1) == '/') {
if ($this->hasTailingSlashAndIsNotRoot($name)) {
$name = substr($name, 0, -1);
}

Expand Down Expand Up @@ -377,4 +377,13 @@ abstract public function getNumFunctions();
* @return integer
*/
abstract public function getNumTestedFunctions();

/**
* @param $name
* @return bool
*/
private function hasTailingSlashAndIsNotRoot($name)
{
return substr($name, -1) == '/' && strlen($name) > 1;
}
}
11 changes: 11 additions & 0 deletions tests/PHP/CodeCoverage/Report/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,17 @@ public function reducePathsProvider()
'/home/sb/Money/MoneyBag.php' => array()
)
),
array(
array(
'driveA/Money.php' => array(),
'driveB/MoneyBag.php' => array()
),
'/',
array(
'/driveA/Money.php' => array(),
'/driveB/MoneyBag.php' => array()
)
),
array(
array(
'Money.php' => array()
Expand Down
98 changes: 98 additions & 0 deletions tests/PHP/CodeCoverage/Report/NodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* PHP_CodeCoverage
*
* Copyright (c) 2009-2014, Sebastian Bergmann <sebastian@phpunit.de>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Sebastian Bergmann nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category PHP
* @package CodeCoverage
* @subpackage Tests
* @author Sebastian Bergmann <sebastian@phpunit.de>
* @copyright 2009-2014 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @link http://github.com/sebastianbergmann/php-code-coverage
*/

/**
* Tests for the PHP_CodeCoverage_Report_Node class.
*
* @category PHP
* @package CodeCoverage
* @subpackage Tests
* @author Sebastian Bergmann <sebastian@phpunit.de>
* @copyright 2009-2014 Sebastian Bergmann <sebastian@phpunit.de>
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
* @link http://github.com/sebastianbergmann/php-code-coverage
*/
class PHP_CodeCoverage_Report_NodeTest extends PHPUnit_Framework_TestCase
{
public function testTrimsTailingSlashes()
{
$node = $this->getInstance('/SomeName.php/');
$this->assertEquals('/SomeName.php', $node->getPath());
}

public function testNodeAcceptsRootScope()
{
$node = $this->getInstance('/');
$this->assertEquals('/', $node->getPath());
}

/**
* @param $path
* @return PHP_CodeCoverage_Report_Node
*/
private function getInstance($path)
{
$builder = $this->getMockBuilder('PHP_CodeCoverage_Report_Node')
->setConstructorArgs(array($path));

$this->mockMethods($builder);
return $builder->getMock();
}

/**
* @param $builder
*/
private function mockMethods($builder)
{
$reflectionClass = new \ReflectionClass('PHP_CodeCoverage_Report_Node');
$methods = array();
foreach ($reflectionClass->getMethods() as $method) {
if ($method->isAbstract()) {
$methods[] = $method->getName();
}
}
$builder->setMethods($methods);
}
}