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

Prerequisites for arrayValuesEqualTo()

Package

phptailors/phpunit-arrays

Trait

ArrayValuesEqualToTrait

Synopsis:

function arrayValuesEqualTo(array $expected)

Creates ArrayValuesEqualTo constraint.

The constraint accepts arrays and ArrayAccess instances having selected values equal to $expected.

Usage of arrayValuesEqualTo()
 1<?php declare(strict_types=1);
 2
 3final class arrayValuesEqualToTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ArrayValuesEqualToTrait;
 6
 7    public function testSuccess(): void
 8    {
 9        $this->assertThat(            ['int' => '123', 1 => null,'foo' => 'FOO'],
10            $this->arrayValuesEqualTo(['int'  => 123,  1 => '' ])
11        );
12    }
13
14    public function testFailure(): void
15    {
16        $this->assertThat(            ['int' => '123', 1 => null,'foo' => 'FOO'],
17            $this->arrayValuesEqualTo(['int'  => 123,  1 => '',  'foo' => 'BAR'])
18        );
19    }
20}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.007, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) arrayValuesEqualToTest::testFailure
13Failed asserting that array is an array or ArrayAccess with values equal to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Values\ExpectedValuesSelection Object #447 (
18-    'int' => 123,
19-    1 => '',
20-    'foo' => 'BAR',
21+Tailors\PHPUnit\Values\ActualValues Object #444 (
22+    'int' => '123',
23+    1 => null,
24+    'foo' => 'FOO',
25 )
26
27arrayValuesEqualToTest.php:16
28
29FAILURES!
30Tests: 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

Prerequisites for arrayValuesIdenticalTo()

Package

phptailors/phpunit-arrays

Trait

ArrayValuesIdenticalToTrait

Synopsis:

function arrayValuesIdenticalTo(array $expected)

Creates ArrayValuesIdenticalTo constraint.

The constraint accepts arrays and ArrayAccess instances having selected values identical to $expected.

Usage of arrayValuesIdenticalTo()
 1<?php declare(strict_types=1);
 2
 3final class arrayValuesIdenticalToTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ArrayValuesIdenticalToTrait;
 6
 7    public function testSuccess(): void
 8    {
 9        $this->assertThat(                ['int' => 123, 1 => null, 'foo' => 'FOO'],
10            $this->arrayValuesIdenticalTo(['int' => 123, 1 => null])
11        );
12    }
13
14    public function testFailure(): void
15    {
16        $this->assertThat(                ['int' => 123, 1 => ''],
17            $this->arrayValuesIdenticalTo(['int' => 123, 1 => null])
18        );
19    }
20}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.007, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) arrayValuesIdenticalToTest::testFailure
13Failed asserting that array is an array or ArrayAccess with values identical to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Values\ExpectedValuesSelection Object #447 (
18+Tailors\PHPUnit\Values\ActualValues Object #444 (
19     'int' => 123,
20-    1 => null,
21+    1 => '',
22 )
23
24arrayValuesIdenticalToTest.php:16
25
26FAILURES!
27Tests: 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

Prerequisites for classPropertiesEqualTo()

Package

phptailors/phpunit-properties

Trait

ClassPropertiesEqualToTrait

Synopsis:

function classPropertiesEqualTo(array $expected)

Creates ClassPropertiesEqualTo constraint.

The constraint accepts classes having selected properties equal to $expected.

Usage of classPropertiesEqualTo()
 1<?php declare(strict_types=1);
 2
 3final class classPropertiesEqualToTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ClassPropertiesEqualToTrait;
 6
 7    public static $attribute = 123;
 8
 9    public static function getValue(): int
