assertThat()

assertThat()

More complex assertions can be formulated using the PHPUnit_Framework_Constraint classes. They can be evaluated using the assertThat() method. Example A.49 shows how the logicalNot() and equalTo() constraints can be used to express the same assertion as assertNotEquals().

assertThat(mixed $value, PHPUnit_Framework_Constraint $constraint[, $message = ''])

Reports an error identified by $message if the $value does not match the $constraint.

Example A.49: Usage of assertThat()

<?php
use PHPUnit\Framework\TestCase;

class BiscuitTest extends TestCase
{
    public function testEquals()
    {
        $theBiscuit = new Biscuit('Ginger');
        $myBiscuit  = new Biscuit('Ginger');

        $this->assertThat(
          $theBiscuit,
          $this->logicalNot(
            $this->equalTo($myBiscuit)
          )
        );
    }
}
?>

Table A.1 shows the available PHPUnit_Framework_Constraint classes.

Table A.1. Constraints

Constraint Meaning
PHPUnit_Framework_Constraint_Attribute attribute(PHPUnit_Framework_Constraint $constraint, $attributeName) Constraint that applies another constraint to an attribute of a class or an object.
PHPUnit_Framework_Constraint_IsAnything anything() Constraint that accepts any input value.
PHPUnit_Framework_Constraint_ArrayHasKey arrayHasKey(mixed $key) Constraint that asserts that the array it is evaluated for has a given key.
PHPUnit_Framework_Constraint_TraversableContains contains(mixed $value) Constraint that asserts that the array or object that implements the Iterator interface it is evaluated for contains a given value.
PHPUnit_Framework_Constraint_TraversableContainsOnly containsOnly(string $type) Constraint that asserts that the array or object that implements the Iterator interface it is evaluated for contains only values of a given type.
PHPUnit_Framework_Constraint_TraversableContainsOnly containsOnlyInstancesOf(string $classname) Constraint that asserts that the array or object that implements the Iterator interface it is evaluated for contains only instances of a given classname.
PHPUnit_Framework_Constraint_IsEqual equalTo($value, $delta = 0, $maxDepth = 10) Constraint that checks if one value is equal to another.
PHPUnit_Framework_Constraint_Attribute attributeEqualTo($attributeName, $value, $delta = 0, $maxDepth = 10) Constraint that checks if a value is equal to an attribute of a class or of an object.
PHPUnit_Framework_Constraint_DirectoryExists directoryExists() Constraint that checks if the directory that it is evaluated for exists.
PHPUnit_Framework_Constraint_FileExists fileExists() Constraint that checks if the file(name) that it is evaluated for exists.
PHPUnit_Framework_Constraint_IsReadable isReadable() Constraint that checks if the file(name) that it is evaluated for is readable.
PHPUnit_Framework_Constraint_IsWritable isWritable() Constraint that checks if the file(name) that it is evaluated for is writable.
PHPUnit_Framework_Constraint_GreaterThan greaterThan(mixed $value) Constraint that asserts that the value it is evaluated for is greater than a given value.
PHPUnit_Framework_Constraint_Or greaterThanOrEqual(mixed $value) Constraint that asserts that the value it is evaluated for is greater than or equal to a given value.
PHPUnit_Framework_Constraint_ClassHasAttribute classHasAttribute(string $attributeName) Constraint that asserts that the class it is evaluated for has a given attribute.
PHPUnit_Framework_Constraint_ClassHasStaticAttribute classHasStaticAttribute(string $attributeName) Constraint that asserts that the class it is evaluated for has a given static attribute.
PHPUnit_Framework_Constraint_ObjectHasAttribute hasAttribute(string $attributeName) Constraint that asserts that the object it is evaluated for has a given attribute.
PHPUnit_Framework_Constraint_IsIdentical identicalTo(mixed $value) Constraint that asserts that one value is identical to another.
PHPUnit_Framework_Constraint_IsFalse isFalse() Constraint that asserts that the value it is evaluated is false.
PHPUnit_Framework_Constraint_IsInstanceOf isInstanceOf(string $className) Constraint that asserts that the object it is evaluated for is an instance of a given class.
PHPUnit_Framework_Constraint_IsNull isNull() Constraint that asserts that the value it is evaluated is null.
PHPUnit_Framework_Constraint_IsTrue isTrue() Constraint that asserts that the value it is evaluated is true.
PHPUnit_Framework_Constraint_IsType isType(string $type) Constraint that asserts that the value it is evaluated for is of a specified type.
PHPUnit_Framework_Constraint_LessThan lessThan(mixed $value) Constraint that asserts that the value it is evaluated for is smaller than a given value.
PHPUnit_Framework_Constraint_Or lessThanOrEqual(mixed $value) Constraint that asserts that the value it is evaluated for is smaller than or equal to a given value.
logicalAnd() Logical AND.
logicalNot(PHPUnit_Framework_Constraint $constraint) Logical NOT.
logicalOr() Logical OR.
logicalXor() Logical XOR.
PHPUnit_Framework_Constraint_PCREMatch matchesRegularExpression(string $pattern) Constraint that asserts that the string it is evaluated for matches a regular expression.
PHPUnit_Framework_Constraint_StringContains stringContains(string $string, bool $case) Constraint that asserts that the string it is evaluated for contains a given string.
PHPUnit_Framework_Constraint_StringEndsWith stringEndsWith(string $suffix) Constraint that asserts that the string it is evaluated for ends with a given suffix.
PHPUnit_Framework_Constraint_StringStartsWith stringStartsWith(string $prefix) Constraint that asserts that the string it is evaluated for starts with a given prefix.
doc_PHPUnit
2016-10-16 15:37:42
Comments
Leave a Comment

Please login to continue.