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 objects having values under certain keys equal to these specified in $expected. The constraint uses operator == for comparison.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 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\Arrays\ExpectedArrayValues Object #447 (
18-    'int' => 123,
19-    1 => '',
20-    'foo' => 'BAR',
21+Tailors\PHPUnit\Arrays\ActualArrayValues Object #444 (
22+    'int' => '123',
23+    1 => null,
24+    'foo' => 'FOO',
25 )
26
27arrayValuesEqualToTest.php:16
28
29FAILURES!
30Tests: 2, Assertions: 2, Failures: 1.

Since versions *.1 (1.1, 2.1, 3.1, …) it’s possible to nest recursively specification of array values. For more details, see arrayValues().

 1<?php declare(strict_types=1);
 2
 3final class arrayValuesEqualToRecursiveTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ArrayValuesEqualToTrait;
 6    use \Tailors\PHPUnit\ArrayValuesTrait;
 7
 8    private $array = [
 9        'int' => '123',
10        'sub' => [
11            'int' => 321,
12            'bar' => 'BAR',
13        ],
14        'foo' => 'FOO'
15    ];
16
17    public function testSuccess(): void
18    {
19        $this->assertThat(
20            $this->array,
21            $this->arrayValuesEqualTo([
22                'sub' => $this->arrayValues([
23                    'int' => '321'
24                ]),
25                'int'  => 123,
26            ])
27        );
28    }
29
30    public function testFailure(): void
31    {
32        $this->assertThat(
33            $this->array,
34            $this->arrayValuesEqualTo([
35                'sub' => [
36                    'int' => '321'
37                ],
38                'int'  => 123,
39            ])
40        );
41    }
42}
 1PHPUnit 10.5.63 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.30
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.016, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) arrayValuesEqualToRecursiveTest::testFailure
13Failed asserting that array is an array or ArrayAccess with values equal to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Arrays\ExpectedArrayValues Object #452 (
18+Tailors\PHPUnit\Arrays\ActualArrayValues Object #343 (
19     'sub' => Array &0 [
20-        'int' => '321',
21+        'int' => 321,
22+        'bar' => 'BAR',
23     ],
24-    'int' => 123,
25+    'int' => '123',
26 )
27
28arrayValuesEqualToRecursiveTest.php:32
29
30FAILURES!
31Tests: 2, Assertions: 2, Failures: 1.

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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 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\Arrays\ExpectedArrayValues Object #447 (
18+Tailors\PHPUnit\Arrays\ActualArrayValues Object #444 (
19     'int' => 123,
20-    1 => null,
21+    1 => '',
22 )
23
24arrayValuesIdenticalToTest.php:16
25
26FAILURES!
27Tests: 2, Assertions: 2, Failures: 1.

Since versions *.1 (1.1, 2.1, 3.1, …) it’s possible to nest recursively specification of array values. For more details, see arrayValues().

Usage of arrayValuesIdenticalTo() with nested selectors
 1<?php declare(strict_types=1);
 2
 3final class arrayValuesIdenticalToRecursiveTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ArrayValuesIdenticalToTrait;
 6    use \Tailors\PHPUnit\ArrayValuesTrait;
 7
 8    private $array = [
 9        'int' => 123,
10        'sub' => [
11            'int' => 321,
12            'bar' => 'BAR',
13        ],
14        'foo' => 'FOO'
15    ];
16
17    public function testSuccess(): void
18    {
19        $this->assertThat(
20            $this->array,
21            $this->arrayValuesIdenticalTo([
22                'sub' => $this->arrayValues([
23                    'int' => 321
24                ]),
25                'int'  => 123,
26            ])
27        );
28    }
29
30    public function testFailure(): void
31    {
32        $this->assertThat(
33            $this->array,
34            $this->arrayValuesIdenticalTo([
35                'sub' => [
36                    'int' => 321
37                ],
38                'int'  => 123,
39            ])
40        );
41    }
42}
 1PHPUnit 10.5.63 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.30
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.016, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) arrayValuesIdenticalToRecursiveTest::testFailure
13Failed asserting that array is an array or ArrayAccess with values identical to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Arrays\ExpectedArrayValues Object #426 (
18+Tailors\PHPUnit\Arrays\ActualArrayValues Object #343 (
19     'sub' => Array &0 [
20         'int' => 321,
21+        'bar' => 'BAR',
22     ],
23     'int' => 123,
24 )
25
26arrayValuesIdenticalToRecursiveTest.php:32
27
28FAILURES!
29Tests: 2, Assertions: 2, Failures: 1.

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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 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\Properties\ExpectedClassProperties Object #451 (
18-    'attribute' => '123',
19-    'getValue()' => null,
20+Tailors\PHPUnit\Properties\ActualClassProperties Object #448 (
21+    'attribute' => 123,
22+    'getValue()' => 321,
23 )
24
25classPropertiesEqualToTest.php:26
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.

