2. Constraints¶
This section lists the various constraint methods provided by sub-packages of phptailors/phpunit-extensions. The methods may be added to your test class by including appropriate trait as shown in prerequisite tables below.
arrayValuesEqualTo¶
Package |
phptailors/phpunit-arrays |
Trait |
Synopsis:
function arrayValuesEqualTo(array $expected)
Creates ArrayValuesEqualTo
constraint.
The constraint accepts arrays and ArrayAccess instances having selected values
equal to $expected
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php declare(strict_types=1);
final class arrayValuesEqualToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ArrayValuesEqualToTrait;
public function testSuccess(): void
{
$this->assertThat( ['int' => '123', 1 => null,'foo' => 'FOO'],
$this->arrayValuesEqualTo(['int' => 123, 1 => '' ])
);
}
public function testFailure(): void
{
$this->assertThat( ['int' => '123', 1 => null,'foo' => 'FOO'],
$this->arrayValuesEqualTo(['int' => 123, 1 => '', 'foo' => 'BAR'])
);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.007, Memory: 6.00 MB
There was 1 failure:
1) arrayValuesEqualToTest::testFailure
Failed asserting that array is an array or ArrayAccess with values equal to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'int' => 123
- 1 => ''
- 'foo' => 'BAR'
+ 'int' => '123'
+ 1 => null
+ 'foo' => 'FOO'
)
arrayValuesEqualToTest.php:17
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
|
The constraint may be used recursively, i.e. it may be used to require given nested value to be an array with prescribed properties.
arrayValuesIdenticalTo¶
Package |
phptailors/phpunit-arrays |
Trait |
Synopsis:
function arrayValuesIdenticalTo(array $expected)
Creates ArrayValuesIdenticalTo
constraint.
The constraint accepts arrays and ArrayAccess instances having selected values
identical to $expected
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php declare(strict_types=1);
final class arrayValuesIdenticalToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ArrayValuesIdenticalToTrait;
public function testSuccess(): void
{
$this->assertThat( ['int' => 123, 1 => null, 'foo' => 'FOO'],
$this->arrayValuesIdenticalTo(['int' => 123, 1 => null])
);
}
public function testFailure(): void
{
$this->assertThat( ['int' => 123, 1 => ''],
$this->arrayValuesIdenticalTo(['int' => 123, 1 => null])
);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.007, Memory: 6.00 MB
There was 1 failure:
1) arrayValuesIdenticalToTest::testFailure
Failed asserting that array is an array or ArrayAccess with values identical to specified.
--- Expected
+++ Actual
@@ @@
values (
'int' => 123
- 1 => null
+ 1 => ''
)
arrayValuesIdenticalToTest.php:17
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
|
The constraint may be used recursively, i.e. it may be used to require a nested value to be an array with prescribed values.
classPropertiesEqualTo¶
Package |
phptailors/phpunit-properties |
Trait |
Synopsis:
function classPropertiesEqualTo(array $expected)
Creates ClassPropertiesEqualTo
constraint.
The constraint accepts classes having selected properties equal to
$expected
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php declare(strict_types=1);
final class classPropertiesEqualToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ClassPropertiesEqualToTrait;
public static $attribute = 123;
public static function getValue(): int
{
return 321;
}
public function testSuccess(): void
{
// assert that:
$this->assertThat(self::class, $this->classPropertiesEqualTo([
'attribute' => '123', // - self::$attribute is 123, equals '123' (ok)
'getValue()' => 321, // - self::getValue() is 321 (ok)
]));
}
public function testFailure(): void
{
// assert that:
$this->assertThat(self::class, $this->classPropertiesEqualTo([
'attribute' => '123', // - self::$attribute is 123, equals '123' (ok)
'getValue()' => null, // - self::getValue() is 321, not equals null (fail)
]));
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.007, Memory: 6.00 MB
There was 1 failure:
1) classPropertiesEqualToTest::testFailure
Failed asserting that classPropertiesEqualToTest is a class with properties equal to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'attribute' => '123'
- 'getValue()' => null
+ 'attribute' => 123
+ 'getValue()' => 321
)
classPropertiesEqualToTest.php:27
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
|
The constraint may be used recursively, i.e. it may be used to require given property to be a class with prescribed properties.
classPropertiesIdenticalTo¶
Package |
phptailors/phpunit-properties |
Trait |
Synopsis:
function classPropertiesIdenticalTo(array $expected)
Creates ClassPropertiesIdenticalTo
constraint.
The constraint accepts classes having selected properties identical to
$expected
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php declare(strict_types=1);
final class classPropertiesIdenticalToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ClassPropertiesIdenticalToTrait;
public static $attribute = 123;
public static function getValue(): int
{
return 321;
}
public function testSuccess(): void
{
// assert that:
$this->assertThat(self::class, $this->classPropertiesIdenticalTo([
'attribute' => 123, // - self::$attribute is 123 (ok)
'getValue()' => 321, // - self::getValue() is 321 (ok)
]));
}
public function testFailure(): void
{
// assert that:
$this->assertThat(self::class, $this->classPropertiesIdenticalTo([
'attribute' => '123', // - self::$attribute is 123, not '123' (fail)
'getValue()' => null, // - self::getValue() is 321, not null (fail)
]));
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.007, Memory: 6.00 MB
There was 1 failure:
1) classPropertiesIdenticalToTest::testFailure
Failed asserting that classPropertiesIdenticalToTest is a class with properties identical to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'attribute' => '123'
- 'getValue()' => null
+ 'attribute' => 123
+ 'getValue()' => 321
)
classPropertiesIdenticalToTest.php:27
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
|
The constraint may be used recursively, i.e. it may be used to require given property to be a class with prescribed properties.
extendsClass¶
Package |
phptailors/phpunit-inheritance |
Trait |
Synopsis:
function extendsClass(string $parent)
Creates ExtendsClass
constraint.
The constraint accepts objects (and classes) that extend $parent
class. The
examined $subject
may be an object
or a class name as string
:
if the
$subject
is anobject
, then this object’s class, as returned byget_class($subject)
, is examined against$parent
, the constraint returnstrue
only if the class extends the$parent
class,otherwise, the necessary conditions for the constraint to be satisfied are that
$subject
is a string,class_exists($subject)
istrue
, andthe
$subject
class extends the$parent
class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php declare(strict_types=1);
final class extendsClassTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ExtendsClassTrait;
public function testExtendsClass(): void
{
$this->assertThat(\RuntimeException::class, $this->extendsClass(\Exception::class));
$this->assertThat(new \RuntimeException(), $this->extendsClass(\Exception::class));
}
public function testExtendsClassFailure(): void
{
$this->assertThat(self::class, $this->extendsClass(\Exception::class));
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) extendsClassTest::testExtendsClassFailure
Failed asserting that extendsClassTest extends class Exception.
extendsClassTest.php:15
FAILURES!
Tests: 2, Assertions: 3, Failures: 1.
|
hasMethod¶
Package |
phptailors/phpunit-methods |
Trait |
Synopsis:
function hasMethod(array $methodSpec)
Creates HasMethod
constraint.
The constraint accepts values having a method that matches $methodSpec
.
The $methodSpec
specifies matching method via its name and modifiers
(optional).
Note
The constraint only metches a value being an object, class, trait or interface. For any other values it always returns false.
The syntax for $methodSpec
string is the following
methodSpec := [modifiers "function"] name
| name
name := [_a-zA-Z][_0-9a-zA-Z]*
modifiers := [abstract] [final] [access] [static] ; order arbitrary
abstract := "abstract"
| "!abstract"
final := "final"
| "!final"
access := "public"
| "!public"
| "protected"
| "!protected"
static := "static"
| "!static"
The only allowed combination of abstract
and static
is "!abstract"
+ "!static"
. All other combinations are treated as syntax error.
Presence of a modifier (such as "final"
) requires the modifier to be
present in a matched method. Presence of negated modifier (such as
"!final"
) requires the modifier to be absent in a matched method. Absence
of a modifier in $methodSpec
means that it may be either present or absent
in a matched method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php declare(strict_types=1);
abstract class ClassWithSomeMethods
{
abstract protected function abstractProtectedMethod();
public function publicMethod() { }
}
final class hasMethodTest extends PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\HasMethodTrait;
public function testHasMethod(): void
{
// ClassWithSomeMethods::publicMethod
$this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('publicMethod'));
$this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('public function publicMethod'));
$this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('!abstract !final public function publicMethod'));
// ClassWithSomeMethods::abstractProtectedMethod
$this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('protected function abstractProtectedMethod'));
$this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('abstract function abstractProtectedMethod'));
$this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('abstract protected function abstractProtectedMethod'));
$this->assertThat(ClassWithSomeMethods::class, $this->logicalNot($this->hasMethod('!abstract function abstractProtectedMethod')));
}
public function testHasMethodFailure(): void
{
$this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('!abstract function abstractProtectedMethod'));
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PHPUnit 9.5.27 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.010, Memory: 6.00 MB
There was 1 failure:
1) hasMethodTest::testHasMethodFailure
Failed asserting that 'ClassWithSomeMethods' has !abstract method abstractProtectedMethod().
hasMethodTest.php:31
FAILURES!
Tests: 2, Assertions: 8, Failures: 1.
|
hasPregCaptures¶
Package |
phptailors/phpunit-regexp |
Trait |
Synopsis:
function hasPregCaptures(array $expected)
Creates HasPregCaptures
constraint.
The constraint accepts arrays of matches returned from preg_match()
having
capture groups as specified in $expected
. Only entries present in
$expected
are checked, so $expected = []
accepts any array. Special
values may be used in the expectations:
['foo' => false]
asserts that group'foo'
was not captured,['foo' => true]
asserts that group'foo'
was captured,['foo' => 'FOO']
asserts that group'foo'
was captured and its value equals'FOO'
.
Boolean expectations (['foo' => true]
or ['foo' => false]
) work
properly only with arrays obtained from preg_match()
invoked with
PREG_UNMATCHED_AS_NULL
flag.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php declare(strict_types=1);
final class hasPregCapturesTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\HasPregCapturesTrait;
private $regexp;
private $subject;
private $matches;
public function setUp(): void
{
$this->regexp = '/(?<name>\w+) (?<surname>\w+)(?:, (?<age>\d+))?(?:, (?<city>\w+))?/';
$this->subject = 'John Smith, London';
preg_match($this->regexp, $this->subject, $this->matches, PREG_UNMATCHED_AS_NULL);
}
public function testHasPregCaptures(): void
{
// assert that:
$this->assertThat($this->matches, $this->hasPregCaptures([
'name' => 'John', // - name is 'John' (ok),
'surname' => 'Smith', // - surname is 'Smith' (ok),
'age' => false, // - age is absent (ok),
'city' => true, // - city is present (ok).
]));
}
public function testHasPregCapturesFailure(): void
{
// assert that:
$this->assertThat($this->matches, $this->hasPregCaptures([
'name' => 'John', // - name is 'John' (ok),
'surname' => 'Brown', // - surname is 'Brown' (fail),
'age' => true, // - age is present (fail),
'city' => false, // - city is absent (fail).
]));
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) hasPregCapturesTest::testHasPregCapturesFailure
Failed asserting that array has expected PCRE capture groups.
--- Expected
+++ Actual
@@ @@
Array &0 (
'name' => 'John'
- 'surname' => 'Brown'
- 'age' => true
- 'city' => false
+ 'surname' => 'Smith'
+ 'age' => null
+ 'city' => 'London'
)
hasPregCapturesTest.php:33
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
|
implementsInterface¶
Package |
phptailors/phpunit-inheritance |
Trait |
Synopsis:
function implementsInterface(array $interface)
Creates ImplementsInterface
constraint.
The constraint accepts objects (and classes/interfaces) that implement given
$interface
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php declare(strict_types=1);
final class implementsInterfaceTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ImplementsInterfaceTrait;
public function testImplementsInterface(): void
{
$this->assertThat(\RuntimeException::class, $this->implementsInterface(\Throwable::class));
$this->assertThat(new \RuntimeException(), $this->implementsInterface(\Throwable::class));
}
public function testImplementsInterfaceFailure(): void
{
$this->assertThat(self::class, $this->implementsInterface(\Throwable::class));
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) implementsInterfaceTest::testImplementsInterfaceFailure
Failed asserting that implementsInterfaceTest implements interface Throwable.
implementsInterfaceTest.php:15
FAILURES!
Tests: 2, Assertions: 3, Failures: 1.
|
ksortedArrayEqualTo¶
Package |
phptailors/phpunit-arrays |
Trait |
Synopsis:
function ksortedArrayEqualTo(array $expected)
Creates KsortedArrayEqualTo
constraint.
The constraint accepts arrays that are equal to $expected
when key-sorted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php declare(strict_types=1);
final class ksortedArrayEqualToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\KsortedArrayEqualToTrait;
public function testSuccess(): void
{
$this->assertThat( ['int' => '123', 1 => null,'foo' => 'FOO'],
$this->ksortedArrayEqualTo(['foo' => 'FOO', 'int' => 123, 1 => '' ])
);
}
public function testFailure(): void
{
$this->assertThat( ['int' => '123', 1 => '', 'foo' => 'FOO'],
$this->ksortedArrayEqualTo(['int' => 123, 1 => '', 'foo' => 'BAR'])
);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.008, Memory: 6.00 MB
There was 1 failure:
1) ksortedArrayEqualToTest::testFailure
Failed asserting that array is an array equal to specified one when ksorted.
--- Expected
+++ Actual
@@ @@
Array &0 (
- 'foo' => 'BAR'
- 'int' => 123
+ 'foo' => 'FOO'
+ 'int' => '123'
1 => ''
)
ksortedArrayEqualToTest.php:17
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
|
ksortedArrayIdenticalTo¶
Package |
phptailors/phpunit-arrays |
Trait |
Synopsis:
function ksortedArrayIdenticalTo(array $expected)
Creates KsortedArrayIdenticalTo
constraint.
The constraint accepts arrays identical to $expected
when key-sorted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php declare(strict_types=1);
final class ksortedArrayIdenticalToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\KsortedArrayIdenticalToTrait;
public function testSuccess(): void
{
$this->assertThat( ['int' => 123, 1 => null, 'foo' => 'FOO'],
$this->ksortedArrayIdenticalTo(['foo' => 'FOO', 'int' => 123, 1 => null])
);
}
public function testFailure(): void
{
$this->assertThat( ['int' => 123, 1 => '', 'foo' => 'FOO'],
$this->ksortedArrayIdenticalTo(['int' => 123, 1 => null, 'foo' => 'FOO'])
);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) ksortedArrayIdenticalToTest::testFailure
Failed asserting that array is an array identical to specified one when ksorted.
--- Expected
+++ Actual
@@ @@
Array &0 (
'foo' => 'FOO'
'int' => 123
- 1 => null
+ 1 => ''
)
ksortedArrayIdenticalToTest.php:17
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
|
objectPropertiesEqualTo¶
Package |
phptailors/phpunit-properties |
Trait |
Synopsis:
function objectPropertiesEqualTo(array $expected)
Creates ObjectPropertiesEqualTo
constraint.
The constraint accepts objects having selected properties equal to
$expected
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php declare(strict_types=1);
final class objectPropertiesEqualToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ObjectPropertiesEqualToTrait;
public $attribute = 123;
public function getValue(): int
{
return 321;
}
public function testSuccess(): void
{
// assert that:
$this->assertThat($this, $this->objectPropertiesEqualTo([
'attribute' => '123', // - $this->attribute is 123, equals '123' (ok)
'getValue()' => 321, // - $this->getValue() is 321 (ok)
]));
}
public function testFailure(): void
{
// assert that:
$this->assertThat($this, $this->objectPropertiesEqualTo([
'attribute' => '123', // - $this->attribute is 123, equals '123' (ok)
'getValue()' => null, // - $this->getValue() is 321, not equals null (fail)
]));
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.007, Memory: 6.00 MB
There was 1 failure:
1) objectPropertiesEqualToTest::testFailure
Failed asserting that object objectPropertiesEqualToTest is an object with properties equal to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'attribute' => '123'
- 'getValue()' => null
+ 'attribute' => 123
+ 'getValue()' => 321
)
objectPropertiesEqualToTest.php:27
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
|
The constraint may be used recursively, i.e. it may be used to require given property to be an object with prescribed properties.
objectPropertiesIdenticalTo¶
Package |
phptailors/phpunit-properties |
Trait |
Synopsis:
function objectPropertiesIdenticalTo(array $expected)
Creates ObjectPropertiesIdenticalTo
constraint.
The constraint accepts objects having selected properties identical to
$expected
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php declare(strict_types=1);
final class objectPropertiesIdenticalToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ObjectPropertiesIdenticalToTrait;
public $attribute = 123;
public function getValue(): int
{
return 321;
}
public function testSuccess(): void
{
// assert that:
$this->assertThat($this, $this->objectPropertiesIdenticalTo([
'attribute' => 123, // - $this->attribute is 123 (ok)
'getValue()' => 321, // - $this->getValue() is 321 (ok)
]));
}
public function testFailure(): void
{
// assert that:
$this->assertThat($this, $this->objectPropertiesIdenticalTo([
'attribute' => '123', // - $this->attribute is 123, not '123' (fail)
'getValue()' => null, // - $this->getValue() is 321, not null (fail)
]));
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.007, Memory: 6.00 MB
There was 1 failure:
1) objectPropertiesIdenticalToTest::testFailure
Failed asserting that object objectPropertiesIdenticalToTest is an object with properties identical to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'attribute' => '123'
- 'getValue()' => null
+ 'attribute' => 123
+ 'getValue()' => 321
)
objectPropertiesIdenticalToTest.php:27
FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
|
The constraint may be used recursively, i.e. it may be used to require given property to be an object with prescribed properties.
usesTrait¶
Package |
phptailors/phpunit-inheritance |
Trait |
Synopsis:
function usesTrait(array $trait)
Creates UsesTrait
constraint.
The constraint accepts objects (and classes) that use given $trait
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php declare(strict_types=1);
trait ExampleTraitForUsesTraitTest
{
}
final class UsesTraitTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\UsesTraitTrait;
use ExampleTraitForUsesTraitTest;
public function testUsesTrait(): void
{
$this->assertThat(self::class, $this->usesTrait(ExampleTraitForUsesTraitTest::class));
$this->assertThat($this, $this->usesTrait(ExampleTraitForUsesTraitTest::class));
}
public function testUsesTraitFailure(): void
{
$this->assertThat(\RuntimeException::class, $this->usesTrait(ExampleTraitForUsesTraitTest::class));
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) UsesTraitTest::testUsesTraitFailure
Failed asserting that RuntimeException uses trait ExampleTraitForUsesTraitTest.
usesTraitTest.php:20
FAILURES!
Tests: 2, Assertions: 3, Failures: 1.
|