123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- <?php
- /*
- * This file is part of the php-code-coverage package.
- *
- * (c) Sebastian Bergmann <sebastian@phpunit.de>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace SebastianBergmann\CodeCoverage\Report;
- use SebastianBergmann\CodeCoverage\Driver\Driver;
- use SebastianBergmann\CodeCoverage\CodeCoverage;
- use SebastianBergmann\CodeCoverage\Filter;
- use SebastianBergmann\CodeCoverage\TestCase;
- use SebastianBergmann\CodeCoverage\Node\Builder;
- class BuilderTest extends TestCase
- {
- protected $factory;
- protected function setUp()
- {
- $this->factory = new Builder;
- }
- public function testSomething()
- {
- $root = $this->getCoverageForBankAccount()->getReport();
- $expectedPath = rtrim(TEST_FILES_PATH, DIRECTORY_SEPARATOR);
- $this->assertEquals($expectedPath, $root->getName());
- $this->assertEquals($expectedPath, $root->getPath());
- $this->assertEquals(10, $root->getNumExecutableLines());
- $this->assertEquals(5, $root->getNumExecutedLines());
- $this->assertEquals(1, $root->getNumClasses());
- $this->assertEquals(0, $root->getNumTestedClasses());
- $this->assertEquals(4, $root->getNumMethods());
- $this->assertEquals(3, $root->getNumTestedMethods());
- $this->assertEquals('0.00%', $root->getTestedClassesPercent());
- $this->assertEquals('75.00%', $root->getTestedMethodsPercent());
- $this->assertEquals('50.00%', $root->getLineExecutedPercent());
- $this->assertEquals(0, $root->getNumFunctions());
- $this->assertEquals(0, $root->getNumTestedFunctions());
- $this->assertNull($root->getParent());
- $this->assertEquals([], $root->getDirectories());
- #$this->assertEquals(array(), $root->getFiles());
- #$this->assertEquals(array(), $root->getChildNodes());
- $this->assertEquals(
- [
- 'BankAccount' => [
- 'methods' => [
- 'getBalance' => [
- 'signature' => 'getBalance()',
- 'startLine' => 6,
- 'endLine' => 9,
- 'executableLines' => 1,
- 'executedLines' => 1,
- 'ccn' => 1,
- 'coverage' => 100,
- 'crap' => '1',
- 'link' => 'BankAccount.php.html#6',
- 'methodName' => 'getBalance',
- 'visibility' => 'public',
- ],
- 'setBalance' => [
- 'signature' => 'setBalance($balance)',
- 'startLine' => 11,
- 'endLine' => 18,
- 'executableLines' => 5,
- 'executedLines' => 0,
- 'ccn' => 2,
- 'coverage' => 0,
- 'crap' => 6,
- 'link' => 'BankAccount.php.html#11',
- 'methodName' => 'setBalance',
- 'visibility' => 'protected',
- ],
- 'depositMoney' => [
- 'signature' => 'depositMoney($balance)',
- 'startLine' => 20,
- 'endLine' => 25,
- 'executableLines' => 2,
- 'executedLines' => 2,
- 'ccn' => 1,
- 'coverage' => 100,
- 'crap' => '1',
- 'link' => 'BankAccount.php.html#20',
- 'methodName' => 'depositMoney',
- 'visibility' => 'public',
- ],
- 'withdrawMoney' => [
- 'signature' => 'withdrawMoney($balance)',
- 'startLine' => 27,
- 'endLine' => 32,
- 'executableLines' => 2,
- 'executedLines' => 2,
- 'ccn' => 1,
- 'coverage' => 100,
- 'crap' => '1',
- 'link' => 'BankAccount.php.html#27',
- 'methodName' => 'withdrawMoney',
- 'visibility' => 'public',
- ],
- ],
- 'startLine' => 2,
- 'executableLines' => 10,
- 'executedLines' => 5,
- 'ccn' => 5,
- 'coverage' => 50,
- 'crap' => '8.12',
- 'package' => [
- 'namespace' => '',
- 'fullPackage' => '',
- 'category' => '',
- 'package' => '',
- 'subpackage' => ''
- ],
- 'link' => 'BankAccount.php.html#2',
- 'className' => 'BankAccount'
- ]
- ],
- $root->getClasses()
- );
- $this->assertEquals([], $root->getFunctions());
- }
- public function testNotCrashParsing()
- {
- $coverage = $this->getCoverageForCrashParsing();
- $root = $coverage->getReport();
- $expectedPath = rtrim(TEST_FILES_PATH, DIRECTORY_SEPARATOR);
- $this->assertEquals($expectedPath, $root->getName());
- $this->assertEquals($expectedPath, $root->getPath());
- $this->assertEquals(2, $root->getNumExecutableLines());
- $this->assertEquals(0, $root->getNumExecutedLines());
- $data = $coverage->getData();
- $expectedFile = $expectedPath . DIRECTORY_SEPARATOR . 'Crash.php';
- $this->assertSame([$expectedFile => [1 => [], 2 => []]], $data);
- }
- public function testBuildDirectoryStructure()
- {
- $s = \DIRECTORY_SEPARATOR;
- $method = new \ReflectionMethod(
- Builder::class,
- 'buildDirectoryStructure'
- );
- $method->setAccessible(true);
- $this->assertEquals(
- [
- 'src' => [
- 'Money.php/f' => [],
- 'MoneyBag.php/f' => [],
- 'Foo' => [
- 'Bar' => [
- 'Baz' => [
- 'Foo.php/f' => [],
- ],
- ],
- ],
- ]
- ],
- $method->invoke(
- $this->factory,
- [
- "src{$s}Money.php" => [],
- "src{$s}MoneyBag.php" => [],
- "src{$s}Foo{$s}Bar{$s}Baz{$s}Foo.php" => [],
- ]
- )
- );
- }
- /**
- * @dataProvider reducePathsProvider
- */
- public function testReducePaths($reducedPaths, $commonPath, $paths)
- {
- $method = new \ReflectionMethod(
- Builder::class,
- 'reducePaths'
- );
- $method->setAccessible(true);
- $_commonPath = $method->invokeArgs($this->factory, [&$paths]);
- $this->assertEquals($reducedPaths, $paths);
- $this->assertEquals($commonPath, $_commonPath);
- }
- public function reducePathsProvider()
- {
- $s = \DIRECTORY_SEPARATOR;
- yield [
- [],
- ".",
- []
- ];
- $prefixes = ["C:$s", "$s"];
- foreach($prefixes as $p){
- yield [
- [
- "Money.php" => []
- ],
- "{$p}home{$s}sb{$s}Money{$s}",
- [
- "{$p}home{$s}sb{$s}Money{$s}Money.php" => []
- ]
- ];
- yield [
- [
- "Money.php" => [],
- "MoneyBag.php" => []
- ],
- "{$p}home{$s}sb{$s}Money",
- [
- "{$p}home{$s}sb{$s}Money{$s}Money.php" => [],
- "{$p}home{$s}sb{$s}Money{$s}MoneyBag.php" => []
- ]
- ];
- yield [
- [
- "Money.php" => [],
- "MoneyBag.php" => [],
- "Cash.phar{$s}Cash.php" => [],
- ],
- "{$p}home{$s}sb{$s}Money",
- [
- "{$p}home{$s}sb{$s}Money{$s}Money.php" => [],
- "{$p}home{$s}sb{$s}Money{$s}MoneyBag.php" => [],
- "phar://{$p}home{$s}sb{$s}Money{$s}Cash.phar{$s}Cash.php" => [],
- ],
- ];
- }
- }
- }
|