ergebnis/test-util

Provides a helper trait and generic data providers for tests.

Fund package maintenance!
localheinz

1.4.0 2020-11-17 17:57 UTC

README

Integrate Prune Release Renew

Code Coverage Type Coverage

Latest Stable Version Total Downloads

Provides a helper trait and generic data providers for tests.

Installation

Run

$ composer require --dev ergebnis/test-util

Usage

Helper

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 = '')

Data Providers

This package provides the following generic data providers:

Since it is possible to use multiple @dataProvider annotations for test methods, these generic data providers allow for reuse and composition of data providers:

<?php

declare(strict_types=1);

namespace Example\Test;

use PHPUnit\Framework;

final class ExampleTest extends Framework\TestCase
{
    /**
     * @dataProvider \Ergebnis\Test\Util\DataProvider\StringProvider::blank()
     * @dataProvider \Ergebnis\Test\Util\DataProvider\StringProvider::empty()
     *
     * @param string $value
     */
    public function testFromNameRejectsBlankOrEmptyStrings(string $value): void
    {
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionMessage('Value can not be an empty or blank string.');

        UserName::fromString($value);
    }
}

DataProvider\BoolProvider

  • arbitrary() provides true, false
  • false() provides false
  • true() provides true

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\BoolProviderTest.

DataProvider\FloatProvider

  • arbitrary() provides arbitrary floats
  • greaterThanOne() provides ints greater than 1.0
  • greaterThanZero() provides ints greater than 0.0
  • lessThanOne() provides ints less than 1.0
  • lessThanZero() provides ints less than 0.0
  • one() provides 1.0
  • zero() provides 0.0

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\FloatProviderTest.

DataProvider\IntProvider

  • arbitrary() provides arbitrary ints
  • greaterThanOne() provides ints greater than 1
  • greaterThanZero() provides ints greater than 0
  • lessThanOne() provides ints less than 1
  • lessThanZero() provides ints less than 0
  • one() provides 1
  • zero() provides 0

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\IntProviderTest.

DataProvider\NullProvider

  • null() provides null

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\NullProviderTest.

DataProvider\ObjectProvider

  • object() provides an instance of stdClass

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\ObjectProviderTest.

DataProvider\ResourceProvider

  • resource() provides a resource

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\ResourceProviderTest.

DataProvider\StringProvider

  • arbitrary() provides arbitrary strings
  • blank() provides strings consisting of whitespace characters only
  • empty() provides an empty string
  • trimmed() provides non-empty, non-blank strings without leading and trailing whitespace
  • untrimmed() provides non-empty, non-blank strings with additional leading and trailing whitespace
  • withWhitespace() provides non-empty, non-blank, trimmed strings containing whitespace

For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\StringProviderTest.

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.

Please have a look at LICENSE.md.

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.

Curious what I am building?

📬 Subscribe to my list, and I will occasionally send you an email to let you know what I am working on.