Skip to content

Commit

Permalink
Fix coverage of phpunit data providers (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 6, 2022
1 parent 34e4383 commit 9686c15
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 67 deletions.
7 changes: 2 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
</listeners>
<coverage>
<include>
<directory suffix=".php">src</directory>
<directory>src</directory>
<directory>tests</directory>
</include>
<exclude>
<file>src/Phpunit/ResultPrinter.php</file>
<file>src/Phpunit/TestCase.php</file>
</exclude>
<report>
<php outputFile="coverage/phpunit.cov" />
</report>
Expand Down
2 changes: 0 additions & 2 deletions src/DebugTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ trait DebugTrait

/**
* Outputs message to STDERR.
*
* @codeCoverageIgnore - replaced with "echo" which can be intercepted by test-suite
*/
protected function _echoStderr(string $message): void
{
Expand Down
2 changes: 0 additions & 2 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ public function addMoreInfo(string $param, $value): self
/**
* Add a suggested/possible solution to the exception.
*
* @TODO can be added more features? usually we are out of App
*
* @return $this
*/
public function addSolution(string $solution): self
Expand Down
32 changes: 32 additions & 0 deletions src/Phpunit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,40 @@ abstract class TestCase extends BaseTestCase
{
use WarnDynamicPropertyTrait;

protected function setUp(): void
{
// rerun data providers to fix coverage when coverage for test files is enabled
// https://github.com/sebastianbergmann/php-code-coverage/issues/920
$staticClass = get_class(new class() {
/** @var array<string, true> */
public static $processedMethods = [];
});
$classRefl = new \ReflectionClass(static::class);
foreach ($classRefl->getMethods() as $methodRefl) {
$methodDoc = $methodRefl->getDocComment();
if ($methodDoc !== false && preg_match('~@dataProvider[ \t]+([\w\x7f-\xff]+::)?([\w\x7f-\xff]+)~', $methodDoc, $matches)) {
$providerClassRefl = $matches[1] === '' ? $classRefl : new \ReflectionClass($matches[1]);
$providerMethodRefl = $providerClassRefl->getMethod($matches[2]);
$key = $providerClassRefl->getName() . '::' . $providerMethodRefl->getName();
if (!isset($staticClass::$processedMethods[$key])) {
$staticClass::$processedMethods[static::class] = true;
$providerInstance = $providerClassRefl->newInstanceWithoutConstructor();
$provider = $providerMethodRefl->invoke($providerInstance);
if (!is_array($provider)) {
// yield all provider data
iterator_to_array($provider);
}
}
}
}

parent::setUp();
}

protected function tearDown(): void
{
parent::tearDown();

// remove once https://github.com/sebastianbergmann/phpunit/issues/4705 is fixed
foreach (array_keys(array_diff_key(get_object_vars($this), get_class_vars(BaseTestCase::class))) as $k) {
$this->{$k} = \PHP_MAJOR_VERSION < 8
Expand Down
6 changes: 2 additions & 4 deletions src/ReadableCaptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ trait ReadableCaptionTrait
*/
public function readableCaption(string $s): string
{
// $s = 'this\\ _isNASA_MyBigBull shit_123\Foo';

// first remove not allowed characters and uppercase words
$s = ucwords(preg_replace('/[^a-z0-9]+/i', ' ', $s));
$s = ucwords(preg_replace('~[^a-z\d]+~i', ' ', $s));

// and then run regex to split camelcased words too
$s = array_map('trim', preg_split('/^[^A-Z\d]+\K|[A-Z\d][^A-Z\d]+\K/', $s, -1, \PREG_SPLIT_NO_EMPTY));
$s = array_map('trim', preg_split('~(?:^|[A-Z\d])[^A-Z\d]+\K~', $s, -1, \PREG_SPLIT_NO_EMPTY));
$s = implode(' ', $s);

return $s;
Expand Down
12 changes: 0 additions & 12 deletions src/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,11 @@ public function setDefaultDomain(string $defaultDomain): self
return $this;
}

/**
* No clone.
*
* @codeCoverageIgnore
*/
protected function __clone()
{
throw new Exception('Translator cannot be cloned');
}

/**
* No serialize.
*
* @codeCoverageIgnore
*/
public function __wakeup(): void
{
throw new Exception('Translator cannot be serialized');
Expand Down Expand Up @@ -119,8 +109,6 @@ public function setAdapter(ITranslatorAdapter $translator): self

/**
* Get the adapter.
*
* @TODO should remain private?
*/
private function getAdapter(): ITranslatorAdapter
{
Expand Down
3 changes: 0 additions & 3 deletions tests/AppScopeTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
use Atk4\Core\Phpunit\TestCase;
use Atk4\Core\TrackableTrait;

/**
* @coversDefaultClass \Atk4\Core\AppScopeTrait
*/
class AppScopeTraitTest extends TestCase
{
public function testConstruct(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/CollectionTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use Atk4\Core;
use Atk4\Core\Phpunit\TestCase;

/**
* @coversDefaultClass \Atk4\Core\ContainerTrait
*/
class CollectionTraitTest extends TestCase
{
public function testBasic(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/ConfigTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use Atk4\Core\ConfigTrait;
use Atk4\Core\Phpunit\TestCase;

/**
* @coversDefaultClass \Atk4\Core\ConfigTrait
*/
class ConfigTraitTest extends TestCase
{
/** @var string */
Expand Down
3 changes: 0 additions & 3 deletions tests/ContainerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use Atk4\Core;
use Atk4\Core\Phpunit\TestCase;

/**
* @coversDefaultClass \Atk4\Core\ContainerTrait
*/
class ContainerTraitTest extends TestCase
{
public function testBasic(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/DebugTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use Atk4\Core\DebugTrait;
use Atk4\Core\Phpunit\TestCase;

/**
* @coversDefaultClass \Atk4\Core\DebugTrait
*/
class DebugTraitTest extends TestCase
{
/**
Expand Down
3 changes: 0 additions & 3 deletions tests/DiContainerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use Atk4\Core\Exception;
use Atk4\Core\Phpunit\TestCase;

/**
* @coversDefaultClass \Atk4\Core\DiContainerTrait
*/
class DiContainerTraitTest extends TestCase
{
public function testFromSeed(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/DynamicMethodTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
use Atk4\Core\HookTrait;
use Atk4\Core\Phpunit\TestCase;

/**
* @coversDefaultClass \Atk4\Core\DynamicMethodTrait
*/
class DynamicMethodTraitTest extends TestCase
{
public function testConstruct(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
use Atk4\Core\Phpunit\TestCase;
use Atk4\Core\TrackableTrait;

/**
* @coversDefaultClass \Atk4\Core\Exception
*/
class ExceptionTest extends TestCase
{
public function testBasic(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
use Atk4\Core\HookBreaker;
use Atk4\Core\Phpunit\TestCase;

/**
* @coversDefaultClass \Atk4\Core\Factory
*/
class FactoryTest extends TestCase
{
public function testMerge1(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/HookTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
use Atk4\Core\HookTrait;
use Atk4\Core\Phpunit\TestCase;

/**
* @coversDefaultClass \Atk4\Core\HookTrait
*/
class HookTraitTest extends TestCase
{
public function testArguments(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/InitializerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use Atk4\Core\Exception;
use Atk4\Core\Phpunit\TestCase;

/**
* @coversDefaultClass \Atk4\Core\InitializerTrait
*/
class InitializerTraitTest extends TestCase
{
public function testInit(): void
Expand Down
34 changes: 34 additions & 0 deletions tests/Phpunit/TestCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Atk4\Core\Tests\Phpunit;

use Atk4\Core\Phpunit\TestCase;

class TestCaseTest extends TestCase
{
/** @var int */
private static $providerCallCounter = 0;

private function coverCoverageFromProvider(): void
{
++self::$providerCallCounter;
}

/**
* @dataProvider provideProviderCoverage
*/
public function testProviderCoverage(string $v): void
{
$this->assertGreaterThan(1, self::$providerCallCounter);
$this->assertTrue(in_array($v, ['x', 'y'], true));
}

public function provideProviderCoverage(): \Traversable
{
yield ['x'];
$this->coverCoverageFromProvider();
yield ['y'];
}
}
3 changes: 0 additions & 3 deletions tests/ReadableCaptionTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
use Atk4\Core\Phpunit\TestCase;
use Atk4\Core\ReadableCaptionTrait;

/**
* @coversDefaultClass \Atk4\Core\ReadableCaptionTrait
*/
class ReadableCaptionTraitTest extends TestCase
{
/**
Expand Down
3 changes: 0 additions & 3 deletions tests/StaticAddToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public function __construct(string $name)
}
}

/**
* @coversDefaultClass \Atk4\Core\StaticAddToTrait
*/
class StaticAddToTest extends TestCase
{
public function testBasic(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/TraitUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
use Atk4\Core\Phpunit\TestCase;
use Atk4\Core\TraitUtil;

/**
* @coversDefaultClass \Atk4\Core\TraitUtil
*/
class TraitUtilTest extends TestCase
{
public function testHasTrait(): void
Expand Down

0 comments on commit 9686c15

Please sign in to comment.