10    {
11        return 321;
12    }
13
14    public function testSuccess(): void
15    {
16        // assert that:
17        $this->assertThat(self::class, $this->classPropertiesEqualTo([
18            'attribute'  => '123', // - self::$attribute is 123, equals '123' (ok)
19            'getValue()' => 321,   // - self::getValue() is 321 (ok)
20        ]));
21    }
22
23    public function testFailure(): void
24    {
25        // assert that:
26        $this->assertThat(self::class, $this->classPropertiesEqualTo([
27            'attribute'  => '123', // - self::$attribute is 123, equals '123' (ok)
28            'getValue()' => null,  // - self::getValue() is 321, not equals null (fail)
29        ]));
30    }
31}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.007, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) classPropertiesEqualToTest::testFailure
13Failed asserting that classPropertiesEqualToTest is a class with properties equal to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Values\ExpectedValuesSelection Object #451 (
18-    'attribute' => '123',
19-    'getValue()' => null,
20+Tailors\PHPUnit\Values\ActualValues Object #448 (
21+    'attribute' => 123,
22+    'getValue()' => 321,
23 )
24
25classPropertiesEqualToTest.php:26
26
27FAILURES!
28Tests: 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

Prerequisites for classPropertiesIdenticalTo()

Package

phptailors/phpunit-properties

Trait

ClassPropertiesIdenticalToTrait

Synopsis:

function classPropertiesIdenticalTo(array $expected)

Creates ClassPropertiesIdenticalTo constraint.

The constraint accepts classes having selected properties identical to $expected.

Usage of classPropertiesIdenticalTo()
 1<?php declare(strict_types=1);
 2
 3final class classPropertiesIdenticalToTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ClassPropertiesIdenticalToTrait;
 6
 7    public static $attribute = 123;
 8
 9    public static function getValue(): int
10    {
11        return 321;
12    }
13
14    public function testSuccess(): void
15    {
16        // assert that:
17        $this->assertThat(self::class, $this->classPropertiesIdenticalTo([
18            'attribute'  => 123,   // - self::$attribute is 123 (ok)
19            'getValue()' => 321,   // - self::getValue() is 321 (ok)
20        ]));
21    }
22
23    public function testFailure(): void
24    {
25        // assert that:
26        $this->assertThat(self::class, $this->classPropertiesIdenticalTo([
27            'attribute'  => '123', // - self::$attribute is 123, not '123' (fail)
28            'getValue()' => null,  // - self::getValue() is 321, not null (fail)
29        ]));
30    }
31}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.007, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) classPropertiesIdenticalToTest::testFailure
13Failed asserting that classPropertiesIdenticalToTest is a class with properties identical to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Values\ExpectedValuesSelection Object #451 (
18-    'attribute' => '123',
19-    'getValue()' => null,
20+Tailors\PHPUnit\Values\ActualValues Object #448 (
21+    'attribute' => 123,
22+    'getValue()' => 321,
23 )
24
25classPropertiesIdenticalToTest.php:26
26
27FAILURES!
28Tests: 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

Prerequisites for extendsClass()

Package

phptailors/phpunit-inheritance

Trait

ExtendsClassTrait

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 an object, then this object’s class, as returned by get_class($subject), is examined against $parent, the constraint returns true 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) is true, and

    • the $subject class extends the $parent class.

Usage of extendsClass()
 1<?php declare(strict_types=1);
 2
 3final class extendsClassTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ExtendsClassTrait;
 6
 7    public function testExtendsClass(): void
 8    {
 9        $this->assertThat(\RuntimeException::class, $this->extendsClass(\Exception::class));
10        $this->assertThat(new \RuntimeException(), $this->extendsClass(\Exception::class));
11    }
12
13    public function testExtendsClassFailure(): void
14    {
15        $this->assertThat(self::class, $this->extendsClass(\Exception::class));
16    }
17}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.006, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) extendsClassTest::testExtendsClassFailure
13Failed asserting that extendsClassTest extends class Exception.
14
15extendsClassTest.php:15
16
17FAILURES!
18Tests: 2, Assertions: 3, Failures: 1.

hasMethod

Prerequisites for hasMethod()

Package

phptailors/phpunit-methods

Trait

HasMethodTrait

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 matches 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 final is "!abstract" + "!final". 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.

Usage of hasMethod()
 1<?php declare(strict_types=1);
 2
 3abstract class ClassWithSomeMethods
 4{
 5    abstract protected function abstractProtectedMethod();
 6    public function publicMethod() { }
 7}
 8
 9final class hasMethodTest extends PHPUnit\Framework\TestCase
