PHP code example of cdn77 / test-utils
1. Go to this page and download the library: Download cdn77/test-utils library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cdn77 / test-utils example snippets
class MyEntity
{
/** @var string */
private $property1;
/** @var string */
private $property2;
public function __construct(string $property1, string $property2)
{
$this->property1 = $property1;
$this->property2 = $property2;
}
public function salute() : string
{
return sprintf('Hello %s!', $this->property2);
}
}
$myEntity = Stub::create(MyEntity::class, ['property2' => 'world']);
self::assertSame('Hello world!', $myEntity->salute());
$myEntity = Stub::create(MyEntity::class, ['property2' => 'world']);
$myEntity = Stub::extends($myEntity, ['property1' => 'value']);
// property 1 and 2 are set now
self::assertSame('Hello world!', $myEntity->salute());
use Cdn77\TestUtils\TestCheck\TestCheck;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Group;
#[CoversNothing]
#[Group('integration')]
final class SuiteComplianceTest extends TestCaseBase
{
/** @dataProvider providerChecks */
public function testChecks(TestCheck $check) : void
{
$check->run($this);
}
/** @return Generator<string, array{callable(self): TestCheck}> */
public static function providerChecks() : Generator
{
$testDir = ROOT_PROJECT_DIR . '/tests';
$testFilePathNames = \Symfony\Component\Finder\Finder::create()
->in($testDir)
->files()
->name('*Test.php');
yield 'Every test has group' => [
new EveryTestHasGroup($testFilePathNames),
];
...
}
}
final class FooTest extends TestCase
use PHPUnit\Framework\Attributes\Group;
#[Group('unit')]
final class FooTest extends TestCase
yield 'Every test has group' => [
new EveryTestHasGroup($testFiles),
];
namespace Ns;
final class Unit {}
namespace Ns\Tests;
final class NonexistentUnitTest extends TestCase {}
namespace Ns\Tests\Sub;
final class UnitTest extends TestCase {}
namespace Ns\Tests;
final class UnitTest extends TestCase {}
namespace Ns\Tests\Sub;
use PHPUnit\Framework\Attributes\CoversClass;
#[CoversClass('\Ns\Unit')]
final class UnitTest extends TestCase {}
yield 'Every test has same namespace as tested class' => [
new EveryTestHasSameNamespaceAsCoveredClass($testFiles),
];
abstract class TestCaseBase extends \PHPUnit\Framework\TestCase {}
final class FooTest extends \PHPUnit\Framework\TestCase
final class FooTest extends TestCaseBase
yield 'Every test inherits from TestCase Base Class' => [
new EveryTestInheritsFromTestCaseBaseClass(
$testFiles,
TestCaseBase::class
),
];
class FooTest extends TestCase
final class FooTest extends TestCase
yield 'Every test is final' => [
new EveryTestIsFinal($testFiles),
];