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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php declare(strict_types=1);
final class AssertArrayValuesEqualToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ArrayValuesEqualToTrait;
public function testSuccess(): void
{
$this->assertArrayValuesEqualTo(
['int' => 123, 1 => null],
['int' => '123', 1 => '', 'foo' => 'FOO']
);
}
public function testFailure(): void
{
$this->assertArrayValuesEqualTo(
['int' => 123, 1 => null, 'foo' => 'BAR'],
['int' => '123', 1 => '', 'foo' => 'FOO']
);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.006, Memory: 6.00 MB
There was 1 failure:
1) AssertArrayValuesEqualToTest::testFailure
Failed asserting that array is an array or ArrayAccess with values equal to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'int' => 123
- 1 => null
- 'foo' => 'BAR'
+ 'int' => '123'
+ 1 => ''
+ 'foo' => 'FOO'
)
AssertArrayValuesEqualToTest.php:19
FAILURES!
Tests: 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php declare(strict_types=1);
final class AssertArrayValuesIdenticalToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ArrayValuesIdenticalToTrait;
public function testSuccess(): void
{
$this->assertArrayValuesIdenticalTo(
['int' => 123, 1 => null],
['int' => 123, 1 => null, 'foo' => 'FOO']
);
}
public function testFailure(): void
{
$this->assertArrayValuesIdenticalTo(
['int' => 123, 1 => null],
['int' => '123', 1 => '']
);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.007, Memory: 6.00 MB
There was 1 failure:
1) AssertArrayValuesIdenticalToTest::testFailure
Failed asserting that array is an array or ArrayAccess with values identical to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'int' => 123
- 1 => null
+ 'int' => '123'
+ 1 => ''
)
AssertArrayValuesIdenticalToTest.php:19
FAILURES!
Tests: 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php declare(strict_types=1);
final class AssertClassPropertiesEqualToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ClassPropertiesEqualToTrait;
public static $attribute = 123;
public static function getValue(): int
{
return 321;
}
public function testSuccess(): void
{
// assert that:
$this->assertClassPropertiesEqualTo([
'attribute' => '123', // - self::$attribute equals '123' (ok)
'getValue()' => '321', // - self::getValue() equals '321' (ok)
], self::class);
}
public function testFailure(): void
{
// assert that:
$this->assertClassPropertiesEqualTo([
'attribute' => '123', // - self::$attribute equals '123' (ok),
'getValue()' => null, // - self::$getValue() is 321, not equals null (fail)
], self::class);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.007, Memory: 6.00 MB
There was 1 failure:
1) AssertClassPropertiesEqualToTest::testFailure
Failed asserting that AssertClassPropertiesEqualToTest is a class with properties equal to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'attribute' => '123'
- 'getValue()' => null
+ 'attribute' => 123
+ 'getValue()' => 321
)
AssertClassPropertiesEqualToTest.php:29
FAILURES!
Tests: 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php declare(strict_types=1);
final class AssertClassPropertiesIdenticalToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ClassPropertiesIdenticalToTrait;
public static $attribute = 123;
public static function getValue(): int
{
return 321;
}
public function testSuccess(): void
{
// assert that:
$this->assertClassPropertiesIdenticalTo([
'attribute' => 123, // - self::$attribute is 123 (ok)
'getValue()' => 321, // - self::getValue() is 321 (ok)
], self::class);
}
public function testFailure(): void
{
// assert that:
$this->assertClassPropertiesIdenticalTo([
'attribute' => '123', // - self::$attribute is 123, not '123' (fail),
'getValue()' => null, // - self::getValue() is 321, not null (fail)
], self::class);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.007, Memory: 6.00 MB
There was 1 failure:
1) AssertClassPropertiesIdenticalToTest::testFailure
Failed asserting that AssertClassPropertiesIdenticalToTest is a class with properties identical to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'attribute' => '123'
- 'getValue()' => null
+ 'attribute' => 123
+ 'getValue()' => 321
)
AssertClassPropertiesIdenticalToTest.php:29
FAILURES!
Tests: 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
$subjectis anobject, then its class, as returned byget_class($subject), is examined against$parent, the assertion succeeds only if the class extends the$parentclass,otherwise, the necessary conditions for the assertion to succeed are that
$subjectis a string,class_exists($subject)istrue, andthe
$subjectclass extends the$parentclass.
The method
function assertNotExtendsClass(string $parent, mixed $subject[, string $message = ''])
is the inverse of this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php declare(strict_types=1);
final class AssertExtendsClassTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ExtendsClassTrait;
public function testAssertExtendsClass(): void
{
$this->assertExtendsClass(\Exception::class, \RuntimeException::class);
$this->assertExtendsClass(\Exception::class, new \RuntimeException());
}
public function testAssertExtendsClassFailure(): void
{
$this->assertExtendsClass(\Exception::class, self::class);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) AssertExtendsClassTest::testAssertExtendsClassFailure
Failed asserting that AssertExtendsClassTest extends class Exception.
AssertExtendsClassTest.php:15
FAILURES!
Tests: 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 static is "!abstract"
+ "!static". All other combinations are treated as syntax error.
Presence of a modifier (such as "final") requires the modifier to be
present in a matched method. Presence of negated modifier (such as
"!final") requires the modifier to be absent in a matched method. Absence
of a modifier in $methodSpec means that it may be either present or absent
in a matched method.
The method
function assertNotHasMethod(array $expected, array $matches[, string $message = ''])
is the inverse of this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php declare(strict_types=1);
abstract class ClassWithSomeMethods
{
abstract protected function abstractProtectedMethod();
public function publicMethod() { }
}
final class AssertHasMethodTest extends PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\HasMethodTrait;
public function testAssertHasMethod(): void
{
// ClassWithSomeMethods::publicMethod
$this->assertHasMethod('publicMethod', ClassWithSomeMethods::class);
$this->assertHasMethod('public function publicMethod', ClassWithSomeMethods::class);
$this->assertHasMethod('!abstract !final public function publicMethod', ClassWithSomeMethods::class);
// ClassWithSomeMethods::abstractProtectedMethod
$this->assertHasMethod('protected function abstractProtectedMethod', ClassWithSomeMethods::class);
$this->assertHasMethod('abstract function abstractProtectedMethod', ClassWithSomeMethods::class);
$this->assertHasMethod('abstract protected function abstractProtectedMethod', ClassWithSomeMethods::class);
$this->assertNotHasMethod('!abstract function abstractProtectedMethod', ClassWithSomeMethods::class);
}
public function testAssertHasMethodFailure(): void
{
$this->assertHasMethod('!abstract function abstractProtectedMethod', ClassWithSomeMethods::class);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PHPUnit 9.5.27 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.009, Memory: 6.00 MB
There was 1 failure:
1) AssertHasMethodTest::testAssertHasMethodFailure
Failed asserting that 'ClassWithSomeMethods' has !abstract method abstractProtectedMethod().
AssertHasMethodTest.php:31
FAILURES!
Tests: 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php declare(strict_types=1);
final class AssertHasPregCapturesTest extends PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\HasPregCapturesTrait;
private $regexp;
private $subject;
private $matches;
public function setUp(): void
{
$this->regexp = '/(?<name>\w+) (?<surname>\w+)(?:, (?<age>\d+))?(?:, (?<city>\w+))?/';
$this->subject = 'John Smith, London';
preg_match($this->regexp, $this->subject, $this->matches, PREG_UNMATCHED_AS_NULL);
}
public function testAssertHasPregCaptures(): void
{
// assert that:
$this->assertHasPregCaptures([
'name' => 'John', // - name is 'John' (ok),
'surname' => 'Smith', // - surname is 'Smith' (ok),
'age' => false, // - age is absent (ok),
'city' => true, // - city is present (ok).
], $this->matches);
}
public function testAssertHasPregCapturesFailure(): void
{
// assert that:
$this->assertHasPregCaptures([
'name' => 'John', // - name is 'John' (ok),
'surname' => 'Brown', // - surname is 'Brown' (fail),
'age' => true, // - age is present (fail),
'city' => false, // - city is absent (fail).
], $this->matches);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) AssertHasPregCapturesTest::testAssertHasPregCapturesFailure
Failed asserting that array has expected PCRE capture groups.
--- Expected
+++ Actual
@@ @@
Array &0 (
'name' => 'John'
- 'surname' => 'Brown'
- 'age' => true
- 'city' => false
+ 'surname' => 'Smith'
+ 'age' => null
+ 'city' => 'London'
)
AssertHasPregCapturesTest.php:37
FAILURES!
Tests: 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
$subjectis 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
$subjectis a string,class_exists($subject)istrueorinterface_exists($subject)istrue, andthe
$subjectimplements the$interface.
The method
function assertNotImplementsInterface(string $interface, mixed $subject[, string $message = ''])
is the inverse of this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php declare(strict_types=1);
final class AssertImplementsInterfaceTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ImplementsInterfaceTrait;
public function testAssertImplementsInterface(): void
{
$this->assertImplementsInterface(\Throwable::class, \RuntimeException::class);
$this->assertImplementsInterface(\Throwable::class, new \RuntimeException());
}
public function testAssertImplementsInterfaceFailure(): void
{
$this->assertImplementsInterface(\Throwable::class, self::class);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) AssertImplementsInterfaceTest::testAssertImplementsInterfaceFailure
Failed asserting that AssertImplementsInterfaceTest implements interface Throwable.
AssertImplementsInterfaceTest.php:15
FAILURES!
Tests: 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php declare(strict_types=1);
final class AssertKsortedArrayEqualToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\KsortedArrayEqualToTrait;
public function testSuccess(): void
{
$this->assertKsortedArrayEqualTo(
['int' => 123, 1 => null, 'foo' => 'FOO'],
['foo' => 'FOO', 'int' => '123', 1 => '']
);
}
public function testFailure(): void
{
$this->assertKsortedArrayEqualTo(
['int' => 123, 1 => '', 'foo' => 'BAR'],
['int' => 123, 1 => '', 'foo' => 'FOO']
);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) AssertKsortedArrayEqualToTest::testFailure
Failed asserting that array is an array equal to specified one when ksorted.
--- Expected
+++ Actual
@@ @@
Array &0 (
- 'foo' => 'BAR'
+ 'foo' => 'FOO'
'int' => 123
1 => ''
)
AssertKsortedArrayEqualToTest.php:19
FAILURES!
Tests: 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php declare(strict_types=1);
final class AssertKsortedArrayIdenticalToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\KsortedArrayIdenticalToTrait;
public function testSuccess(): void
{
$this->assertKsortedArrayIdenticalTo(
['int' => 123, 1 => null, 'foo' => 'FOO'],
['foo' => 'FOO', 1 => null, 'int' => 123]
);
}
public function testFailure(): void
{
$this->assertKsortedArrayIdenticalTo(
['int' => 123, 1 => null, 'foo' =>'FOO'],
['int' => '123', 1 => '', 'foo' => 'FOO']
);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) AssertKsortedArrayIdenticalToTest::testFailure
Failed asserting that array is an array identical to specified one when ksorted.
--- Expected
+++ Actual
@@ @@
Array &0 (
'foo' => 'FOO'
- 'int' => 123
- 1 => null
+ 'int' => '123'
+ 1 => ''
)
AssertKsortedArrayIdenticalToTest.php:19
FAILURES!
Tests: 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php declare(strict_types=1);
final class AssertObjectPropertiesEqualToTest extends PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ObjectPropertiesEqualToTrait;
public $attribute = 123;
public function getValue(): int
{
return 321;
}
public function testSuccess(): void
{
// assert that:
$this->assertObjectPropertiesEqualTo([
'attribute' => '123', // - $this->attribute equals '123' (ok)
'getValue()' => '321', // - $this->getValue() equals '321' (ok)
], $this);
}
public function testFailure(): void
{
// assert that:
$this->assertObjectPropertiesEqualTo([
'attribute' => '123', // - $this->attribute equals '123' (ok),
'getValue()' => null, // - $this->getValue() is 321, not equals null (fail)
], $this);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.008, Memory: 6.00 MB
There was 1 failure:
1) AssertObjectPropertiesEqualToTest::testFailure
Failed asserting that object AssertObjectPropertiesEqualToTest is an object with properties equal to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'attribute' => '123'
- 'getValue()' => null
+ 'attribute' => 123
+ 'getValue()' => 321
)
AssertObjectPropertiesEqualToTest.php:27
FAILURES!
Tests: 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php declare(strict_types=1);
final class AssertObjectPropertiesIdenticalToTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\ObjectPropertiesIdenticalToTrait;
public $attribute = 123;
public function getValue(): int
{
return 321;
}
public function testSuccess(): void
{
// assert that:
$this->assertObjectPropertiesIdenticalTo([
'attribute' => 123, // - $this->attribute is 123 (ok)
'getValue()' => 321, // - $this->getValue() is 321 (ok)
], $this);
}
public function testFailure(): void
{
// assert that:
$this->assertObjectPropertiesIdenticalTo([
'attribute' => '123', // - $this->attribute is 123, not '123' (fail),
'getValue()' => null, // - $this->getValue() is 321, not null (fail)
], $this);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.023, Memory: 6.00 MB
There was 1 failure:
1) AssertObjectPropertiesIdenticalToTest::testFailure
Failed asserting that object AssertObjectPropertiesIdenticalToTest is an object with properties identical to specified.
--- Expected
+++ Actual
@@ @@
values (
- 'attribute' => '123'
- 'getValue()' => null
+ 'attribute' => 123
+ 'getValue()' => 321
)
AssertObjectPropertiesIdenticalToTest.php:27
FAILURES!
Tests: 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
$subjectis 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
$subjectis a string,class_exists($subject)istrue, andthe
$subjectimplements the$trait.
The method
function assertNotUsesTrait(string $trait, mixed $subject[, string $message = ''])
is the inverse of this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php declare(strict_types=1);
trait ExampleTraitForAssertUsesTraitTest
{
}
final class AssertUsesTraitTest extends \PHPUnit\Framework\TestCase
{
use \Tailors\PHPUnit\UsesTraitTrait;
use ExampleTraitForAssertUsesTraitTest;
public function testAssertUsesTrait(): void
{
$this->assertUsesTrait(ExampleTraitForAssertUsesTraitTest::class, self::class);
$this->assertUsesTrait(ExampleTraitForAssertUsesTraitTest::class, $this);
}
public function testAssertUsesTraitFailure(): void
{
$this->assertUsesTrait(ExampleTraitForAssertUsesTraitTest::class, \RuntimeException::class);
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 00:00.005, Memory: 6.00 MB
There was 1 failure:
1) AssertUsesTraitTest::testAssertUsesTraitFailure
Failed asserting that RuntimeException uses trait ExampleTraitForAssertUsesTraitTest.
AssertUsesTraitTest.php:20
FAILURES!
Tests: 2, Assertions: 3, Failures: 1.
|