10{
11    use \Tailors\PHPUnit\HasMethodTrait;
12
13    public function testHasMethod(): void
14    {
15        // ClassWithSomeMethods::publicMethod
16        $this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('publicMethod'));
17        $this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('public function publicMethod'));
18        $this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('!abstract !final public function publicMethod'));
19
20        // ClassWithSomeMethods::abstractProtectedMethod
21        $this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('protected function abstractProtectedMethod'));
22        $this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('abstract function abstractProtectedMethod'));
23        $this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('abstract protected function abstractProtectedMethod'));
24
25
26        $this->assertThat(ClassWithSomeMethods::class, $this->logicalNot($this->hasMethod('!abstract function abstractProtectedMethod')));
27    }
28
29    public function testHasMethodFailure(): void
30    {
31        $this->assertThat(ClassWithSomeMethods::class, $this->hasMethod('!abstract function abstractProtectedMethod'));
32    }
33}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.012, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) hasMethodTest::testHasMethodFailure
13Failed asserting that 'ClassWithSomeMethods' has !abstract method abstractProtectedMethod().
14
15hasMethodTest.php:31
16
17FAILURES!
18Tests: 2, Assertions: 8, Failures: 1.

hasPregCaptures

Prerequisites for hasPregCaptures()

Package

phptailors/phpunit-regexp

Trait

HasPregCapturesTrait

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.

Usage of hasPregCaptures()
 1<?php declare(strict_types=1);
 2
 3final class hasPregCapturesTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\HasPregCapturesTrait;
 6
 7    private $regexp;
 8    private $subject;
 9    private $matches;
10
11    public function setUp(): void
12    {
13        $this->regexp  = '/(?<name>\w+) (?<surname>\w+)(?:, (?<age>\d+))?(?:, (?<city>\w+))?/';
14        $this->subject = 'John Smith, London';
15        preg_match($this->regexp, $this->subject, $this->matches, PREG_UNMATCHED_AS_NULL);
16    }
17
18    public function testHasPregCaptures(): void
19    {
20        // assert that:
21        $this->assertThat($this->matches, $this->hasPregCaptures([
22            'name'    => 'John',  // - name is 'John' (ok),
23            'surname' => 'Smith', // - surname is 'Smith' (ok),
24            'age'     => false,   // - age is absent (ok),
25            'city'    => true,    // - city is present (ok).
26        ]));
27    }
28
29    public function testHasPregCapturesFailure(): void
30    {
31        // assert that:
32        $this->assertThat($this->matches, $this->hasPregCaptures([
33            'name'    => 'John',  // - name is 'John' (ok),
34            'surname' => 'Brown', // - surname is 'Brown' (fail),
35            'age'     => true,    // - age is present (fail),
36            'city'    => false,   // - city is absent (fail).
37        ]));
38    }
39}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.008, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) hasPregCapturesTest::testHasPregCapturesFailure
13Failed asserting that array has expected PCRE capture groups.
14--- Expected
15+++ Actual
16@@ @@
17 Array &0 [
18     'name' => 'John',
19-    'surname' => 'Brown',
20-    'age' => true,
21-    'city' => false,
22+    'surname' => 'Smith',
23+    'age' => null,
24+    'city' => 'London',
25 ]
26
27hasPregCapturesTest.php:32
28
29FAILURES!
30Tests: 2, Assertions: 2, Failures: 1.

implementsInterface

Prerequisites for implementsInterface()

Package

phptailors/phpunit-inheritance

Trait

ImplementsInterfaceTrait

Synopsis:

function implementsInterface(array $interface)

Creates ImplementsInterface constraint.

The constraint accepts objects (and classes/interfaces) that implement given $interface.

Usage of implementsInterface()
 1<?php declare(strict_types=1);
 2
 3final class implementsInterfaceTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ImplementsInterfaceTrait;
 6
 7    public function testImplementsInterface(): void
 8    {
 9        $this->assertThat(\RuntimeException::class, $this->implementsInterface(\Throwable::class));
10        $this->assertThat(new \RuntimeException(), $this->implementsInterface(\Throwable::class));
11    }
12
13    public function testImplementsInterfaceFailure(): void
14    {
15        $this->assertThat(self::class, $this->implementsInterface(\Throwable::class));
16    }
17}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.007, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) implementsInterfaceTest::testImplementsInterfaceFailure
13Failed asserting that implementsInterfaceTest implements interface Throwable.
14
15implementsInterfaceTest.php:15
16
17FAILURES!
18Tests: 2, Assertions: 3, Failures: 1.

