Provides a helper trait and generic data providers for tests.
Run
$ composer require --dev ergebnis/test-util
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;
}
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
.
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 = '')
This package provides the following generic data providers:
Ergebnis\Test\Util\DataProvider\BoolProvider
Ergebnis\Test\Util\DataProvider\FloatProvider
Ergebnis\Test\Util\DataProvider\IntProvider
Ergebnis\Test\Util\DataProvider\NullProvider
Ergebnis\Test\Util\DataProvider\ObjectProvider
Ergebnis\Test\Util\DataProvider\StringProvider
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);
}
}
arbitrary()
providestrue
,false
false()
providesfalse
true()
providestrue
For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\BoolProviderTest
.
arbitrary()
provides arbitraryfloat
sgreaterThanOne()
providesint
s greater than1.0
greaterThanZero()
providesint
s greater than0.0
lessThanOne()
providesint
s less than1.0
lessThanZero()
providesint
s less than0.0
one()
provides1.0
zero()
provides0.0
For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\FloatProviderTest
.
arbitrary()
provides arbitraryint
sgreaterThanOne()
providesint
s greater than1
greaterThanZero()
providesint
s greater than0
lessThanOne()
providesint
s less than1
lessThanZero()
providesint
s less than0
one()
provides1
zero()
provides0
For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\IntProviderTest
.
null()
providesnull
For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\NullProviderTest
.
object()
provides an instance ofstdClass
For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\ObjectProviderTest
.
arbitrary()
provides arbitrarystring
sblank()
providesstring
s consisting of whitespace characters onlyempty()
provides an emptystring
trimmed()
provides non-empty, non-blankstrings
without leading and trailing whitespaceuntrimmed()
provides non-empty, non-blankstring
s with additional leading and trailing whitespace
For examples, see Ergebnis\Test\Util\Test\Unit\DataProvider\StringProviderTest
.
Please have a look at CHANGELOG.md
.
Please have a look at CONTRIBUTING.md
.
Please have a look at CODE_OF_CONDUCT.md
.
This package is licensed using the MIT License.
Please have a look at LICENSE.md
.
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.
📬 Subscribe to my list, and I will occasionally send you an email to let you know what I am working on.