2. Writing tests
2.1. Asserters
2.1.1. variable
The base asserter for all variables. It contains all the tests you would need for any kind of variable.
2.1.1.1. isCallable
isCallable checks that the variable call be called like a function
$f = function() { // code }; $this ->variable($f) ->isCallable() // passes ->variable('\Vendor\Project\foobar') ->isCallable() ->isCallable() ->variable('\Vendor\Project\Foo::bar') ->isCallable() ;
2.1.1.2. isEqualTo
isEqualTo checks that the variable is equal to an expected value
$a = 'a'; $this ->variable($a) ->isEqualTo('a') // passes ;
isEqualTo does not check the variable type. If you want to also check the type, use isIdenticalTo.
2.1.1.3. isIdenticalTo
isIdenticalTo checks that the variable is equal to the expected value, and also checks that the types are the same. With objects, isIdenticalTo checks that both values are references to the same object
$a = '1'; $this ->variable($a) ->isIdenticalTo(1) // fails ; $stdClass1 = new \StdClass(); $stdClass2 = new \StdClass(); $stdClass3 = $stdClass1; $this ->variable($stdClass1) ->isIdenticalTo(stdClass3) // passes ->isIdenticalTo(stdClass2) // fails ;
isIdenticalTo checks the variable type. If you do not want to check the type, useisEqualTo.
2.1.1.4. isNotCallable
isNotCallable checks the variable cannot be called like a function.
$f = function() { // code }; $int = 1; $string = 'nonExistingMethod'; $this ->variable($f) ->isNotCallable() // fails ->variable($int) ->isNotCallable() // passes ->variable($string) ->isNotCallable() // passes ->variable(new StdClass) ->isNotCallable() // passes ;
2.1.1.5. isNotEqualTo
isNotEqualTo checks that the variable is not the same as the given value
$a = 'a'; $aString = '1'; $this ->variable($a) ->isNotEqualTo('b') // passes ->isNotEqualTo('a') // fails ->variable($aString) ->isNotEqualTo($1) // fails ;
isNotEqualTo does not check the variable type. If you also want to check the type, use isNotIdenticalTo.
2.1.1.6. isNotIdenticalTo
isNotIdenticalTo checks that the variable has neither the same type nor the same value as the given value
With objects, isNotIdenticalTo checks that both values do not reference the same instance.
$a = '1'; $this ->variable($a) ->isNotIdenticalTo(1) // passes ; $stdClass1 = new \StdClass(); $stdClass2 = new \StdClass(); $stdClass3 = $stdClass1; $this ->variable($stdClass1) ->isNotIdenticalTo(stdClass2) // passes ->isNotIdenticalTo(stdClass3) // fails ;
isNotIdenticalTo checks the variable type. If you do not want to check the variable type, use isNotEqualTo.
2.1.1.7. isNull
isNull checks that the variable is null.
$emptyString = ''; $null = null; $this ->variable($emptyString) ->isNull() // fails // (it is empty but not null) ->variable($null) ->isNull() // passes ;
2.1.1.8. isNotNull
isNotNull checks that the variable is not null.
$emptyString = ''; $null = null; $this ->variable($emptyString) ->isNotNull() // passe (it is empty but not null) ->variable($null) ->isNotNull() // fails ;
2.1.2. boolean
This is the asserter for booleans.
The check will fail if you pass a non boolean value.
null is not a boolean. You can read the PHP manual to know what is_bool considers a boolean or not.
2.1.2.1. isEqualTo
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.2.2. isFalse
isFalse checks that the boolean is strictly equal to false.
$true = true; $false = false; $this ->boolean($true) ->isFalse() // fails ->boolean($false) ->isFalse() // passes ;
2.1.2.3. isIdenticalTo
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.2.4. isNotEqualTo
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.2.5. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.2.6. isTrue
isTrue checks that the boolean is strictly equal to true.
$true = true; $false = false; $this ->boolean($true) ->isTrue() // passes ->boolean($false) ->isTrue() // fails ;
2.1.3. integer
This is the asserter for integers.
The check will fail if pass a non integer value.
null is not an integer. You can read the PHP manual to know what is_int considers an integer or not.
2.1.3.1. isEqualTo
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.3.2. isGreaterThan
isGreaterThan checks that the integer is strictly greater then the given value.
$zero = 0; $this ->integer($zero) ->isGreaterThan(-1) // passes ->isGreaterThan('-1') // fails because "-1" // is not an integer (string) ->isGreaterThan(0) // fails ;
2.1.3.3. isGreaterThanOrEqualTo
isGreaterThanOrEqualTo checks that the integer is greater or equal to the given value.
$zero = 0; $this ->integer($zero) ->isGreaterThanOrEqualTo(-1) // passes ->isGreaterThanOrEqualTo(0) // passes ->isGreaterThanOrEqualTo('-1') // fails because "-1" // is not an integer (string) ;
2.1.3.4. isIdenticalTo
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.3.5. isLessThan
isLessThan checks that the integer is strictly lower than the given value.
$zero = 0; $this ->integer($zero) ->isLessThan(10) // passes ->isLessThan('10') // fails because "10" is not an integer (string) ->isLessThan(0) // fails ;
2.1.3.6. isLessThanOrEqualTo
isLessThanOrEqualTo checks that the integer is less or equal than the given value.
$zero = 0; $this ->integer($zero) ->isLessThanOrEqualTo(10) // passes ->isLessThanOrEqualTo(0) // passes ->isLessThanOrEqualTo('10') // fails because "10" // is not an integer ;
2.1.3.7. isNotEqualTo
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.3.8. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.3.9. isZero
isZero checks that the integer is equal to 0.
$zero = 0; $notZero = -1; $this ->integer($zero) ->isZero() // passes ->integer($notZero) ->isZero() // fails ;
isZero is equivalent to isEqualTo(0).
2.1.4. float
This is the asserter for floats.
The check will fail if you pass a non float value.
null is not a float. Read the PHP manual to know what is_float considers a float or not.
2.1.4.1. isEqualTo
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.4.2. isGreaterThan
isGreaterThan is an inherited method from the integer asserter.
For more information, you can read the integer::isGreaterThan documentation
2.1.4.3. isGreaterThanOrEqualTo
isGreaterThanOrEqualTo is an inherited method from the integer asserter.
For more information, you can read the integer::isGreaterThanOrEqualTo documentation
2.1.4.4. isIdenticalTo
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.4.5. isLessThan
isLessThan is an inherited method from the integer asserter.
For more information, you can read the integer::isLessThan documentation
2.1.4.6. isLessThanOrEqualTo
isLessThanOrEqualTo is an inherited method from the integer asserter.
For more information, you can read the integer::isLessThanOrEqualoo documentation
2.1.4.7. isNearlyEqualTo
isNearlyEqualTo checks that the float is approximately equal to the given value.
Computers handle floats in a way that makes precise comparisons impossible without using advanced tools. Try for example the following command:
$ php -r 'var_dump(1 - 0.97 === 0.03);' bool(false)
The result should be true though.
For more information about this behavior, read the PHP manual
This method tries to avoid this issue.
$float = 1 - 0.97; $this ->float($float) ->isNearlyEqualTo(0.03) // passes ->isEqualTo(0.03) // fails ;
For more information about the algorithm used, read the floating point guide.
2.1.4.8. isNotEqualTo
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.4.9. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.4.10. isZero
isZero is an inherited method from the integer asserter.
For more information, you can read the integer::isZero documentation
2.1.5. sizeOf
This is the asserter for array sizes and objects that implements the Countable interface.
$countableObject = new GlobIterator('*'); $this ->isEqualTo(3) ->isGreaterThan(0) ;
2.1.5.1. isEqualTo
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.5.2. isGreaterThan
isGreaterThan is an inherited method from the integer asserter.
For more information, you can read the integer::isGreaterThan documentation
2.1.5.3. isGreaterThanOrEqualTo
isGreaterThanOrEqualTo is an inherited method from the integer asserter.
For more information, you can read the integer::isGreaterThanOrEqualTo documentation
2.1.5.4. isIdenticalTo
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.5.5. isLessThan
isLessThan is an inherited method from the integer asserter.
For more information, you can read the integer::isLessThan documentation
2.1.5.6. isLessThanOrEqualTo
isLessThanOrEqualTo is an inherited method from the integer asserter.
For more information, you can read the integer::isLessThanOrEqualoo documentation
2.1.5.7. isNotEqualTo
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.5.8. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.5.9. isZero
isZero is an inherited method from the integer asserter.
For more information, you can read the integer::isZero documentation
2.1.6. object
This is the asserter for objects.
The check will fail if you pass a non object.
null is not an object. Read the PHP manual to know what is_object considers an object or not.
2.1.6.1. hasSize
hasSize checks the size of objects that implement the Countable interface.
$countableObject = new GlobIterator('*'); $this ->object($countableObject) ->hasSize(3) ;
2.1.6.2. isCallable
class foo { public function __invoke() { // code } } $this ->object(new foo) ->isCallable() // passes ->object(new StdClass) ->isCallable() // fails ;
To be callable, your objects must be instantiated from classes that implement the __invoke magic method.
isCallable is an inherited method from the variable asserter.
For more information, you can read the variable::isCallable documentation
2.1.6.3. isCloneOf
isCloneOf checks that the object is the clone of the given object, that is to say the objects are equal, but are not the same instance.
$object1 = new \StdClass; $object2 = new \StdClass; $object3 = clone($object1); $object4 = new \StdClass; $object4->foo = 'bar'; $this ->object($object1) ->isCloneOf($object2) // passes ->isCloneOf($object3) // passes ->isCloneOf($object4) // fails ;
For more information on object comparison, read the PHP manual.
2.1.6.4. isEmpty
isEmpty checks that the size of an object that implements the Countable interface is equal to 0.
$countableObject = new GlobIterator('atoum.php'); $this ->object($countableObject) ->isEmpty() ;
isEmpty is equivalent to hasSize(0).
2.1.6.5. isEqualTo
isEqualTo checks that the object is equal to the given object.
Two objects are considered equal when they have the same attributes and attributes values, and that they are instances of the same class.
For more information on object comparison, read the PHP manual.
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.6.6. isIdenticalTo
isIdenticalTo checks that the objects are identical.
Two objects are considered identical when they are references to the same instance of the same class.
For more information on object comparison, read the PHP manual.
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.6.7. isInstanceOf
isInstanceOf checks that an object is :
- an instance of the given class,
- a subclass of the given class (abstract or not),
- an instance of a class that implements the given interface.
$object = new \StdClass(); $this ->object($object) ->isInstanceOf('\StdClass') // passes ->isInstanceOf('\Iterator') // fails ; interface FooInterface { public function foo(); } class FooClass implements FooInterface { public function foo() { echo "foo"; } } class BarClass extends FooClass { } $foo = new FooClass; $bar = new BarClass; $this ->object($foo) ->isInstanceOf('\FooClass') // passes ->isInstanceOf('\FooInterface') // passes ->isInstanceOf('\BarClass') // fails ->isInstanceOf('\StdClass') // fails ->object($bar) ->isInstanceOf('\FooClass') // passes ->isInstanceOf('\FooInterface') // passes ->isInstanceOf('\BarClass') // passes ->isInstanceOf('\StdClass') // fails ;
Classes and interfaces names have to be absolute, because namespace import are not taken into account.
2.1.6.8. isNotCallable
class foo { public function __invoke() { // code } } $this ->variable(new foo) ->isNotCallable() // fails ->variable(new StdClass) ->isNotCallable() // passes ;
isNotCallable is an inherited method from the variable asserter.
For more information, you can read the variable::isNotCallable documentation
2.1.6.9. isNotEqualTo
isEqualTo checks that the object is not equal to the given object.
Two objects are considered equal when they have the same attributes and attributes values, and that they are instances of the same class.
For more information on object comparison, read the PHP manual.
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.6.10. isNotIdenticalTo
isIdenticalTo checks that the object is not identical to the given object.
Two objects are considered identical when they are references to the same instance of the same class.
For more information on object comparison, read the PHP manual.
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.7. dateInterval
This is the asserter for the DateInterval object.
The check will fail if you pass a value that is not a DateInterval instance (or an instance of a class that extends it).
2.1.7.1. isCloneOf
isCloneOf is an inherited method from the object asserter.
For more information, you can read the object::isCloneOf documentation
2.1.7.2. isEqualTo
isEqualTo checks that the duration of the DateInterval object is equal to the duration of the given DateInterval object.
$di = new DateInterval('P1D'); $this ->dateInterval($di) ->isEqualTo( // passes new DateInterval('P1D') ) ->isEqualTo( // fails new DateInterval('P2D') ) ;
2.1.7.3. isGreaterThan
isGreaterThan checks that the duration of the DateInterval object is greater than the duration of the given DateInterval object.
$di = new DateInterval('P2D'); $this ->dateInterval($di) ->isGreaterThan( // passes new DateInterval('P1D') ) ->isGreaterThan( // fails new DateInterval('P2D') ) ;
2.1.7.4. isGreaterThanOrEqualTo
isGreaterThanOrEqualTo checks that the duration of the DateInterval object is greater or equal to the duration of the given DateInterval object.
$di = new DateInterval('P2D'); $this ->dateInterval($di) ->isGreaterThanOrEqualTo( // passes new DateInterval('P1D') ) ->isGreaterThanOrEqualTo( // passes new DateInterval('P2D') ) ->isGreaterThanOrEqualTo( // fails new DateInterval('P3D') ) ;
2.1.7.5. isIdenticalTo
isIdenticalTo is an inherited method from the object asserter.
For more information, you can read the object::isIdenticalTo documentation
2.1.7.6. isInstanceOf
isInstanceOf is an inherited method from the object asserter.
For more information, you can read the object::isInstanceOf documentation
2.1.7.7. isLessThan
isLessThan checks that the duration of the DateInterval object is less than the duration of the given DateInterval object.
$di = new DateInterval('P1D'); $this ->dateInterval($di) ->isLessThan( // passes new DateInterval('P2D') ) ->isLessThan( // fails new DateInterval('P1D') ) ;
2.1.7.8. isLessThanOrEqualTo
isLessThanOrEqualTo checks that the duration of the DateInterval object is less or equal than the duration of the given DateInterval object.
$di = new DateInterval('P2D'); $this ->dateInterval($di) ->isLessThanOrEqualTo( // passes new DateInterval('P3D') ) ->isLessThanOrEqualTo( // passes new DateInterval('P2D') ) ->isLessThanOrEqualTo( // fails new DateInterval('P1D') ) ;
2.1.7.9. isNotEqualTo
isNotEqualTo is an inherited method from the object asserter.
For more information, you can read the object::isNotEqualTo documentation
2.1.7.10. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the object asserter.
For more information, you can read the object::isNotIdenticalTo documentation
2.1.7.11. isZero
isZero checks that the duration of the DateInterval is equal to 0.
$di1 = new DateInterval('P0D'); $di2 = new DateInterval('P1D'); $this ->dateInterval($di1) ->isZero() // passes ->dateInterval($di2) ->isZero() // fails ;
2.1.8. dateTime
This is the asserter for the DateTime object.
The check will fail if you pass a value that is not an instance of DateTime (or an instance of a class that extends it).
2.1.8.1. hasDate
hasDate checks the date part of the DateTime object.
$dt = new DateTime('1981-02-13'); $this ->dateTime($dt) ->hasDate('1981', '02', '13') // passes ->hasDate('1981', '2', '13') // passes ->hasDate(1981, 2, 13) // passes ;
2.1.8.2. hasDateAndTime
hasDateAndTime check the date and time of the DateTime object.
$dt = new DateTime('1981-02-13 01:02:03'); $this ->dateTime($dt) // passes ->hasDateAndTime('1981', '02', '13', '01', '02', '03') // passes ->hasDateAndTime('1981', '2', '13', '1', '2', '3') // passes ->hasDateAndTime(1981, 2, 13, 1, 2, 3) ;
2.1.8.3. hasDay
hasDay checks the day of the DateTime object.
$dt = new DateTime('1981-02-13'); $this ->dateTime($dt) ->hasDay(13) // passes ;
2.1.8.4. hasHours
hasHours checks the hours of the DateTime object.
$dt = new DateTime('01:02:03'); $this ->dateTime($dt) ->hasHours('01') // passes ->hasHours('1') // passes ->hasHours(1) // passes ;
2.1.8.5. hasMinutes
hasMinutes checks the minutes of the DateTime object.
$dt = new DateTime('01:02:03'); $this ->dateTime($dt) ->hasMinutes('02') // passes ->hasMinutes('2') // passes ->hasMinutes(2) // passes ;
2.1.8.6. hasMonth
hasMonth checks the month of the DateTime object.
$dt = new DateTime('1981-02-13'); $this ->dateTime($dt) ->hasMonth(2) // passes ;
2.1.8.7. hasSeconds
hasSeconds checks the seconds of the DateTime object.
$dt = new DateTime('01:02:03'); $this ->dateTime($dt) ->hasSeconds('03') // passes ->hasSeconds('3') // passes ->hasSeconds(3) // passes ;
2.1.8.8. hasTime
hasTime checks the time part of the DateTime object.
$dt = new DateTime('01:02:03'); $this ->dateTime($dt) ->hasTime('01', '02', '03') // passes ->hasTime('1', '2', '3') // passes ->hasTime(1, 2, 3) // passes ;
2.1.8.9. hasTimezone
hasTimezone checks the timezone of the DateTime object.
$dt = new DateTime(); $this ->dateTime($dt) ->hasTimezone('Europe/Paris') ;
2.1.8.10. hasYear
hasYear checks the year of the DateTime object.
$dt = new DateTime('1981-02-13'); $this ->dateTime($dt) ->hasYear(1981) // passes ;
2.1.8.11. isCloneOf
isCloneOf is an inherited method from the object asserter.
For more information, you can read the object::isCloneOf documentation
2.1.8.12. isEqualTo
isEqualTo is an inherited method from the object asserter.
For more information, you can read the object::isEqualTo documentation
2.1.8.13. isIdenticalTo
isIdenticalTo is an inherited method from the object asserter.
For more information, you can read the object::isIdenticalTo documentation
2.1.8.14. isInstanceOf
isInstanceOf is an inherited method from the object asserter.
For more information, you can read the object::isInstanceOf documentation
2.1.8.15. isNotEqualTo
isNotEqualTo is an inherited method from the object asserter.
For more information, you can read the object::isNotEqualTo documentation
2.1.8.16. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the object asserter.
For more information, you can read the object::isNotIdenticalTo documentation
2.1.9. mysqlDateTime
This is the asserter for objects representing a MySQL date, based on the DateTime object.
The date must use a format compatible with MySQL and other DBMS, in particular « Y-m-d H:i:s » (for more information read the date() function document on the PHP manual).
The check will fail if you pass a value that is not a DateTime object (or an instance of a class that extends it).
2.1.9.1. hasDate
hasDate is an inherited method from the dateTime asserter.
For more information, you can read the dateTime::hasDate documentation
2.1.9.2. hasDateAndTime
hasDateAndTime is an inherited method from the dateTime asserter.
For more information, you can read the dateTime::hasDateAndTime documentation
2.1.9.3. hasDay
hasDay is an inherited method from the dateTime asserter.
For more information, you can read the dateTime::hasDay documentation
2.1.9.4. hasHours
hasHours is an inherited method from the dateTime asserter.
For more information, you can read the dateTime::hasHours documentation
2.1.9.5. hasMinutes
hasMinutes is an inherited method from the dateTime asserter.
For more information, you can read the dateTime::hasMinutes documentation
2.1.9.6. hasMonth
hasMonth is an inherited method from the dateTime asserter.
For more information, you can read the dateTime::hasMonth documentation
2.1.9.7. hasSeconds
hasSeconds is an inherited method from the dateTime asserter.
For more information, you can read the dateTime::hasSeconds documentation
2.1.9.8. hasTime
hasTime is an inherited method from the dateTime asserter.
For more information, you can read the dateTime::hasTime documentation
2.1.9.9. hasTimezone
hasTimezone is an inherited method from the dateTime asserter.
For more information, you can read the dateTime::hasTimezone documentation
2.1.9.10. hasYear
hasYear is an inherited method from the dateTime asserter.
For more information, you can read the dateTime::hasYear documentation
2.1.9.11. isCloneOf
isCloneOf is an inherited method from the object asserter.
For more information, you can read the object::isCloneOf documentation
2.1.9.12. isEqualTo
isEqualTo is an inherited method from the object asserter.
For more information, you can read the object::isEqualTo documentation
2.1.9.13. isIdenticalTo
isIdenticalTo is an inherited method from the object asserter.
For more information, you can read the object::isIdenticalTo documentation
2.1.9.14. isInstanceOf
isInstanceOf is an inherited method from the object asserter.
For more information, you can read the object::isInstanceOf documentation
2.1.9.15. isNotEqualTo
isNotEqualTo is an inherited method from the object asserter.
For more information, you can read the object::isNotEqualTo documentation
2.1.9.16. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the object asserter.
For more information, you can read the object::isNotIdenticalTo documentation
2.1.10. exception
This is the asserter for exceptions.
$this ->exception( function() use($myObject) { // this throws an exception: throw new \Exception; $myObject->doOneThing('wrongParameter'); } ) ;
The syntax use anonymous functions (also named closures) introduced in PHP 5.3. For more information read the PHP manual.
2.1.10.1. hasCode
hasCode checks the exception code
$this ->exception( function() use($myObject) { // this throws an exception: throw new \Exception('Message', 42); $myObject->doOneThing('wrongParameter'); } ) ->hasCode(42) ;
2.1.10.2. hasDefaultCode
hasDefaultCode checks that the exception code is the default value, 0.
$this ->exception( function() use($myObject) { // this throws an exception: throw new \Exception; $myObject->doOneThing('wrongParameter'); } ) ->hasDefaultCode() ;
hasDefaultCode is equivalent to hasCode(0).
2.1.10.3. hasMessage
hasMessage checks the exception message
$this ->exception( function() use($myObject) { // this throws an exception: throw new \Exception('Message'); $myObject->doOneThing('wrongParameter'); } ) ->hasMessage('Message') // passes ->hasMessage('message') // fails ;
2.1.10.4. hasNestedException
hasNestedException checks that the exception contains a reference to the previous exception. If the exception class is given, it will also check the exception class.
$this ->exception( function() use($myObject) { // this throws an exception: throw new \Exception('Message'); $myObject->doOneThing('wrongParameter'); } ) ->hasNestedException() // fails ->exception( function() use($myObject) { try { // this throws an exception: throw new \FirstException('Message 1', 42); $myObject->doOneThing('wrongParameter'); } // ... the exception is catched catch(\FirstException $e) { // ... then thrown again, wrapped in a second exception throw new \SecondException('Message 2', 24, $e); } } ) ->isInstanceOf('\FirstException') // fails ->isInstanceOf('\SecondException') // passes ->hasNestedException() // passes ->hasNestedException(new \FirstException) // passes ->hasNestedException(new \SecondException) // fails ;
2.1.10.5. isCloneOf
isCloneOf is an inherited method from the object asserter.
For more information, you can read the object::isCloneOf documentation
2.1.10.6. isEqualTo
isEqualTo is an inherited method from the object asserter.
For more information, you can read the object::isEqualTo documentation
2.1.10.7. isIdenticalTo
isIdenticalTo is an inherited method from the object asserter.
For more information, you can read the object::isIdenticalTo documentation
2.1.10.8. isInstanceOf
isInstanceOf is an inherited method from the object asserter.
For more information, you can read the object::isInstanceOf documentation
2.1.10.9. isNotEqualTo
isNotEqualTo is an inherited method from the object asserter.
For more information, you can read the object::isNotEqualTo documentation
2.1.10.10. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the object asserter.
For more information, you can read the object::isNotIdenticalTo documentation
2.1.10.11. message
message gives you an asserter of type string containing the thrown exception message
$this ->exception( function() { throw new \Exception('My custom message to test'); } ) ->message ->contains('message') ;
2.1.11. array
This is the asserter for arrays.
array being a PHP reserved keyword, it was not possible to create an array asserter class. That's why its name is actually phpArray. You may encounter some ->phpArray() or des ->array().
It is advised to only use ->array() to simplify test reading.
2.1.11.1. contains
contains checks that an array contains the given value.
$this ->contains('1') // passes ->contains(1) // passes, because it does not ... ->contains('2') // ... check the type ->contains(10) // fails ;
contains does not search recursively.
contains does not check the type. If you want to check the type, use strictlyContains.
2.1.11.2. containsValues
containsValues checks that an array contains all the values of the given array.
$this ;
containsValues does not search recursively.
containsValues does not check the type. If you want to check the type, use strictlyContainsValues.
2.1.11.3. hasKey
hasKey checks that the array contains the given key.
'name' => 'atoum', 'owner' => 'mageekguy', ); $this ->hasKey(0) // passes ->hasKey(1) // passes ->hasKey('1') // passes ->hasKey(10) // fails ->hasKey('name') // passes ->hasKey('price') // fails ;
hasKey does not search recursively.
hasKey does not check the type..
2.1.11.4. hasKeys
hasKeys checks that the keys of the array contains all the values of the given array.
'name' => 'atoum', 'owner' => 'mageekguy', ); $this ;
hasKeys does not search recursively.
hasKeys does not check the type.
2.1.11.5. hasSize
hasSize checks the array size.
$this ->hasSize(7) // passes ->hasSize(10) // fails ;
hasSize is not recursive.
2.1.11.6. isEmpty
isEmpty checks that the array is empty.
$this ->isEmpty() // passes ->isEmpty() // fails ;
2.1.11.7. isEqualTo
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.11.8. isIdenticalTo
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.11.9. isNotEmpty
isNotEmpty checks that an array is not empty.
$this ->isNotEmpty() // fails ->isNotEmpty() // passes ;
2.1.11.10. isNotEqualTo
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.11.11. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.11.12. keys
keys gives you an array asserter containing the keys of the array.
'name' => 'atoum', 'owner' => 'mageekguy', ); $this ->keys ->isEqualTo( 'name', 'owner', ) ) ;
2.1.11.13. notContains
notContains checks that an array does not contains the given value.
$this ->notContains(null) // passes ->notContains(1) // fails ->notContains(10) // passes ;
notContains does not search recursively.
notContains does not check the type. If you want to also check the type, use strictlyNotContains.
2.1.11.14. notContainsValues
notContainsValues checks that the array does not contain any value of the given array.
$this ;
notContainsValues does not search recursively.
notContainsValues does not check the type. If you want to also check the type, use strictlyNotContainsValues.
2.1.11.15. notHasKey
notHasKey checks that an array does not contain the given key.
'name' => 'atoum', 'owner' => 'mageekguy', ); $this ->notHasKey(0) // fails ->notHasKey(1) // fails ->notHasKey('1') // fails ->notHasKey(10) // passes ->notHasKey('name') // fails ->notHasKey('price') // passes ;
notHasKey does not search recursively.
notHasKey does not check the type.
2.1.11.16. notHasKeys
notHasKeys checks that the array keys does not contain any of the given values.
'name' => 'atoum', 'owner' => 'mageekguy', ); $this ;
notHasKeys does not search recursively.
notHasKeys does not check the type.
2.1.11.17. size
size gives you an integer asserter containing the array size.
$this ->size ->isGreaterThan(5) ;
2.1.11.18. strictlyContains
strictlyContains checks that an array strictly contains the given value (same value and type).
$this ->strictlyContains('1') // passes ->strictlyContains(1) // fails ->strictlyContains('2') // fails ->strictlyContains(2) // passes ->strictlyContains(10) // fails ;
strictlyContains does not search recursively.
strictlyContains checks the type. If you do not want to check the type, use contains.
2.1.11.19. strictlyContainsValues
strictlyContainsValues checks that an array strictly contains of all the given values (same value and type).
$this ;
strictlyContainsValues does not search recursively.
strictlyContainsValues checks the type. If you do not want to check the type, use containsValues.
2.1.11.20. strictlyNotContains
strictlyNotContains checks that the array strictly does not contain the given value (same value and type).
$this ->strictlyNotContains(null) // passes ->strictlyNotContains('1') // fails ->strictlyNotContains(1) // passes ->strictlyNotContains(10) // passes ;
strictlyNotContains does not search recursively.
strictlyNotContains checks the type. If you do not want to check the type, use notContains.
2.1.11.21. strictlyNotContainsValues
strictlyNotContainsValues checks that an array strictly does not contain any of the given values (same value and type).
$this ;
strictlyNotContainsValues does not search recursively.
strictlyNotContainsValues checks the type. If you do not want to check the type, use notContainsValues.
2.1.12. string
This is the asserter for strings.
2.1.12.1. contains
contains checks that the string contains the given string.
$string = 'Hello world'; $this ->string($string) ->contains('ll') // passes ->contains(' ') // passes ->contains('php') // fails ;
2.1.12.2. hasLength
hasLength checks the string length.
$string = 'Hello world'; $this ->string($string) ->hasLength(11) // passes ->hasLength(20) // fails ;
2.1.12.3. hasLengthGreaterThan
hasLengthGreaterThan checks that the string length is greater than the given value.
$string = 'Hello world'; $this ->string($string) ->hasLengthGreaterThan(10) // passes ->hasLengthGreaterThan(20) // fails ;
2.1.12.4. hasLengthLessThan
hasLengthLessThan checks that the string length is less than the given value.
$string = 'Hello world'; $this ->string($string) ->hasLengthLessThan(20) // passes ->hasLengthLessThan(10) // fails ;
2.1.12.5. isEmpty
isEmpty checks that the string is empty.
$emptyString = ''; $nonEmptyString = 'atoum'; $this ->string($emptyString) ->isEmpty() // passes ->string($nonEmptyString) ->isEmpty() // fails ;
2.1.12.6. isEqualTo
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.12.7. isEqualToContentsOfFile
isEqualToContentsOfFile checks that the string is equal to the content of the given file path.
$this ->string($string) ->isEqualToContentsOfFile('/path/to/file') ;
The test fails if the file does not exist.
2.1.12.8. isIdenticalTo
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.12.9. isNotEmpty
isNotEmpty checks that the string is not empty.
$emptyString = ''; $nonEmptyString = 'atoum'; $this ->string($emptyString) ->isNotEmpty() // fails ->string($nonEmptyString) ->isNotEmpty() // passes ;
2.1.12.10. isNotEqualTo
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.12.11. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.12.12. length
length gives you an integer asserter containing the string length.
$string = 'atoum' $this ->string($string) ->length ->isGreaterThanOrEqualTo(5) ;
2.1.12.13. match
match checks that the string matches a regular expression.
$phone = '0102030405'; $vdm = "Aujourd'hui, à 57 ans, mon père s'est fait tatouer une licorne sur l'épaule. VDM"; $this ->string($phone) ->match('#^0[1-9]\d{8}$#') ->string($vdm) ->match("#^Aujourd'hui.*VDM$#") ;
2.1.12.14. notContains
notContains checks that the string does not contain the given string.
$string = 'Hello world'; $this ->string($string) ->notContains('php') // passes ->notContains(';') // passes ->notContains('ll') // fails ->notContains(' ') // fails ;
2.1.13. castToString
This is the asserter for casting objects to sting.
class AtoumVersion { private $version = '1.0'; public function __toString() { return 'atoum v' . $this->version; } } $this ->castToString(new AtoumVersion()) ->isEqualTo('atoum v1.0') ;
2.1.13.1. contains
contains is an inherited method from the string asserter.
For more information, you can read the string::contains documentation
2.1.13.2. notContains
notContains is an inherited method from the string asserter.
For more information, you can read the string::notContains documentation
2.1.13.3. hasLength
hasLength is an inherited method from the string asserter.
For more information, you can read the string::hasLength documentation
2.1.13.4. hasLengthGreaterThan
hasLengthGreaterThan is an inherited method from the string asserter.
For more information, you can read the string::hasLengthGreaterThan documentation
2.1.13.5. hasLengthLessThan
hasLengthLessThan is an inherited method from the string asserter.
For more information, you can read the string::hasLengthLessThan documentation
2.1.13.6. isEmpty
isEmpty is an inherited method from the string asserter.
For more information, you can read the string::isEmpty documentation
2.1.13.7. isEqualTo
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.13.8. isEqualToContentsOfFile
isEqualToContentsOfFile is an inherited method from the string asserter.
For more information, you can read the string::isEqualToContentsOfFile documentation
2.1.13.9. isIdenticalTo
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.13.10. isNotEmpty
isNotEmpty is an inherited method from the string asserter.
For more information, you can read the string::isNotEmpty documentation
2.1.13.11. isNotEqualTo
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.13.12. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.13.13. match
match is an inherited method from the string asserter.
For more information, you can read the string::match documentation
2.1.14. hash
This is the asserter for hashes.
2.1.14.1. contains
contains is an inherited method from the string asserter.
For more information, you can read the string::contains documentation
2.1.14.2. isEqualTo
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.14.3. isEqualToContentsOfFile
isEqualToContentsOfFile is an inherited method from the string asserter.
For more information, you can read the string::isEqualToContentsOfFile documentation
2.1.14.4. isIdenticalTo
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.14.5. isMd5
isMd5 checks that the string is a valid md5, an hexadecimal string of 32 characters.
$notHash = 'atoum'; $this ->isMd5() // passes ->isMd5() // fails ;
2.1.14.6. isNotEqualTo
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.14.7. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.14.8. isSha1
isSha1 checks that the string is a sha1, an hexadecimal string of 40 characters.
$notHash = 'atoum'; $this ->isSha1() // passes ->isSha1() // fails ;
2.1.14.9. isSha256
isSha256 checks that the string is a sha256, an hexadecimal string of 64 characters.
$notHash = 'atoum'; $this ->isSha256() // passes ->isSha256() // fails ;
2.1.14.10. isSha512
isSha512 checks that the string is a sha512, an hexadecimal string of 128 characeters.
$notHash = 'atoum'; $this ->isSha512() // passes ->isSha512() // fails ;
2.1.14.11. notContains
notContains is an inherited method from the string asserter.
For more information, you can read the string::notContains documentation
2.1.15. output
This is the asserter for output streams, that is supposed to be displayed on the screen.
$this ->output( function() { echo 'Hello world'; } ) ;
The syntax use anonymous functions (also named closures) introduced in PHP 5.3. For more information read the PHP manual.
2.1.15.1. contains
contains is an inherited method from the string asserter.
For more information, you can read the string::contains documentation
2.1.15.2. hasLength
hasLength is an inherited method from the string asserter.
For more information, you can read the string::hasLength documentation
2.1.15.3. hasLengthGreaterThan
hasLengthGreaterThan is an inherited method from the string asserter.
For more information, you can read the string::hasLengthGreaterThan documentation
2.1.15.4. hasLengthLessThan
hasLengthLessThan is an inherited method from the string asserter.
For more information, you can read the string::hasLengthLessThan documentation
2.1.15.5. isEmpty
isEmpty is an inherited method from the string asserter.
For more information, you can read the string::isEmpty documentation
2.1.15.6. isEqualTo
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.15.7. isEqualToContentsOfFile
isEqualToContentsOfFile is an inherited method from the string asserter.
For more information, you can read the string::isEqualToContentsOfFile documentation
2.1.15.8. isIdenticalTo
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.15.9. isNotEmpty
isNotEmpty is an inherited method from the string asserter.
For more information, you can read the string::isNotEmpty documentation
2.1.15.10. isNotEqualTo
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.15.11. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.15.12. match
match is an inherited method from the string asserter.
For more information, you can read the string::match documentation
2.1.15.13. notContains
notContains is an inherited method from the string asserter.
For more information, you can read the string::notContains documentation
2.1.16. utf8String
This is the asserter for UTF-8 strings.
utf8Strings uses the mb_* functions to handle multi-bytes strings. Read the PHP manual for more information about the extension mbstring.
2.1.16.1. contains
contains is an inherited method from the string asserter.
For more information, you can read the string::contains documentation
2.1.16.2. hasLength
hasLength is an inherited method from the string asserter.
For more information, you can read the string::hasLength documentation
2.1.16.3. hasLengthGreaterThan
hasLengthGreaterThan is an inherited method from the string asserter.
For more information, you can read the string::hasLengthGreaterThan documentation
2.1.16.4. hasLengthLessThan
hasLengthLessThan is an inherited method from the string asserter.
For more information, you can read the string::hasLengthLessThan documentation
2.1.16.5. isEmpty
isEmpty is an inherited method from the string asserter.
For more information, you can read the string::isEmpty documentation
2.1.16.6. isEqualTo
isEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isEqualTo documentation
2.1.16.7. isEqualToContentsOfFile
isEqualToContentsOfFile is an inherited method from the string asserter.
For more information, you can read the string::isEqualToContentsOfFile documentation
2.1.16.8. isIdenticalTo
isIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isIdenticalTo documentation
2.1.16.9. isNotEmpty
isNotEmpty is an inherited method from the string asserter.
For more information, you can read the string::isNotEmpty documentation
2.1.16.10. isNotEqualTo
isNotEqualTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotEqualTo documentation
2.1.16.11. isNotIdenticalTo
isNotIdenticalTo is an inherited method from the variable asserter.
For more information, you can read the variable::isNotIdenticalTo documentation
2.1.16.12. match
match is an inherited method from the string asserter.
For more information, you can read the string::match documentation
Don't forget to add the u to your regular expression. For more information read the PHP manual.
$vdm = "Aujourd'hui, à 57 ans, mon père s'est fait tatouer une licorne sur l'épaule. VDM"; $this ->utf8String($vdm) ->match("#^Aujourd'hui.*VDM$#u") ;
2.1.16.13. notContains
notContains is an inherited method from the string asserter.
For more information, you can read the string::notContains documentation
2.1.17. afterDestructionOf
This is the asserter for object destruction.
The asserter only receives an object, make sure the __destruct() method is defined and call it.
If __destruct() exists and calling does not raise any error or exception, the test will pass.
$this ->afterDestructionOf($objectWithDestructor) // passes ->afterDestructionOf($objectWithoutDestructor) // fails ;
2.1.18. error
This is the asserter for errors.
$this ->when( function() { } ) ->error() ->exists() // or notExists ;
The syntax use anonymous functions (also named closures) introduced in PHP 5.3. For more information read the PHP manual.
The errors types E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING along with most of the E_STRICT can't be handled by this function.
2.1.18.1. exists
exists checks that an error has been raised when calling the anonymous function.
$this ->when( function() { } ) ->error() ->exists() // passes ->when( function() { // code sans erreur } ) ->error() ->exists() // fails ;
2.1.18.2. notExists
notExists checks that no error has been raised when calling the anonymous function.
$this ->when( function() { } ) ->error() ->notExists() // fails ->when( function() { // no error there } ) ->error() ->notExists() // passes ;
2.1.18.3. withType
withType checks the raised error type.
$this ->when( function() { } ) ->error() ->withType(E_USER_NOTICE) // passes ->withType(E_USER_WARNING) // fails ;
2.1.19. class
This is the asserter for classes.
$object = new \StdClass; $this ->class('\StdClass') ;
class being a reserved PHP keyword, it wasn't possible to create a class asserter. It is actually named phpClass and a class alias has been added. You may encounter ->phpClass() or ->class().
It is advised to only use ->class().
2.1.19.1. hasInterface
hasInterface checks that the class implements the given interface.
$this ->class('\ArrayIterator') ->hasInterface('Countable') // passes ->class('\StdClass') ->hasInterface('Countable') // fails ;
2.1.19.2. hasMethod
hasMethod checks that the class contains the given method.
$this ->class('\ArrayIterator') ->hasMethod('count') // passes ->class('\StdClass') ->hasMethod('count') // fails ;
2.1.19.3. hasNoParent
hasNoParent checks that the class does not inherit from any class.
$this ->class('\StdClass') ->hasNoParent() // passes ->class('\FilesystemIterator') ->hasNoParent() // fails ;
A class can implements one or more interface while not inheriting from any class. hasNoParent does not check implementd interfaces, only inherited classes.
2.1.19.4. hasParent
hasParent checks that the class inherits from a class.
$this ->class('\StdClass') ->hasParent() // fails ->class('\FilesystemIterator') ->hasParent() // passes ;
A class can implements one or more interface while not inheriting from any class. hasParent does not check implementd interfaces, only inherited classes.
2.1.19.5. isAbstract
isAbstract checks that the class is abstract.
$this ->class('\StdClass') ->isAbstract() // fails ;
2.1.19.6. isSubclassOf
isSubclassOf checks that the class inherits from the given class.
$this ->class('\FilesystemIterator') ->isSubclassOf('\DirectoryIterator') // passes ->isSubclassOf('\SplFileInfo') // passes ->isSubclassOf('\StdClass') // fails ;
2.1.20. mock
This is the asserter for mocks.
$mock = new \mock\MyClass; $this ->mock($mock) ;
For more information on how to create mocks see Mocks;
2.1.20.1. call
call let you specify which method of the mock to check
$mock = new \mock\MyFirstClass; $this ->object(new MySecondClass($mock)) ->mock($mock) ->call('myMethod') ->once() ;
2.1.20.1.1. atLeastOnce
atLeastOnce check that the tested method (see call) has been called at least once.
$mock = new \mock\MyFirstClass; $this ->object(new MySecondClass($mock)) ->mock($mock) ->call('myMethod') ->atLeastOnce() ;
2.1.20.1.2. exactly
exactly check that the tested method (see call) has been called a specific number of times.
$mock = new \mock\MyFirstClass; $this ->object(new MySecondClass($mock)) ->mock($mock) ->call('myMethod') ->exactly(2) ;
2.1.20.1.3. never
never check that the tested method (see call) has never been called.
$mock = new \mock\MyFirstClass; $this ->object(new MySecondClass($mock)) ->mock($mock) ->call('myMethod') ->never() ;
never is equivalent to exactly(0).
2.1.20.1.4. once/twice/thrice
This asserters check that the tested method (see call) has been called exactly:
- once
- twice
- thrice
$mock = new \mock\MyFirstClass; $this ->object(new MySecondClass($mock)) ->mock($mock) ->call('myMethod') ->once() ->call('mySecondMethod') ->twice() ->call('myThirdMethod') ->thrice() ;
2.1.20.1.5. withAnyArguments
withAnyArguments let you not specify the expected argument when the tested method is called (see call).
This is especially useful to reset arguments, like this example:
$mock = new \mock\MyFirstClass; $this ->object(new MySecondClass($mock)) ->mock($mock) ->call('myMethod') ->withArguments('first') ->once() ->withArguments('second') ->once() ->withAnyArguments()->exactly(2) ;
2.1.20.1.6. withArguments
withArguments let you specify the expected arguments that tested method should receive when called (see call).
$mock = new \mock\MyFirstClass; $this ->object(new MySecondClass($mock)) ->mock($mock) ->call('myMethod') ->withArguments('first', 'second')->once() ;
withArguments does not check the arguments type. If you also want to check the type, use withIdenticalArguments.
2.1.20.1.7. withIdenticalArguments
withIdenticalArguments let you specify the expected arguments that tested method should receive when called (see call).
$mock = new \mock\MyFirstClass; $this ->object(new MySecondClass($mock)) ->mock($mock) ->call('myMethod') ->withIdenticalArguments('first', 'second')->once() ;
withIdenticalArguments checks the arguments type. If you do not want to check the type, use withArguments.
2.1.20.2. wasCalled
wasCalled checks that at least one method of the mock has been called at least once.
$mock = new \mock\MyFirstClass; $this ->object(new MySecondClass($mock)) ->mock($mock) ->wasCalled() ;
2.1.20.3. wasNotCalled
wasNotCalled checks that no method of the mock has been called.
$mock = new \mock\MyFirstClass; $this ->object(new MySecondClass($mock)) ->mock($mock) ->wasNotCalled() ;
2.1.21. stream
This is the asserter for streams.
Unfortunately, I do not know how it works, feel free to contribute!
2.1.21.1. isRead
We need help to write this section !
2.1.21.2. isWrite
We need help to write this section !
2.2. Writing help
There are several ways to write unit tests with atoum, and one of them is to use keywords like if, and or then, when or assert.
2.2.1. if, and, then
Usage of this keywords is really intuitive:
$this ->if($computer = new computer())) ->and($computer->setFirstOperand(2)) ->and($computer->setSecondOperand(2)) ->then ->object($computer->add()) ->isIdenticalTo($computer) ->integer($computer->getResult()) ->isEqualTo(4) ;
It is important to note that theses keywords do change anything technically or functionally. Their only goal is to ease the test comprehension, which next developers will be thankful for :).
Thereby, if et and let you define the prior conditions so that the assertions that follow the then keyword pass.
However, there is no grammar defining the order these keywords are used, and no syntax check is done by atoum.
It is the developer responsibility to use them wisely, though it is possible to write tests like this:
$this ->and($computer = new computer())) ->and($computer->setFirstOperand(2)) ->then ->if($computer->setSecondOperand(2)) ->object($computer->add()) ->isIdenticalTo($computer) ->integer($computer->getResult()) ->isEqualTo(4) ;
For the same reasons, the use of then is optional.
It is also important to note that it is possible to write the same test without any keyword:
$computer = new computer(); $computer->setFirstOperand(2); $computer->setSecondOperand(2); $this ->object($computer->add()) ->isIdenticalTo($computer) ->integer($computer->getResult()) ->isEqualTo(4) ;
There is not speed difference, the only important thing is to chose one way of doing it and stick with it.
2.2.2. when
In addition to if, and and then, there are other keywords.
One of them is when. It adds a feature to get around the fact that it is forbidden to write the following code in PHP:
$this ->then ->isZero() ;
PHP will raise the following fatal error: Parse error: syntax error, unexpected 'unset' (T_UNSET), expecting »)'
It is impossible to use unset() as a function argument.
To fix this problem the when keyword is capable of evaluating the anonymous function that you may pass as an argument. The previous may then be written like this:
$this ->when( function() use ($object) { } ) ->then ->isZero() ;
Of course, if when does not receive any anonymous function, it will behave exactly like if, and and then.
2.2.3. assert
Finally, there is also the assert keyword.
The following test will be used to illustrate its usage:
$this ->if($foo = new \mock\foo()) ->and($bar = new bar($foo)) ->and($bar->doSomething()) ->then ->mock($foo) ->call('doOtherThing') ->once() ->then ->mock($foo) ->call('doOtherThing') ->exactly(2) ;
This test has a drawback maintenance wise, because if the developer wants to add one or more new calls to bar::doOtherThing() between the two existing calls, he will have to update the value given to exactly().
To fix this issue, you can reset a mock using 2 ways:
- using $mock->getMockController()->resetCalls() ;
- using utilisant $this->resetMock($mock).
$this ->if($foo = new \mock\foo()) ->and($bar = new bar($foo)) ->and($bar->doSomething()) ->then ->mock($foo) ->call('doOtherThing') ->once() // 1st way ->if($foo->getMockController()->resetCalls()) ->then ->mock($foo) ->call('doOtherThing') ->once() // 2nd way ->if($this->resetMock($foo)) ->then ->mock($foo) ->call('doOtherThing') ->once() ;
These methods reset the controller memory, it is then possible to write the next assertion as if the mock was never called.
The assert keyword let you avoid having to explicitly call resetCalls() and it also triggers the memory reset of all the adapters and mock controllers defined when it is used.
Thanks to this feature, it is possible to write the previous test in a more readable simpler way by passing a string describing the next assertions.
$this ->if($foo = new \mock\foo()) ->and($bar = new bar($foo)) ->and($bar->doSomething()) ->then ->mock($foo) ->call('doOtherThing') ->once() ->then ->mock($foo) ->call('doOtherThing') ->once() ;
The string will be used by atoum in atoum generated messages if one of the assertions fail.