ksortedArrayEqualTo

Prerequisites for ksortedArrayEqualTo()

Package

phptailors/phpunit-arrays

Trait

KsortedArrayEqualToTrait

Synopsis:

function ksortedArrayEqualTo(array $expected)

Creates KsortedArrayEqualTo constraint.

The constraint accepts arrays that are equal to $expected when key-sorted.

Usage of ksortedArrayEqualTo()
 1<?php declare(strict_types=1);
 2
 3final class ksortedArrayEqualToTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\KsortedArrayEqualToTrait;
 6
 7    public function testSuccess(): void
 8    {
 9        $this->assertThat(             ['int' => '123', 1 => null,'foo' => 'FOO'],
10            $this->ksortedArrayEqualTo(['foo' => 'FOO', 'int' => 123, 1 => '' ])
11        );
12    }
13
14    public function testFailure(): void
15    {
16        $this->assertThat(             ['int' => '123', 1 => '', 'foo' => 'FOO'],
17            $this->ksortedArrayEqualTo(['int'  => 123,  1 => '', 'foo' => 'BAR'])
18        );
19    }
20}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.007, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) ksortedArrayEqualToTest::testFailure
13Failed asserting that array is an array equal to specified one when ksorted.
14--- Expected
15+++ Actual
16@@ @@
17 Array &0 [
18     1 => '',
19-    'foo' => 'BAR',
20-    'int' => 123,
21+    'foo' => 'FOO',
22+    'int' => '123',
23 ]
24
25ksortedArrayEqualToTest.php:16
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.

ksortedArrayIdenticalTo

Prerequisites for ksortedArrayIdenticalTo()

Package

phptailors/phpunit-arrays

Trait

KsortedArrayIdenticalToTrait

Synopsis:

function ksortedArrayIdenticalTo(array $expected)

Creates KsortedArrayIdenticalTo constraint.

The constraint accepts arrays identical to $expected when key-sorted.

Usage of ksortedArrayIdenticalTo()
 1<?php declare(strict_types=1);
 2
 3final class ksortedArrayIdenticalToTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\KsortedArrayIdenticalToTrait;
 6
 7    public function testSuccess(): void
 8    {
 9        $this->assertThat(                 ['int' => 123, 1 => null, 'foo' => 'FOO'],
10            $this->ksortedArrayIdenticalTo(['foo' => 'FOO', 'int' => 123, 1 => null])
11        );
12    }
13
14    public function testFailure(): void
15    {
16        $this->assertThat(                 ['int' => 123, 1 => '', 'foo' => 'FOO'],
17            $this->ksortedArrayIdenticalTo(['int' => 123, 1 => null, 'foo' => 'FOO'])
18        );
19    }
20}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.014, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) ksortedArrayIdenticalToTest::testFailure
13Failed asserting that array is an array identical to specified one when ksorted.
14--- Expected
15+++ Actual
16@@ @@
17 Array &0 [
18-    1 => null,
19+    1 => '',
20     'foo' => 'FOO',
21     'int' => 123,
22 ]
23
24ksortedArrayIdenticalToTest.php:16
25
26FAILURES!
27Tests: 2, Assertions: 2, Failures: 1.

objectPropertiesEqualTo

Prerequisites for objectPropertiesEqualTo()

Package

phptailors/phpunit-properties

Trait

ObjectPropertiesEqualToTrait

Synopsis:

function objectPropertiesEqualTo(array $expected)

Creates ObjectPropertiesEqualTo constraint.

The constraint accepts objects having selected properties equal to $expected.

Usage of objectPropertiesEqualTo()
 1<?php declare(strict_types=1);
 2
 3final class objectPropertiesEqualToTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ObjectPropertiesEqualToTrait;
 6
 7    public $attribute = 123;
 8
 9    public function getValue(): int
