Skip to content

Commit 1eeb948

Browse files
Schranksebastianbergmann
authored andcommitted
Add tests for code coverage
1 parent 8a46005 commit 1eeb948

File tree

9 files changed

+227
-0
lines changed

9 files changed

+227
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Add tests for code coverage attributes for class, method, function
3+
--INI--
4+
pcov.directory=tests/end-to-end/code-coverage/code-coverage
5+
--SKIPIF--
6+
<?php declare(strict_types=1);
7+
require __DIR__ . '/../../_files/skip-if-requires-code-coverage-driver.php';
8+
--FILE--
9+
<?php declare(strict_types=1);
10+
$_SERVER['argv'][] = '--do-not-cache-result';
11+
$_SERVER['argv'][] = '--colors=never';
12+
$_SERVER['argv'][] = '--coverage-text';
13+
$_SERVER['argv'][] = '--configuration';
14+
$_SERVER['argv'][] = __DIR__ . '/code-coverage/phpunit.xml';
15+
16+
require_once __DIR__ . '/../../bootstrap.php';
17+
18+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
19+
--EXPECTF--
20+
PHPUnit %s by Sebastian Bergmann and contributors.
21+
22+
Runtime: %s
23+
Configuration: %s
24+
25+
... 3 / 3 (100%)
26+
27+
Time: %s, Memory: %s MB
28+
29+
OK (3 tests, 3 assertions)
30+
31+
32+
Code Coverage Report:%w
33+
%s
34+
%w
35+
%wSummary:%w
36+
%wClasses: 50.00% (1/2)%w
37+
%wMethods: 75.00% (3/4)%w
38+
%wLines: 66.67% (4/6)%w
39+
%w
40+
%wPHPUnit\TestFixture\CodeCoverage\FullyTestedClass%w
41+
%wMethods: 100.00% ( 1/ 1) Lines: 100.00% ( 1/ 1)
42+
%wPHPUnit\TestFixture\CodeCoverage\Method
43+
%wMethods: 66.67% ( 2/ 3) Lines: 66.67% ( 2/ 3)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
require_once __DIR__.'/src/Method.php';
6+
require_once __DIR__.'/src/FullyTestedClass.php';
7+
require_once __DIR__.'/src/functions.php';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="../../../../phpunit.xsd"
4+
bootstrap="bootstrap.php"
5+
executionOrder="depends,defects"
6+
requireCoverageMetadata="true"
7+
beStrictAboutCoverageMetadata="true"
8+
beStrictAboutOutputDuringTests="true"
9+
failOnRisky="true"
10+
failOnWarning="true">
11+
<testsuites>
12+
<testsuite name="default">
13+
<directory>tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
17+
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
18+
<include>
19+
<directory>src</directory>
20+
</include>
21+
</source>
22+
</phpunit>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPUnit\TestFixture\CodeCoverage;
6+
/*
7+
* This file is part of PHPUnit.
8+
*
9+
* (c) Sebastian Bergmann <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
class FullyTestedClass
16+
{
17+
public function method(): bool
18+
{
19+
return true;
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/*
5+
* This file is part of PHPUnit.
6+
*
7+
* (c) Sebastian Bergmann <[email protected]>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace PHPUnit\TestFixture\CodeCoverage;
14+
15+
final class Method
16+
{
17+
public function greet(): string
18+
{
19+
return $this->internalMethod();
20+
}
21+
22+
private function internalMethod(): string
23+
{
24+
return 'Hello, World!';
25+
}
26+
27+
public function unusedPublicMethod(): string
28+
{
29+
return 'never returned';
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPUnit\TestFixture\CodeCoverage;
6+
7+
/*
8+
* This file is part of PHPUnit.
9+
*
10+
* (c) Sebastian Bergmann <[email protected]>
11+
*
12+
* For the full copyright and license information, please view the LICENSE
13+
* file that was distributed with this source code.
14+
*/
15+
16+
17+
function fixture_for_phpunit_code_coverage(): bool
18+
{
19+
return true;
20+
}
21+
22+
23+
function fixture_for_phpunit_code_coverage_not_called(): bool
24+
{
25+
return false;
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPUnit\TestFixture\CodeCoverage;
6+
7+
/*
8+
* This file is part of PHPUnit.
9+
*
10+
* (c) Sebastian Bergmann <[email protected]>
11+
*
12+
* For the full copyright and license information, please view the LICENSE
13+
* file that was distributed with this source code.
14+
*/
15+
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
18+
#[CoversClass(\PHPUnit\TestFixture\CodeCoverage\FullyTestedClass::class)]
19+
class FullyTestedClassTest extends \PHPUnit\Framework\TestCase
20+
{
21+
public function testMethod(): void
22+
{
23+
$this->assertTrue((new FullyTestedClass)->method());
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPUnit\TestFixture\CodeCoverage;
6+
7+
/*
8+
* This file is part of PHPUnit.
9+
*
10+
* (c) Sebastian Bergmann <[email protected]>
11+
*
12+
* For the full copyright and license information, please view the LICENSE
13+
* file that was distributed with this source code.
14+
*/
15+
16+
17+
use PHPUnit\Framework\Attributes\CoversFunction;
18+
use PHPUnit\Framework\TestCase;
19+
20+
#[CoversFunction('\PHPUnit\TestFixture\CodeCoverage\fixture_for_phpunit_code_coverage')]
21+
class FunctionTest extends TestCase
22+
{
23+
public function testFunction(): void
24+
{
25+
$this->assertTrue(fixture_for_phpunit_code_coverage());
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/*
5+
* This file is part of PHPUnit.
6+
*
7+
* (c) Sebastian Bergmann <[email protected]>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace PHPUnit\TestFixture\CodeCoverage;
14+
15+
use PHPUnit\Framework\Attributes\CoversClass;
16+
use PHPUnit\Framework\TestCase;
17+
18+
#[CoversClass(\PHPUnit\TestFixture\CodeCoverage\Method::class)]
19+
final class MethodTest extends TestCase
20+
{
21+
public function testMethod(): void
22+
{
23+
$this->assertSame('Hello, World!', (new Method)->greet());
24+
}
25+
}

0 commit comments

Comments
 (0)