<?php
if (!defined('TEST_FILES_PATH')) {
define('TEST_FILES_PATH', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR);
}
require_once TEST_FILES_PATH . '../TestCase.php';
require_once TEST_FILES_PATH . 'BankAccount.php';
require_once TEST_FILES_PATH . 'BankAccountTest.php';
class PHP_CodeCoverageTest extends PHP_CodeCoverage_TestCase {
protected $coverage;
protected $getLinesToBeCovered;
protected function setUp() {
$this->coverage = new PHP_CodeCoverage();
$this->getLinesToBeCovered = new ReflectionMethod('PHP_CodeCoverage', 'getLinesToBeCovered');
$this->getLinesToBeCovered
->setAccessible(TRUE);
}
public function testConstructor() {
$this
->assertAttributeInstanceOf('PHP_CodeCoverage_Driver_Xdebug', 'driver', $this->coverage);
$this
->assertAttributeInstanceOf('PHP_CodeCoverage_Filter', 'filter', $this->coverage);
}
public function testConstructor2() {
$filter = new PHP_CodeCoverage_Filter();
$coverage = new PHP_CodeCoverage(NULL, $filter);
$this
->assertAttributeInstanceOf('PHP_CodeCoverage_Driver_Xdebug', 'driver', $coverage);
$this
->assertSame($filter, $coverage
->filter());
}
public function testStartThrowsExceptionForInvalidArgument() {
$this->coverage
->start(NULL, array(), NULL);
}
public function testStopThrowsExceptionForInvalidArgument() {
$this->coverage
->stop(NULL);
}
public function testAppendThrowsExceptionForInvalidArgument() {
$this->coverage
->append(array(), NULL);
}
public function testSetCacheTokensThrowsExceptionForInvalidArgument() {
$this->coverage
->setCacheTokens(NULL);
}
public function testSetCacheTokens() {
$this->coverage
->setCacheTokens(TRUE);
$this
->assertAttributeEquals(TRUE, 'cacheTokens', $this->coverage);
}
public function testSetForceCoversAnnotationThrowsExceptionForInvalidArgument() {
$this->coverage
->setForceCoversAnnotation(NULL);
}
public function testSetForceCoversAnnotation() {
$this->coverage
->setForceCoversAnnotation(TRUE);
$this
->assertAttributeEquals(TRUE, 'forceCoversAnnotation', $this->coverage);
}
public function testSetProcessUncoveredFilesFromWhitelistThrowsExceptionForInvalidArgument() {
$this->coverage
->setProcessUncoveredFilesFromWhitelist(NULL);
}
public function testSetProcessUncoveredFilesFromWhitelist() {
$this->coverage
->setProcessUncoveredFilesFromWhitelist(TRUE);
$this
->assertAttributeEquals(TRUE, 'processUncoveredFilesFromWhitelist', $this->coverage);
}
public function testSetMapTestClassNameToCoveredClassName() {
$this->coverage
->setMapTestClassNameToCoveredClassName(TRUE);
$this
->assertAttributeEquals(TRUE, 'mapTestClassNameToCoveredClassName', $this->coverage);
}
public function testSetMapTestClassNameToCoveredClassNameThrowsExceptionForInvalidArgument() {
$this->coverage
->setMapTestClassNameToCoveredClassName(NULL);
}
public function testClear() {
$this->coverage
->clear();
$this
->assertAttributeEquals(NULL, 'currentId', $this->coverage);
$this
->assertAttributeEquals(array(), 'data', $this->coverage);
$this
->assertAttributeEquals(array(), 'tests', $this->coverage);
}
public function testCollect() {
$coverage = $this
->getCoverageForBankAccount();
$this
->assertEquals($this
->getExpectedDataArrayForBankAccount(), $coverage
->getData());
$this
->assertEquals(array(
'BankAccountTest::testBalanceIsInitiallyZero' => NULL,
'BankAccountTest::testBalanceCannotBecomeNegative' => NULL,
'BankAccountTest::testBalanceCannotBecomeNegative2' => NULL,
'BankAccountTest::testDepositWithdrawMoney' => NULL,
), $coverage
->getTests());
}
public function testMerge() {
$coverage = $this
->getCoverageForBankAccountForFirstTwoTests();
$coverage
->merge($this
->getCoverageForBankAccountForLastTwoTests());
$this
->assertEquals($this
->getExpectedDataArrayForBankAccount(), $coverage
->getData());
}
public function testMerge2() {
$coverage = new PHP_CodeCoverage($this
->getMock('PHP_CodeCoverage_Driver_Xdebug'), new PHP_CodeCoverage_Filter());
$coverage
->merge($this
->getCoverageForBankAccount());
$this
->assertEquals($this
->getExpectedDataArrayForBankAccount(), $coverage
->getData());
}
public function testGetLinesToBeCovered($test, $lines) {
if (strpos($test, 'Namespace') === 0) {
$expected = array(
TEST_FILES_PATH . 'NamespaceCoveredClass.php' => $lines,
);
}
else {
if ($test === 'CoverageNoneTest') {
$expected = array();
}
else {
if ($test === 'CoverageNothingTest') {
$expected = false;
}
else {
if ($test === 'CoverageFunctionTest') {
$expected = array(
TEST_FILES_PATH . 'CoveredFunction.php' => $lines,
);
}
else {
$expected = array(
TEST_FILES_PATH . 'CoveredClass.php' => $lines,
);
}
}
}
}
$this
->assertEquals($expected, $this->getLinesToBeCovered
->invoke($this->coverage, $test, 'testSomething'));
}
public function testGetLinesToBeCovered2() {
$this->getLinesToBeCovered
->invoke($this->coverage, 'NotExistingCoveredElementTest', 'testOne');
}
public function testGetLinesToBeCovered3() {
$this->getLinesToBeCovered
->invoke($this->coverage, 'NotExistingCoveredElementTest', 'testTwo');
}
public function testGetLinesToBeCovered4() {
$this->getLinesToBeCovered
->invoke($this->coverage, 'NotExistingCoveredElementTest', 'testThree');
}
public function testGetLinesToBeCoveredSkipsNonExistantMethods() {
$this
->assertSame(array(), $this->getLinesToBeCovered
->invoke($this->coverage, 'NotExistingCoveredElementTest', 'methodDoesNotExist'));
}
public function testTwoCoversDefaultClassAnnoationsAreNotAllowed() {
$this->getLinesToBeCovered
->invoke($this->coverage, 'CoverageTwoDefaultClassAnnotations', 'testSomething');
}
public function testFunctionParenthesesAreAllowed() {
$this
->assertSame(array(
TEST_FILES_PATH . 'CoveredFunction.php' => range(2, 4),
), $this->getLinesToBeCovered
->invoke($this->coverage, 'CoverageFunctionParenthesesTest', 'testSomething'));
}
public function testFunctionParenthesesAreAllowedWithWhitespace() {
$this
->assertSame(array(
TEST_FILES_PATH . 'CoveredFunction.php' => range(2, 4),
), $this->getLinesToBeCovered
->invoke($this->coverage, 'CoverageFunctionParenthesesWhitespaceTest', 'testSomething'));
}
public function testMethodParenthesesAreAllowed() {
$this
->assertSame(array(
TEST_FILES_PATH . 'CoveredClass.php' => range(31, 35),
), $this->getLinesToBeCovered
->invoke($this->coverage, 'CoverageMethodParenthesesTest', 'testSomething'));
}
public function testMethodParenthesesAreAllowedWithWhitespace() {
$this
->assertSame(array(
TEST_FILES_PATH . 'CoveredClass.php' => range(31, 35),
), $this->getLinesToBeCovered
->invoke($this->coverage, 'CoverageMethodParenthesesWhitespaceTest', 'testSomething'));
}
public function getLinesToBeCoveredProvider() {
return array(
array(
'CoverageNoneTest',
array(),
),
array(
'CoverageClassExtendedTest',
array_merge(range(19, 36), range(2, 17)),
),
array(
'CoverageClassTest',
range(19, 36),
),
array(
'CoverageMethodTest',
range(31, 35),
),
array(
'CoverageMethodOneLineAnnotationTest',
range(31, 35),
),
array(
'CoverageNotPrivateTest',
array_merge(range(25, 29), range(31, 35)),
),
array(
'CoverageNotProtectedTest',
array_merge(range(21, 23), range(31, 35)),
),
array(
'CoverageNotPublicTest',
array_merge(range(21, 23), range(25, 29)),
),
array(
'CoveragePrivateTest',
range(21, 23),
),
array(
'CoverageProtectedTest',
range(25, 29),
),
array(
'CoveragePublicTest',
range(31, 35),
),
array(
'CoverageFunctionTest',
range(2, 4),
),
array(
'NamespaceCoverageClassExtendedTest',
array_merge(range(21, 38), range(4, 19)),
),
array(
'NamespaceCoverageClassTest',
range(21, 38),
),
array(
'NamespaceCoverageMethodTest',
range(33, 37),
),
array(
'NamespaceCoverageNotPrivateTest',
array_merge(range(27, 31), range(33, 37)),
),
array(
'NamespaceCoverageNotProtectedTest',
array_merge(range(23, 25), range(33, 37)),
),
array(
'NamespaceCoverageNotPublicTest',
array_merge(range(23, 25), range(27, 31)),
),
array(
'NamespaceCoveragePrivateTest',
range(23, 25),
),
array(
'NamespaceCoverageProtectedTest',
range(27, 31),
),
array(
'NamespaceCoveragePublicTest',
range(33, 37),
),
array(
'NamespaceCoverageCoversClassTest',
array_merge(range(23, 25), range(27, 31), range(33, 37), range(6, 8), range(10, 13), range(15, 18)),
),
array(
'NamespaceCoverageCoversClassPublicTest',
range(33, 37),
),
array(
'CoverageNothingTest',
false,
),
);
}
}