Since versions *.1 (1.1, 2.1, 3.1, …) it’s possible to nest recursively specifications of class properties. For more details, see arrayValues().

 1<?php declare(strict_types=1);
 2
 3final class classPropertiesEqualToRecursiveTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ClassPropertiesEqualToTrait;
 6    use \Tailors\PHPUnit\ClassPropertiesTrait;
 7
 8    public static $attribute = 123;
 9    public static $nested = self::class;
10    public static $other = 321;
11
12    public function testSuccess(): void
13    {
14        $this->assertThat(self::class, $this->classPropertiesEqualTo([
15            'attribute'  => '123',
16            'nested' => $this->classProperties([
17                'other' => '321',
18            ])
19        ]));
20    }
21
22    public function testFailure(): void
23    {
24        $this->assertThat(self::class, $this->classPropertiesEqualTo([
25            'attribute'  => '123',
26            'nested' => [
27                'other' => '321',
28            ]
29        ]));
30    }
31}
 1PHPUnit 10.5.63 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.30
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.016, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) classPropertiesEqualToRecursiveTest::testFailure
13Failed asserting that classPropertiesEqualToRecursiveTest is a class with properties equal to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Properties\ExpectedClassProperties Object #461 (
18-    'attribute' => '123',
19-    'nested' => Array &0 [
20-        'other' => '321',
21-    ],
22+Tailors\PHPUnit\Properties\ActualClassProperties Object #343 (
23+    'attribute' => 123,
24+    'nested' => 'classPropertiesEqualToRecursiveTest',
25 )
26
27classPropertiesEqualToRecursiveTest.php:24
28
29FAILURES!
30Tests: 2, Assertions: 2, Failures: 1.

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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 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\Properties\ExpectedClassProperties Object #451 (
18-    'attribute' => '123',
19-    'getValue()' => null,
20+Tailors\PHPUnit\Properties\ActualClassProperties Object #448 (
21+    'attribute' => 123,
22+    'getValue()' => 321,
23 )
24
25classPropertiesIdenticalToTest.php:26
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.

Since versions *.1 (1.1, 2.1, 3.1, …) it’s possible to nest recursively specifications of class properties. For more details, see arrayValues().

 1<?php declare(strict_types=1);
 2
 3final class classPropertiesIdenticalToRecursiveTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ClassPropertiesIdenticalToTrait;
 6    use \Tailors\PHPUnit\ClassPropertiesTrait;
 7
 8    public static $attribute = 123;
 9    public static $nested = self::class;
10    public static $other = 321;
11
12    public function testSuccess(): void
13    {
14        $this->assertThat(self::class, $this->classPropertiesIdenticalTo([
15            'attribute'  => 123,
16            'nested' => $this->classProperties([
17                'other' => 321,
18            ])
19        ]));
20    }
21
22    public function testFailure(): void
23    {
24        $this->assertThat(self::class, $this->classPropertiesIdenticalTo([
25            'attribute'  => 123,
26            'nested' => [
27                'other' => 321,
28            ]
29        ]));
30    }
31}
 1PHPUnit 10.5.63 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.30
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.016, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) classPropertiesIdenticalToRecursiveTest::testFailure
13Failed asserting that classPropertiesIdenticalToRecursiveTest is a class with properties identical to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Properties\ExpectedClassProperties Object #461 (
18+Tailors\PHPUnit\Properties\ActualClassProperties Object #343 (
19     'attribute' => 123,
20-    'nested' => Array &0 [
21-        'other' => 321,
22-    ],
23+    'nested' => 'classPropertiesIdenticalToRecursiveTest',
24 )
25
26classPropertiesIdenticalToRecursiveTest.php:24
27
28FAILURES!
29Tests: 2, Assertions: 2, Failures: 1.

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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.009, 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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 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) 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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.013, 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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 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) 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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.051, 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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.009, 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\Properties\ExpectedObjectProperties Object #451 (
18-    'attribute' => '123',
19-    'getValue()' => null,
20+Tailors\PHPUnit\Properties\ActualObjectProperties Object #448 (
21+    'attribute' => 123,
22+    'getValue()' => 321,
23 )
24
25objectPropertiesEqualToTest.php:26
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.

Since versions *.1 (1.1, 2.1, 3.1, …) it’s possible to nest recursively specifications of object properties. For more details, see arrayValues().

 1<?php declare(strict_types=1);
 2
 3final class objectPropertiesEqualToRecursiveTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ObjectPropertiesEqualToTrait;
 6    use \Tailors\PHPUnit\ObjectPropertiesTrait;
 7
 8    public $attribute = 123;
 9    public $other = 321;
