1. Assertions
This section lists the various assertion methods that are provided by sub-packages of phptailors/phpunit-extensions. Assertions may be added to your test class by including appropriate trait as shown in prerequisite tables below.
assertArrayValuesEqualTo()
Package |
phptailors/phpunit-arrays |
Trait |
Synopsis:
function assertArrayValuesEqualTo(array $expected, mixed $actual[, string $message = ''])
Reports an error identified by $message
if values in $actual
(array or
ArrayAccess instance) are not equal to $expected
ones (tested with ==
operator). The method compares only values specified in $expected
array, so
$expected = []
accepts any $actual
array. If $actual
is not an
array nor ArrayAccess instance, the constraint fails.
The arguments are:
$expected
- an array of expected values,$actual
- an array or an ArrayAccess instance with actual values,$message
- optional failure message,
The method
function assertNotArrayValuesEqualTo(array $expected, mixed $actual[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertArrayValuesEqualToTest extends \PHPUnit\Framework\TestCase
4{
5 use \Tailors\PHPUnit\ArrayValuesEqualToTrait;
6
7 public function testSuccess(): void
8 {
9 $this->assertArrayValuesEqualTo(
10 ['int' => 123, 1 => null],
11 ['int' => '123', 1 => '', 'foo' => 'FOO']
12 );
13 }
14
15 public function testFailure(): void
16 {
17 $this->assertArrayValuesEqualTo(
18 ['int' => 123, 1 => null, 'foo' => 'BAR'],
19 ['int' => '123', 1 => '', 'foo' => 'FOO']
20 );
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.008, Memory: 8.00 MB
9
10There was 1 failure:
11
121) AssertArrayValuesEqualToTest::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 => null,
20- 'foo' => 'BAR',
21+Tailors\PHPUnit\Values\ActualValues Object #444 (
22+ 'int' => '123',
23+ 1 => '',
24+ 'foo' => 'FOO',
25 )
26
27AssertArrayValuesEqualToTest.php:17
28
29FAILURES!
30Tests: 2, Assertions: 2, Failures: 1.
assertArrayValuesIdenticalTo()
Package |
phptailors/phpunit-arrays |
Trait |
Synopsis:
function assertArrayValuesIdenticalTo(array $expected, mixed $actual[, string $message = ''])
Reports an error identified by $message
if values in $actual
(array or
ArrayAccess instance) are not identical to $expected
ones (tested with
===
operator). The method compares only values specified in $expected
,
so $expected = []
accepts any $actual
array. If $actual
is not an
array nor an ArrayAccess instance, the constraint fails.
The arguments are:
$expected
- an array of expected values,$actual
- an array or an ArrayAccess instance with actual values,$message
- optional failure message,
The method
function assertNotArrayValuesIdenticalTo(array $expected, mixed $actual[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertArrayValuesIdenticalToTest extends \PHPUnit\Framework\TestCase
4{
5 use \Tailors\PHPUnit\ArrayValuesIdenticalToTrait;
6
7 public function testSuccess(): void
8 {
9 $this->assertArrayValuesIdenticalTo(
10 ['int' => 123, 1 => null],
11 ['int' => 123, 1 => null, 'foo' => 'FOO']
12 );
13 }
14
15 public function testFailure(): void
16 {
17 $this->assertArrayValuesIdenticalTo(
18 ['int' => 123, 1 => null],
19 ['int' => '123', 1 => '']
20 );
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.008, Memory: 8.00 MB
9
10There was 1 failure:
11
121) AssertArrayValuesIdenticalToTest::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- 'int' => 123,
19- 1 => null,
20+Tailors\PHPUnit\Values\ActualValues Object #444 (
21+ 'int' => '123',
22+ 1 => '',
23 )
24
25AssertArrayValuesIdenticalToTest.php:17
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.
assertClassPropertiesEqualTo()
Package |
phptailors/phpunit-properties |
Trait |
Synopsis:
function assertClassPropertiesEqualTo(array $expected, string $class[, string $message = ''])
Reports an error identified by $message
if properties of $class
are not
equal to $expected
ones (tested with ==
operator).
A property is either a static attribute value or a value returned by class’s
static method that is callable without arguments. The method compares only
properties specified in $expected
, so $expected = []
accepts any
existing $class
. If $class
does not exists, the constraint fails.
The arguments are:
$expected
- an associative array with property names as keys and their expected values as values, if a key ends with"()"
, then the property is assumed to be a method, for example$expected = ['foo()' => 'F']
requires methodfoo()
to return'F'
,$class
- name of the class to be examined,$message
- optional failure message,
The method
function assertNotClassPropertiesEqualTo(array $expected, string $class[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertClassPropertiesEqualToTest 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->assertClassPropertiesEqualTo([
18 'attribute' => '123', // - self::$attribute equals '123' (ok)
19 'getValue()' => '321', // - self::getValue() equals '321' (ok)
20 ], self::class);
21 }
22
23 public function testFailure(): void
24 {
25 // assert that:
26 $this->assertClassPropertiesEqualTo([
27 'attribute' => '123', // - self::$attribute equals '123' (ok),
28 'getValue()' => null, // - self::$getValue() is 321, not equals null (fail)
29 ], self::class);
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) AssertClassPropertiesEqualToTest::testFailure
13Failed asserting that AssertClassPropertiesEqualToTest 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
25AssertClassPropertiesEqualToTest.php:26
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.
assertClassPropertiesIdenticalTo()
Package |
phptailors/phpunit-properties |
Trait |
Synopsis:
function assertClassPropertiesIdenticalTo(array $expected, string $class[, string $message = ''])
Reports an error identified by $message
if properties of $class
’s are
not identical to $expected
ones (tested with ===
operator).
A property is either a static attribute value or a value returned by
$class
’s static method that is callable without arguments. The method
compares only properties specified in $expected
, so $expected = []
accepts any existing $class
. If $class
does not exist, the constraint
fails.
The arguments are:
$expected
- an associative array with property names as keys and their expected values as values, if a key ends with"()"
, then the property is assumed to be a method, for example$expected = ['foo()' => 'F']
requires methodfoo()
to return'F'
,$class
- name of the class to be examined,$message
- optional failure message,
The method
function assertNotClassPropertiesIdenticalTo(array $expected, string $class[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertClassPropertiesIdenticalToTest 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->assertClassPropertiesIdenticalTo([
18 'attribute' => 123, // - self::$attribute is 123 (ok)
19 'getValue()' => 321, // - self::getValue() is 321 (ok)
20 ], self::class);
21 }
22
23 public function testFailure(): void
24 {
25 // assert that:
26 $this->assertClassPropertiesIdenticalTo([
27 'attribute' => '123', // - self::$attribute is 123, not '123' (fail),
28 'getValue()' => null, // - self::getValue() is 321, not null (fail)
29 ], self::class);
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) AssertClassPropertiesIdenticalToTest::testFailure
13Failed asserting that AssertClassPropertiesIdenticalToTest 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
25AssertClassPropertiesIdenticalToTest.php:26
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.
assertExtendsClass()
Package |
phptailors/phpunit-inheritance |
Trait |
Synopsis:
function assertExtendsClass(string $parent, mixed $subject[, string $message = ''])
Reports an error identified by $message
if $subject
does not extend the
$parent
class. The $subject
may be an object
or a class name as
string
:
if
$subject
is anobject
, then its class, as returned byget_class($subject)
, is examined against$parent
, the assertion succeeds only if the class extends the$parent
class,otherwise, the necessary conditions for the assertion to succeed are that
$subject
is a string,class_exists($subject)
istrue
, andthe
$subject
class extends the$parent
class.
The method
function assertNotExtendsClass(string $parent, mixed $subject[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertExtendsClassTest extends \PHPUnit\Framework\TestCase
4{
5 use \Tailors\PHPUnit\ExtendsClassTrait;
6
7 public function testAssertExtendsClass(): void
8 {
9 $this->assertExtendsClass(\Exception::class, \RuntimeException::class);
10 $this->assertExtendsClass(\Exception::class, new \RuntimeException());
11 }
12
13 public function testAssertExtendsClassFailure(): void
14 {
15 $this->assertExtendsClass(\Exception::class, self::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) AssertExtendsClassTest::testAssertExtendsClassFailure
13Failed asserting that AssertExtendsClassTest extends class Exception.
14
15AssertExtendsClassTest.php:15
16
17FAILURES!
18Tests: 2, Assertions: 3, Failures: 1.
assertHasMethod()
Package |
phptailors/phpunit-methods |
Trait |
Synopsis:
function assertHasMethod(array $methodSpec, mixed $subject[, string $message = ''])
Reports an error identified by $message
if $subject
has no method
that matches $methodSpec
. The $methodSpec
specifies matching method
via its name and modifiers (optional).
Note
The assertion can only be successfull for $subject
being an object,
class, trait or interface. For any other values it always fails.
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.
The method
function assertNotHasMethod(array $expected, array $matches[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3abstract class ClassWithSomeMethods
4{
5 abstract protected function abstractProtectedMethod();
6 public function publicMethod() { }
7}
8
9final class AssertHasMethodTest extends PHPUnit\Framework\TestCase
10{
11 use \Tailors\PHPUnit\HasMethodTrait;
12
13 public function testAssertHasMethod(): void
14 {
15 // ClassWithSomeMethods::publicMethod
16 $this->assertHasMethod('publicMethod', ClassWithSomeMethods::class);
17 $this->assertHasMethod('public function publicMethod', ClassWithSomeMethods::class);
18 $this->assertHasMethod('!abstract !final public function publicMethod', ClassWithSomeMethods::class);
19
20 // ClassWithSomeMethods::abstractProtectedMethod
21 $this->assertHasMethod('protected function abstractProtectedMethod', ClassWithSomeMethods::class);
22 $this->assertHasMethod('abstract function abstractProtectedMethod', ClassWithSomeMethods::class);
23 $this->assertHasMethod('abstract protected function abstractProtectedMethod', ClassWithSomeMethods::class);
24
25
26 $this->assertNotHasMethod('!abstract function abstractProtectedMethod', ClassWithSomeMethods::class);
27 }
28
29 public function testAssertHasMethodFailure(): void
30 {
31 $this->assertHasMethod('!abstract function abstractProtectedMethod', ClassWithSomeMethods::class);
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.006, Memory: 8.00 MB
9
10There was 1 failure:
11
121) AssertHasMethodTest::testAssertHasMethodFailure
13Failed asserting that 'ClassWithSomeMethods' has !abstract method abstractProtectedMethod().
14
15AssertHasMethodTest.php:31
16
17FAILURES!
18Tests: 2, Assertions: 8, Failures: 1.
assertHasPregCaptures()
Package |
phptailors/phpunit-regexp |
Trait |
Synopsis:
function assertHasPregCaptures(array $expected, array $matches[, string $message = ''])
Reports an error identified by $message
if PCRE captures found in
$matches
(an array supposedly returned from preg_match()) do not agree
with the expectations prescribed in the $expected
array. The method
verifies only groups specified in $expected
, so $expected = []
accepts any array of $matches
. Expectations are formulated as follows:
$expected = ['foo' => true]
requires$matches['foo']
to be present,$expected = ['foo' => false]
requires$matches['foo']
to be absent,$expected = ['foo' => 'FOO']
requires that$matches['foo'] === 'FOO'
,
A capture group foo
is considered absent if:
$matches['foo']
is not set, or$matches['foo'] === null
, or$matches['foo'] === [null, ...]
.
Note
The presence/absence checks work only with $matches
returned from
preg_match() when invoked with the PREG_UNMATCHED_AS_NULL
flag.
The method
function assertNotHasPregCaptures(array $expected, array $matches[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertHasPregCapturesTest 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 testAssertHasPregCaptures(): void
19 {
20 // assert that:
21 $this->assertHasPregCaptures([
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 ], $this->matches);
27 }
28
29 public function testAssertHasPregCapturesFailure(): void
30 {
31 // assert that:
32 $this->assertHasPregCaptures([
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 ], $this->matches);
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.007, Memory: 8.00 MB
9
10There was 1 failure:
11
121) AssertHasPregCapturesTest::testAssertHasPregCapturesFailure
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
27AssertHasPregCapturesTest.php:32
28
29FAILURES!
30Tests: 2, Assertions: 2, Failures: 1.
assertImplementsInterface()
Package |
phptailors/phpunit-inheritance |
Trait |
Synopsis:
function assertImplementsInterface(string $interface, mixed $subject[, string $message = ''])
Reports an error identified by $message
if $subject
does not implement
the $interface
. The $subject
may be an object
or a class/interface
name as string
:
if
$subject
is anobject
, then its class, as returned byget_class($subject)
, is examined against$interface
, the assertion succeeds only if the class implements the$interface
,otherwise, the necessary conditions for the assertion to succeed are that
$subject
is a string,class_exists($subject)
istrue
orinterface_exists($subject)
istrue
, andthe
$subject
implements the$interface
.
The method
function assertNotImplementsInterface(string $interface, mixed $subject[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertImplementsInterfaceTest extends \PHPUnit\Framework\TestCase
4{
5 use \Tailors\PHPUnit\ImplementsInterfaceTrait;
6
7 public function testAssertImplementsInterface(): void
8 {
9 $this->assertImplementsInterface(\Throwable::class, \RuntimeException::class);
10 $this->assertImplementsInterface(\Throwable::class, new \RuntimeException());
11 }
12
13 public function testAssertImplementsInterfaceFailure(): void
14 {
15 $this->assertImplementsInterface(\Throwable::class, self::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) AssertImplementsInterfaceTest::testAssertImplementsInterfaceFailure
13Failed asserting that AssertImplementsInterfaceTest implements interface Throwable.
14
15AssertImplementsInterfaceTest.php:15
16
17FAILURES!
18Tests: 2, Assertions: 3, Failures: 1.
assertKsortedArrayEqualTo()
Package |
phptailors/phpunit-arrays |
Trait |
Synopsis:
function assertKsortedArrayEqualTo(array $expected, mixed $actual[, string $message = ''])
Reports an error identified by $message
if $actual
array is not equal
to $expected
when key-sorted (tested with ==
operator). If $actual
is not an array, the constraint fails.
The arguments are:
$expected
- an array of expected values,$actual
- an actual value,$message
- optional failure message,
The method
function assertNotKsortedArrayEqualTo(array $expected, mixed $actual[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertKsortedArrayEqualToTest extends \PHPUnit\Framework\TestCase
4{
5 use \Tailors\PHPUnit\KsortedArrayEqualToTrait;
6
7 public function testSuccess(): void
8 {
9 $this->assertKsortedArrayEqualTo(
10 ['int' => 123, 1 => null, 'foo' => 'FOO'],
11 ['foo' => 'FOO', 'int' => '123', 1 => '']
12 );
13 }
14
15 public function testFailure(): void
16 {
17 $this->assertKsortedArrayEqualTo(
18 ['int' => 123, 1 => '', 'foo' => 'BAR'],
19 ['int' => 123, 1 => '', 'foo' => 'FOO']
20 );
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) AssertKsortedArrayEqualToTest::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+ 'foo' => 'FOO',
21 'int' => 123,
22 ]
23
24AssertKsortedArrayEqualToTest.php:17
25
26FAILURES!
27Tests: 2, Assertions: 2, Failures: 1.
assertKsortedArrayIdenticalTo()
Package |
phptailors/phpunit-arrays |
Trait |
Synopsis:
function assertKsortedArrayIdenticalTo(array $expected, mixed $actual[, string $message = ''])
Reports an error identified by $message
if $actual
is not identical to
$expected
one when key-sorted (tested with ===
operator). If
$actual
is not an array, the constraint fails.
The arguments are:
$expected
- an array of expected values,$actual
- actual value,$message
- optional failure message,
The method
function assertNotKsortedArrayIdenticalTo(array $expected, mixed $actual[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertKsortedArrayIdenticalToTest extends \PHPUnit\Framework\TestCase
4{
5 use \Tailors\PHPUnit\KsortedArrayIdenticalToTrait;
6
7 public function testSuccess(): void
8 {
9 $this->assertKsortedArrayIdenticalTo(
10 ['int' => 123, 1 => null, 'foo' => 'FOO'],
11 ['foo' => 'FOO', 1 => null, 'int' => 123]
12 );
13 }
14
15 public function testFailure(): void
16 {
17 $this->assertKsortedArrayIdenticalTo(
18 ['int' => 123, 1 => null, 'foo' =>'FOO'],
19 ['int' => '123', 1 => '', 'foo' => 'FOO']
20 );
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) AssertKsortedArrayIdenticalToTest::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+ 'int' => '123',
23 ]
24
25AssertKsortedArrayIdenticalToTest.php:17
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.
assertObjectPropertiesEqualTo()
Package |
phptailors/phpunit-properties |
Trait |
Synopsis:
function assertObjectPropertiesEqualTo(array $expected, object $object[, string $message = ''])
Reports an error identified by $message
if $object
’s properties are not
equal to $expected
ones (tested with ==
operator).
A property is either an attribute value or a value returned by object’s method
that is callable without arguments. The method compares only properties
specified in $expected
, so $expected = []
accepts any $object
.
The arguments are:
$expected
- an associative array with property names as keys and their expected values as values, if a key ends with"()"
, then the property is assumed to be a method, for example$expected = ['foo()' => 'F']
requires methodfoo()
to return'F'
,$object
- an object to be examined,$message
- optional failure message,
The method
function assertNotObjectPropertiesEqualTo(array $expected, object $object[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertObjectPropertiesEqualToTest 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->assertObjectPropertiesEqualTo([
18 'attribute' => '123', // - $this->attribute equals '123' (ok)
19 'getValue()' => '321', // - $this->getValue() equals '321' (ok)
20 ], $this);
21 }
22
23 public function testFailure(): void
24 {
25 // assert that:
26 $this->assertObjectPropertiesEqualTo([
27 'attribute' => '123', // - $this->attribute equals '123' (ok),
28 'getValue()' => null, // - $this->getValue() is 321, not equals null (fail)
29 ], $this);
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) AssertObjectPropertiesEqualToTest::testFailure
13Failed asserting that object AssertObjectPropertiesEqualToTest 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
25AssertObjectPropertiesEqualToTest.php:26
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.
assertObjectPropertiesIdenticalTo()
Package |
phptailors/phpunit-properties |
Trait |
Synopsis:
function assertObjectPropertiesIdenticalTo(array $expected, object $object[, string $message = ''])
Reports an error identified by $message
if $object
’s properties are not
identical with $expected
ones (tested with ===
operator).
A property is either an attribute value or a value returned by object’s method
that is callable without arguments. The method compares only properties
specified in $expected
, so $expected = []
accepts any $object
.
The arguments are:
$expected
- an associative array with property names as keys and their expected values as values, if a key ends with"()"
, then the property is assumed to be a method, for example$expected = ['foo()' => 'F']
requires methodfoo()
to return'F'
,$object
- an object to be examined,$message
- optional failure message,
The method
function assertNotObjectPropertiesIdenticalTo(array $expected, array $matches[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3final class AssertObjectPropertiesIdenticalToTest 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->assertObjectPropertiesIdenticalTo([
18 'attribute' => 123, // - $this->attribute is 123 (ok)
19 'getValue()' => 321, // - $this->getValue() is 321 (ok)
20 ], $this);
21 }
22
23 public function testFailure(): void
24 {
25 // assert that:
26 $this->assertObjectPropertiesIdenticalTo([
27 'attribute' => '123', // - $this->attribute is 123, not '123' (fail),
28 'getValue()' => null, // - $this->getValue() is 321, not null (fail)
29 ], $this);
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) AssertObjectPropertiesIdenticalToTest::testFailure
13Failed asserting that object AssertObjectPropertiesIdenticalToTest 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
25AssertObjectPropertiesIdenticalToTest.php:26
26
27FAILURES!
28Tests: 2, Assertions: 2, Failures: 1.
assertUsesTrait()
Package |
phptailors/phpunit-inheritance |
Trait |
Synopsis:
function assertUsesTrait(string $trait, mixed $subject[, string $message = ''])
Reports an error identified by $message
if $subject
does not use the
$trait
. The $subject
may be an object
or a class name as string
:
if
$subject
is anobject
, then its class, as returned byget_class($subject)
, is examined against$trait
, the assertion succeeds only if the class uses the$trait
,otherwise, the necessary conditions for the assertion to succeed are that
$subject
is a string,class_exists($subject)
istrue
, andthe
$subject
implements the$trait
.
The method
function assertNotUsesTrait(string $trait, mixed $subject[, string $message = ''])
is the inverse of this.
1<?php declare(strict_types=1);
2
3trait ExampleTraitForAssertUsesTraitTest
4{
5}
6
7final class AssertUsesTraitTest extends \PHPUnit\Framework\TestCase
8{
9 use \Tailors\PHPUnit\UsesTraitTrait;
10 use ExampleTraitForAssertUsesTraitTest;
11
12 public function testAssertUsesTrait(): void
13 {
14 $this->assertUsesTrait(ExampleTraitForAssertUsesTraitTest::class, self::class);
15 $this->assertUsesTrait(ExampleTraitForAssertUsesTraitTest::class, $this);
16 }
17
18 public function testAssertUsesTraitFailure(): void
19 {
20 $this->assertUsesTrait(ExampleTraitForAssertUsesTraitTest::class, \RuntimeException::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) AssertUsesTraitTest::testAssertUsesTraitFailure
13Failed asserting that RuntimeException uses trait ExampleTraitForAssertUsesTraitTest.
14
15AssertUsesTraitTest.php:20
16
17FAILURES!
18Tests: 2, Assertions: 3, Failures: 1.