123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- <?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;
- use SebastianBergmann\CodeCoverage\Driver\Driver;
- use SebastianBergmann\CodeCoverage\Report\Xml\Coverage;
- abstract class TestCase extends \PHPUnit\Framework\TestCase
- {
- protected static $TEST_TMP_PATH;
- public static function setUpBeforeClass(): void
- {
- self::$TEST_TMP_PATH = TEST_FILES_PATH . 'tmp';
- }
- protected function getXdebugDataForBankAccount()
- {
- return [
- [
- TEST_FILES_PATH . 'BankAccount.php' => [
- 8 => 1,
- 9 => -2,
- 13 => -1,
- 14 => -1,
- 15 => -1,
- 16 => -1,
- 18 => -1,
- 22 => -1,
- 24 => -1,
- 25 => -2,
- 29 => -1,
- 31 => -1,
- 32 => -2
- ]
- ],
- [
- TEST_FILES_PATH . 'BankAccount.php' => [
- 8 => 1,
- 13 => 1,
- 16 => 1,
- 29 => 1,
- ]
- ],
- [
- TEST_FILES_PATH . 'BankAccount.php' => [
- 8 => 1,
- 13 => 1,
- 16 => 1,
- 22 => 1,
- ]
- ],
- [
- TEST_FILES_PATH . 'BankAccount.php' => [
- 8 => 1,
- 13 => 1,
- 14 => 1,
- 15 => 1,
- 18 => 1,
- 22 => 1,
- 24 => 1,
- 29 => 1,
- 31 => 1,
- ]
- ]
- ];
- }
- protected function getCoverageForBankAccount(): CodeCoverage
- {
- $data = $this->getXdebugDataForBankAccount();
- $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 . 'BankAccount.php');
- $coverage = new CodeCoverage($stub, $filter);
- $coverage->start(
- new \BankAccountTest('testBalanceIsInitiallyZero'),
- true
- );
- $coverage->stop(
- true,
- [TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
- );
- $coverage->start(
- new \BankAccountTest('testBalanceCannotBecomeNegative')
- );
- $coverage->stop(
- true,
- [TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
- );
- $coverage->start(
- new \BankAccountTest('testBalanceCannotBecomeNegative2')
- );
- $coverage->stop(
- true,
- [TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
- );
- $coverage->start(
- new \BankAccountTest('testDepositWithdrawMoney')
- );
- $coverage->stop(
- true,
- [
- TEST_FILES_PATH . 'BankAccount.php' => array_merge(
- range(6, 9),
- range(20, 25),
- range(27, 32)
- )
- ]
- );
- return $coverage;
- }
- protected function getCoverageForBankAccountForFirstTwoTests(): CodeCoverage
- {
- $data = $this->getXdebugDataForBankAccount();
- $stub = $this->createMock(Driver::class);
- $stub->expects($this->any())
- ->method('stop')
- ->will($this->onConsecutiveCalls(
- $data[0],
- $data[1]
- ));
- $filter = new Filter;
- $filter->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
- $coverage = new CodeCoverage($stub, $filter);
- $coverage->start(
- new \BankAccountTest('testBalanceIsInitiallyZero'),
- true
- );
- $coverage->stop(
- true,
- [TEST_FILES_PATH . 'BankAccount.php' => range(6, 9)]
- );
- $coverage->start(
- new \BankAccountTest('testBalanceCannotBecomeNegative')
- );
- $coverage->stop(
- true,
- [TEST_FILES_PATH . 'BankAccount.php' => range(27, 32)]
- );
- return $coverage;
- }
- protected function getCoverageForBankAccountForLastTwoTests()
- {
- $data = $this->getXdebugDataForBankAccount();
- $stub = $this->createMock(Driver::class);
- $stub->expects($this->any())
- ->method('stop')
- ->will($this->onConsecutiveCalls(
- $data[2],
- $data[3]
- ));
- $filter = new Filter;
- $filter->addFileToWhitelist(TEST_FILES_PATH . 'BankAccount.php');
- $coverage = new CodeCoverage($stub, $filter);
- $coverage->start(
- new \BankAccountTest('testBalanceCannotBecomeNegative2')
- );
- $coverage->stop(
- true,
- [TEST_FILES_PATH . 'BankAccount.php' => range(20, 25)]
- );
- $coverage->start(
- new \BankAccountTest('testDepositWithdrawMoney')
- );
- $coverage->stop(
- true,
- [
- TEST_FILES_PATH . 'BankAccount.php' => array_merge(
- range(6, 9),
- range(20, 25),
- range(27, 32)
- )
- ]
- );
- return $coverage;
- }
- protected function getExpectedDataArrayForBankAccount(): array
- {
- return [
- TEST_FILES_PATH . 'BankAccount.php' => [
- 8 => [
- 0 => 'BankAccountTest::testBalanceIsInitiallyZero',
- 1 => 'BankAccountTest::testDepositWithdrawMoney'
- ],
- 9 => null,
- 13 => [],
- 14 => [],
- 15 => [],
- 16 => [],
- 18 => [],
- 22 => [
- 0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
- 1 => 'BankAccountTest::testDepositWithdrawMoney'
- ],
- 24 => [
- 0 => 'BankAccountTest::testDepositWithdrawMoney',
- ],
- 25 => null,
- 29 => [
- 0 => 'BankAccountTest::testBalanceCannotBecomeNegative',
- 1 => 'BankAccountTest::testDepositWithdrawMoney'
- ],
- 31 => [
- 0 => 'BankAccountTest::testDepositWithdrawMoney'
- ],
- 32 => null
- ]
- ];
- }
- protected function getExpectedDataArrayForBankAccountInReverseOrder(): array
- {
- return [
- TEST_FILES_PATH . 'BankAccount.php' => [
- 8 => [
- 0 => 'BankAccountTest::testDepositWithdrawMoney',
- 1 => 'BankAccountTest::testBalanceIsInitiallyZero'
- ],
- 9 => null,
- 13 => [],
- 14 => [],
- 15 => [],
- 16 => [],
- 18 => [],
- 22 => [
- 0 => 'BankAccountTest::testBalanceCannotBecomeNegative2',
- 1 => 'BankAccountTest::testDepositWithdrawMoney'
- ],
- 24 => [
- 0 => 'BankAccountTest::testDepositWithdrawMoney',
- ],
- 25 => null,
- 29 => [
- 0 => 'BankAccountTest::testDepositWithdrawMoney',
- 1 => 'BankAccountTest::testBalanceCannotBecomeNegative'
- ],
- 31 => [
- 0 => 'BankAccountTest::testDepositWithdrawMoney'
- ],
- 32 => null
- ]
- ];
- }
- protected function getCoverageForFileWithIgnoredLines(): CodeCoverage
- {
- $filter = new Filter;
- $filter->addFileToWhitelist(TEST_FILES_PATH . 'source_with_ignore.php');
- $coverage = new CodeCoverage(
- $this->setUpXdebugStubForFileWithIgnoredLines(),
- $filter
- );
- $coverage->start('FileWithIgnoredLines', true);
- $coverage->stop();
- return $coverage;
- }
- protected function setUpXdebugStubForFileWithIgnoredLines(): Driver
- {
- $stub = $this->createMock(Driver::class);
- $stub->expects($this->any())
- ->method('stop')
- ->will($this->returnValue(
- [
- TEST_FILES_PATH . 'source_with_ignore.php' => [
- 2 => 1,
- 4 => -1,
- 6 => -1,
- 7 => 1
- ]
- ]
- ));
- return $stub;
- }
- protected function getCoverageForClassWithAnonymousFunction(): CodeCoverage
- {
- $filter = new Filter;
- $filter->addFileToWhitelist(TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php');
- $coverage = new CodeCoverage(
- $this->setUpXdebugStubForClassWithAnonymousFunction(),
- $filter
- );
- $coverage->start('ClassWithAnonymousFunction', true);
- $coverage->stop();
- return $coverage;
- }
- protected function setUpXdebugStubForClassWithAnonymousFunction(): Driver
- {
- $stub = $this->createMock(Driver::class);
- $stub->expects($this->any())
- ->method('stop')
- ->will($this->returnValue(
- [
- TEST_FILES_PATH . 'source_with_class_and_anonymous_function.php' => [
- 7 => 1,
- 9 => 1,
- 10 => -1,
- 11 => 1,
- 12 => 1,
- 13 => 1,
- 14 => 1,
- 17 => 1,
- 18 => 1
- ]
- ]
- ));
- return $stub;
- }
- protected function getCoverageForCrashParsing(): CodeCoverage
- {
- $filter = new Filter;
- $filter->addFileToWhitelist(TEST_FILES_PATH . 'Crash.php');
- // This is a file with invalid syntax, so it isn't executed.
- return new CodeCoverage(
- $this->setUpXdebugStubForCrashParsing(),
- $filter
- );
- }
- protected function setUpXdebugStubForCrashParsing(): Driver
- {
- $stub = $this->createMock(Driver::class);
- $stub->expects($this->any())
- ->method('stop')
- ->will($this->returnValue([]));
- return $stub;
- }
- }
|