10
11    public function nested(): \Exception
12    {
13        return new \Exception('FOO', 456);
14    }
15
16    public function testSuccess(): void
17    {
18        $this->assertThat($this, $this->objectPropertiesEqualTo([
19            'attribute'  => '123',
20            'nested()' => $this->objectProperties([
21                'getMessage()' => 'FOO',
22                'getCode()' => '456',
23            ])
24        ]));
25    }
26
27    public function testFailure(): void
28    {
29        $this->assertThat($this, $this->objectPropertiesEqualTo([
30            'attribute'  => '123',
31            'nested()' => [
32                'getMessage()' => 'FOO',
33                'getCode()' => '456',
34            ]
35        ]));
36    }
37}
 1PHPUnit 10.5.63 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.30
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.010, Memory: 8.00 MB
 9
10There was 1 failure:
11
121) objectPropertiesEqualToRecursiveTest::testFailure
13Failed asserting that object objectPropertiesEqualToRecursiveTest is an object with properties equal to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Properties\ExpectedObjectProperties Object #466 (
18-    'attribute' => '123',
19-    'nested()' => Array &0 [
20-        'getMessage()' => 'FOO',
21-        'getCode()' => '456',
22-    ],
23+Tailors\PHPUnit\Properties\ActualObjectProperties Object #484 (
24+    'attribute' => 123,
25+    'nested()' => Exception Object #464 (
26+        'message' => 'FOO',
27+        'string' => '',
28+        'code' => 456,
29+        'file' => 'objectPropertiesEqualToRecursiveTest.php',
30+        'line' => 13,
31+        'previous' => null,
32+    ),
33 )
34
35objectPropertiesEqualToRecursiveTest.php:29
36
37FAILURES!
38Tests: 2, Assertions: 2, Failures: 1.

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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 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\Properties\ExpectedObjectProperties Object #451 (
18-    'attribute' => '123',
19-    'getValue()' => null,
20+Tailors\PHPUnit\Properties\ActualObjectProperties Object #448 (
21+    'attribute' => 123,
22+    'getValue()' => 321,
23 )
24
25objectPropertiesIdenticalToTest.php:26
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.

Since versions *.1 (1.1, 2.1, 3.1, …) it’s possible to nest recursively specifications of object properties. For more details, see arrayValues().

 1<?php declare(strict_types=1);
 2
 3final class objectPropertiesIdenticalToRecursiveTest extends \PHPUnit\Framework\TestCase
 4{
 5    use \Tailors\PHPUnit\ObjectPropertiesIdenticalToTrait;
 6    use \Tailors\PHPUnit\ObjectPropertiesTrait;
 7
 8    public $attribute = 123;
 9    public $other = 321;
10
11    public function nested(): \Exception
12    {
13        return new \Exception('FOO', 456);
14    }
15
16    public function testSuccess(): void
17    {
18        $this->assertThat($this, $this->objectPropertiesIdenticalTo([
19            'attribute'  => 123,
20            'nested()' => $this->objectProperties([
21                'getMessage()' => 'FOO',
22                'getCode()' => 456,
23            ])
24        ]));
25    }
26
27    public function testFailure(): void
28    {
29        $this->assertThat($this, $this->objectPropertiesIdenticalTo([
30            'attribute'  => 123,
31            'nested()' => [
32                'getMessage()' => 'FOO',
33                'getCode()' => 456,
34            ]
35        ]));
36    }
37}
 1PHPUnit 10.5.63 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.2.30
 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) objectPropertiesIdenticalToRecursiveTest::testFailure
13Failed asserting that object objectPropertiesIdenticalToRecursiveTest is an object with properties identical to specified.
14--- Expected
15+++ Actual
16@@ @@
17-Tailors\PHPUnit\Properties\ExpectedObjectProperties Object #467 (
18+Tailors\PHPUnit\Properties\ActualObjectProperties Object #485 (
19     'attribute' => 123,
20-    'nested()' => Array &0 [
21-        'getMessage()' => 'FOO',
22-        'getCode()' => 456,
23-    ],
24+    'nested()' => Exception Object #465 (
25+        'message' => 'FOO',
26+        'string' => '',
27+        'code' => 456,
28+        'file' => 'objectPropertiesIdenticalToRecursiveTest.php',
29+        'line' => 13,
30+        'previous' => null,
31+    ),
32 )
33
34objectPropertiesIdenticalToRecursiveTest.php:29
35
36FAILURES!
37Tests: 2, Assertions: 2, Failures: 1.

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.

 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 11.1.2 by Sebastian Bergmann and contributors.
 2
 3Runtime:       PHP 8.3.6
 4Configuration: phpunit.xml
 5
 6.F                                                                  2 / 2 (100%)
 7
 8Time: 00:00.011, 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.