10    {
11        return 321;
12    }
13
14    public function testSuccess(): void
15    {
16        // assert that:
17        $this->assertThat($this, $this->objectPropertiesEqualTo([
18            'attribute'  => '123', // - $this->attribute is 123, equals '123' (ok)
19            'getValue()' => 321,   // - $this->getValue() is 321 (ok)
20        ]));
21    }
22
23    public function testFailure(): void
24    {
25        // assert that:
26        $this->assertThat($this, $this->objectPropertiesEqualTo([
27            'attribute'  => '123', // - $this->attribute is 123, equals '123' (ok)
28            'getValue()' => null,  // - $this->getValue() is 321, not equals null (fail)
29        ]));
30    }
31}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.007, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) objectPropertiesEqualToTest::testFailure
13Failed asserting that object objectPropertiesEqualToTest is an object with properties equal to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Values\ExpectedValuesSelection Object #451 (
18-    'attribute' => '123',
19-    'getValue()' => null,
20+Tailors\PHPUnit\Values\ActualValues Object #448 (
21+    'attribute' => 123,
22+    'getValue()' => 321,
23 )
24
25objectPropertiesEqualToTest.php:26
26
27FAILURES!
28Tests: 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

Prerequisites for objectPropertiesIdenticalTo()

Package

phptailors/phpunit-properties

Trait

ObjectPropertiesIdenticalToTrait

Synopsis:

function objectPropertiesIdenticalTo(array $expected)

Creates ObjectPropertiesIdenticalTo constraint.

The constraint accepts objects having selected properties identical to $expected.

Usage of objectPropertiesIdenticalTo()
 1<?php declare(strict_types=1);
 2
 3final class objectPropertiesIdenticalToTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ObjectPropertiesIdenticalToTrait;
 6
 7    public $attribute = 123;
 8
 9    public function getValue(): int
10    {
11        return 321;
12    }
13
14    public function testSuccess(): void
15    {
16        // assert that:
17        $this->assertThat($this, $this->objectPropertiesIdenticalTo([
18            'attribute'  => 123,   // - $this->attribute is 123 (ok)
19            'getValue()' => 321,   // - $this->getValue() is 321 (ok)
20        ]));
21    }
22
23    public function testFailure(): void
24    {
25        // assert that:
26        $this->assertThat($this, $this->objectPropertiesIdenticalTo([
27            'attribute'  => '123', // - $this->attribute is 123, not '123' (fail)
28            'getValue()' => null,  // - $this->getValue() is 321, not null (fail)
29        ]));
30    }
31}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.007, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) objectPropertiesIdenticalToTest::testFailure
13Failed asserting that object objectPropertiesIdenticalToTest is an object with properties identical to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Values\ExpectedValuesSelection Object #451 (
18-    'attribute' => '123',
19-    'getValue()' => null,
20+Tailors\PHPUnit\Values\ActualValues Object #448 (
21+    'attribute' => 123,
22+    'getValue()' => 321,
23 )
24
25objectPropertiesIdenticalToTest.php:26
26
27FAILURES!
28Tests: 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

Prerequisites for usesTrait()

Package

phptailors/phpunit-inheritance

Trait

UsesTraitTrait

Synopsis:

function usesTrait(array $trait)

Creates UsesTrait constraint.

The constraint accepts objects (and classes) that use given $trait.

Usage of usesTrait()
 1<?php declare(strict_types=1);
 2
 3trait ExampleTraitForUsesTraitTest
 4{
 5}
 6
 7final class UsesTraitTest extends \PHPUnit\Framework\TestCase
 8{
 9    use \Tailors\PHPUnit\UsesTraitTrait;
10    use ExampleTraitForUsesTraitTest;
11
12    public function testUsesTrait(): void
13    {
14        $this->assertThat(self::class, $this->usesTrait(ExampleTraitForUsesTraitTest::class));
15        $this->assertThat($this, $this->usesTrait(ExampleTraitForUsesTraitTest::class));
16    }
17
18    public function testUsesTraitFailure(): void
19    {
20        $this->assertThat(\RuntimeException::class, $this->usesTrait(ExampleTraitForUsesTraitTest::class));
21    }
22}
 1PHPUnit 10.5.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.13
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.006, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) UsesTraitTest::testUsesTraitFailure
13Failed asserting that RuntimeException uses trait ExampleTraitForUsesTraitTest.
14
15usesTraitTest.php:20
16
17FAILURES!
18Tests: 2, Assertions: 3, Failures: 1.