Skip to content
👓 Provides utilities for tests.
PHP Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.dependabot
.github
src
test
.editorconfig
.gitattributes
.gitignore
.php_cs
.php_cs.fixture
CHANGELOG.md
LICENSE
Makefile
README.md
composer-require-checker.json
composer.json
composer.lock
infection.json
phpstan-baseline.neon
phpstan.neon
psalm-baseline.xml
psalm.xml

README.md

test-util

Continuous Deployment Continuous Integration Code Coverage Type Coverage Latest Stable Version Total Downloads

Provides utilities for tests.

Installation

Run

$ composer require --dev ergebnis/test-util

Usage

Import the Ergebnis\Test\Util\Helper trait into your test class:

<?php

declare(strict_types=1);

namespace Foo\Bar\Test\Unit;

use Ergebnis\Test\Util\Helper;
use PHPUnit\Framework;

final class BazTest extends Framework\TestCase
{
    use Helper;
}

Easy access to localized instances of Faker\Generator

The Helper trait provides a method to fetch a localized instance of Faker\Generator:

  • faker(string $locale = 'en_US') : \Faker\Generator
<?php

declare(strict_types=1);

namespace Example\Test\Unit;

use Ergebnis\Test\Util\Helper;
use Example\Player;
use PHPUnit\Framework;

final class PlayerTest extends Framework\TestCase
{
    use Helper;

    public function testConstructorSetsValues(): void
    {
        $name = self::faker()->firstName;

        $player = new Player($name);

        $this->assertSame($name, $player->firstName());
    }
}

For reference, see fzaninotto/faker.

Additional Assertions

In addition to the assertions made available by extending from PHPUnit\Framework\TestCase, the Helper trait provides the following assertions:

  • assertClassesAreAbstractOrFinal(string $directory, array $excludeClassNames = [])
  • assertClassesHaveTests(string $directory, string $namespace, string $testNamespace, array $excludeClassyNames = [])
  • assertClassExists(string $className)
  • assertClassExtends(string $parentClassName, string $className)
  • assertClassImplementsInterface(string $interfaceName, string $className)
  • assertClassIsAbstract(string $className)
  • assertClassIsFinal(string $className)
  • assertClassSatisfiesSpecification(callable $specification, string $className, string $message = '')
  • assertClassUsesTrait(string $traitName, string $className)
  • assertClassyConstructsSatisfySpecification(callable $specification, string $directory, array $excludeClassNames = [], $message = '')
  • assertInterfaceExists(string $interfaceName)
  • assertInterfaceExtends(string $parentInterfaceName, string $interfaceName)
  • assertInterfaceSatisfiesSpecification(callable $specification, string $interfaceName, string $message = '')
  • assertTraitExists(string $traitName)
  • assertTraitSatisfiesSpecification(callable $specification, string $traitName, string $message = '')

Changelog

Please have a look at CHANGELOG.md.

Contributing

Please have a look at CONTRIBUTING.md.

Code of Conduct

Please have a look at CODE_OF_CONDUCT.md.

License

This package is licensed using the MIT License.

Credits

The SrcCodeTest in this and other projects I maintain or contribute to is inspired by ProjectCodeTest in friends-of-php/php-cs-fixer, and was initially created by Dariusz Rumiński.

You can’t perform that action at this time.