atoum's documentation

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

  1. $f = function() {
  2. // code
  3. };
  4.  
  5. $this
  6. ->variable($f)
  7. ->isCallable() // passes
  8.  
  9. ->variable('\Vendor\Project\foobar')
  10. ->isCallable()
  11.  
  12. ->variable(array('\Vendor\Project\Foo', 'bar'))
  13. ->isCallable()
  14.  
  15. ->variable('\Vendor\Project\Foo::bar')
  16. ->isCallable()
  17. ;

2.1.1.2. isEqualTo

isEqualTo checks that the variable is equal to an expected value

  1. $a = 'a';
  2.  
  3. $this
  4. ->variable($a)
  5. ->isEqualTo('a') // passes
  6. ;

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

  1. $a = '1';
  2.  
  3. $this
  4. ->variable($a)
  5. ->isIdenticalTo(1) // fails
  6. ;
  7.  
  8. $stdClass1 = new \StdClass();
  9. $stdClass2 = new \StdClass();
  10. $stdClass3 = $stdClass1;
  11.  
  12. $this
  13. ->variable($stdClass1)
  14. ->isIdenticalTo(stdClass3) // passes
  15. ->isIdenticalTo(stdClass2) // fails
  16. ;

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.

  1. $f = function() {
  2. // code
  3. };
  4. $int = 1;
  5. $string = 'nonExistingMethod';
  6.  
  7. $this
  8. ->variable($f)
  9. ->isNotCallable() // fails
  10.  
  11. ->variable($int)
  12. ->isNotCallable() // passes
  13.  
  14. ->variable($string)
  15. ->isNotCallable() // passes
  16.  
  17. ->variable(new StdClass)
  18. ->isNotCallable() // passes
  19. ;

2.1.1.5. isNotEqualTo

isNotEqualTo checks that the variable is not the same as the given value

  1. $a = 'a';
  2. $aString = '1';
  3.  
  4. $this
  5. ->variable($a)
  6. ->isNotEqualTo('b') // passes
  7. ->isNotEqualTo('a') // fails
  8.  
  9. ->variable($aString)
  10. ->isNotEqualTo($1) // fails
  11. ;

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.

  1. $a = '1';
  2.  
  3. $this
  4. ->variable($a)
  5. ->isNotIdenticalTo(1) // passes
  6. ;
  7.  
  8. $stdClass1 = new \StdClass();
  9. $stdClass2 = new \StdClass();
  10. $stdClass3 = $stdClass1;
  11.  
  12. $this
  13. ->variable($stdClass1)
  14. ->isNotIdenticalTo(stdClass2) // passes
  15. ->isNotIdenticalTo(stdClass3) // fails
  16. ;

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.

  1. $emptyString = '';
  2. $null = null;
  3.  
  4. $this
  5. ->variable($emptyString)
  6. ->isNull() // fails
  7. // (it is empty but not null)
  8.  
  9. ->variable($null)
  10. ->isNull() // passes
  11. ;

2.1.1.8. isNotNull

isNotNull checks that the variable is not null.

  1. $emptyString = '';
  2. $null = null;
  3.  
  4. $this
  5. ->variable($emptyString)
  6. ->isNotNull() // passe (it is empty but not null)
  7.  
  8. ->variable($null)
  9. ->isNotNull() // fails
  10. ;

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.

  1. $true = true;
  2. $false = false;
  3.  
  4. $this
  5. ->boolean($true)
  6. ->isFalse() // fails
  7.  
  8. ->boolean($false)
  9. ->isFalse() // passes
  10. ;

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.

  1. $true = true;
  2. $false = false;
  3.  
  4. $this
  5. ->boolean($true)
  6. ->isTrue() // passes
  7.  
  8. ->boolean($false)
  9. ->isTrue() // fails
  10. ;

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.

  1. $zero = 0;
  2.  
  3. $this
  4. ->integer($zero)
  5. ->isGreaterThan(-1) // passes
  6. ->isGreaterThan('-1') // fails because "-1"
  7. // is not an integer (string)
  8. ->isGreaterThan(0) // fails
  9. ;

2.1.3.3. isGreaterThanOrEqualTo

isGreaterThanOrEqualTo checks that the integer is greater or equal to the given value.

  1. $zero = 0;
  2.  
  3. $this
  4. ->integer($zero)
  5. ->isGreaterThanOrEqualTo(-1) // passes
  6. ->isGreaterThanOrEqualTo(0) // passes
  7. ->isGreaterThanOrEqualTo('-1') // fails because "-1"
  8. // is not an integer (string)
  9. ;

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.

  1. $zero = 0;
  2.  
  3. $this
  4. ->integer($zero)
  5. ->isLessThan(10) // passes
  6. ->isLessThan('10') // fails because "10" is not an integer (string)
  7. ->isLessThan(0) // fails
  8. ;

2.1.3.6. isLessThanOrEqualTo

isLessThanOrEqualTo checks that the integer is less or equal than the given value.

  1. $zero = 0;
  2.  
  3. $this
  4. ->integer($zero)
  5. ->isLessThanOrEqualTo(10) // passes
  6. ->isLessThanOrEqualTo(0) // passes
  7. ->isLessThanOrEqualTo('10') // fails because "10"
  8. // is not an integer
  9. ;

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.

  1. $zero = 0;
  2. $notZero = -1;
  3.  
  4. $this
  5. ->integer($zero)
  6. ->isZero() // passes
  7.  
  8. ->integer($notZero)
  9. ->isZero() // fails
  10. ;

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:

  1. $ php -r 'var_dump(1 - 0.97 === 0.03);'
  2. 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.

  1. $float = 1 - 0.97;
  2.  
  3. $this
  4. ->float($float)
  5. ->isNearlyEqualTo(0.03) // passes
  6. ->isEqualTo(0.03) // fails
  7. ;

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.

  1. $array = array(1, 2, 3);
  2. $countableObject = new GlobIterator('*');
  3.  
  4. $this
  5. ->sizeOf($array)
  6. ->isEqualTo(3)
  7.  
  8. ->sizeOf($countableObject)
  9. ->isGreaterThan(0)
  10. ;

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.

  1. $countableObject = new GlobIterator('*');
  2.  
  3. $this
  4. ->object($countableObject)
  5. ->hasSize(3)
  6. ;

2.1.6.2. isCallable

  1. class foo
  2. {
  3. public function __invoke()
  4. {
  5. // code
  6. }
  7. }
  8.  
  9. $this
  10. ->object(new foo)
  11. ->isCallable() // passes
  12.  
  13. ->object(new StdClass)
  14. ->isCallable() // fails
  15. ;

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.

  1. $object1 = new \StdClass;
  2. $object2 = new \StdClass;
  3. $object3 = clone($object1);
  4. $object4 = new \StdClass;
  5. $object4->foo = 'bar';
  6.  
  7. $this
  8. ->object($object1)
  9. ->isCloneOf($object2) // passes
  10. ->isCloneOf($object3) // passes
  11. ->isCloneOf($object4) // fails
  12. ;

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.

  1. $countableObject = new GlobIterator('atoum.php');
  2.  
  3. $this
  4. ->object($countableObject)
  5. ->isEmpty()
  6. ;

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.
  1. $object = new \StdClass();
  2.  
  3. $this
  4. ->object($object)
  5. ->isInstanceOf('\StdClass') // passes
  6. ->isInstanceOf('\Iterator') // fails
  7. ;
  8.  
  9.  
  10. interface FooInterface
  11. {
  12. public function foo();
  13. }
  14.  
  15. class FooClass implements FooInterface
  16. {
  17. public function foo()
  18. {
  19. echo "foo";
  20. }
  21. }
  22.  
  23. class BarClass extends FooClass
  24. {
  25. }
  26.  
  27. $foo = new FooClass;
  28. $bar = new BarClass;
  29.  
  30. $this
  31. ->object($foo)
  32. ->isInstanceOf('\FooClass') // passes
  33. ->isInstanceOf('\FooInterface') // passes
  34. ->isInstanceOf('\BarClass') // fails
  35. ->isInstanceOf('\StdClass') // fails
  36.  
  37. ->object($bar)
  38. ->isInstanceOf('\FooClass') // passes
  39. ->isInstanceOf('\FooInterface') // passes
  40. ->isInstanceOf('\BarClass') // passes
  41. ->isInstanceOf('\StdClass') // fails
  42. ;

Classes and interfaces names have to be absolute, because namespace import are not taken into account.

2.1.6.8. isNotCallable

  1. class foo
  2. {
  3. public function __invoke()
  4. {
  5. // code
  6. }
  7. }
  8.  
  9. $this
  10. ->variable(new foo)
  11. ->isNotCallable() // fails
  12.  
  13. ->variable(new StdClass)
  14. ->isNotCallable() // passes
  15. ;

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.

  1. $di = new DateInterval('P1D');
  2.  
  3. $this
  4. ->dateInterval($di)
  5. ->isEqualTo( // passes
  6. new DateInterval('P1D')
  7. )
  8. ->isEqualTo( // fails
  9. new DateInterval('P2D')
  10. )
  11. ;

2.1.7.3. isGreaterThan

isGreaterThan checks that the duration of the DateInterval object is greater than the duration of the given DateInterval object.

  1. $di = new DateInterval('P2D');
  2.  
  3. $this
  4. ->dateInterval($di)
  5. ->isGreaterThan( // passes
  6. new DateInterval('P1D')
  7. )
  8. ->isGreaterThan( // fails
  9. new DateInterval('P2D')
  10. )
  11. ;

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.

  1. $di = new DateInterval('P2D');
  2.  
  3. $this
  4. ->dateInterval($di)
  5. ->isGreaterThanOrEqualTo( // passes
  6. new DateInterval('P1D')
  7. )
  8. ->isGreaterThanOrEqualTo( // passes
  9. new DateInterval('P2D')
  10. )
  11. ->isGreaterThanOrEqualTo( // fails
  12. new DateInterval('P3D')
  13. )
  14. ;

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.

  1. $di = new DateInterval('P1D');
  2.  
  3. $this
  4. ->dateInterval($di)
  5. ->isLessThan( // passes
  6. new DateInterval('P2D')
  7. )
  8. ->isLessThan( // fails
  9. new DateInterval('P1D')
  10. )
  11. ;

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.

  1. $di = new DateInterval('P2D');
  2.  
  3. $this
  4. ->dateInterval($di)
  5. ->isLessThanOrEqualTo( // passes
  6. new DateInterval('P3D')
  7. )
  8. ->isLessThanOrEqualTo( // passes
  9. new DateInterval('P2D')
  10. )
  11. ->isLessThanOrEqualTo( // fails
  12. new DateInterval('P1D')
  13. )
  14. ;

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.

  1. $di1 = new DateInterval('P0D');
  2. $di2 = new DateInterval('P1D');
  3.  
  4. $this
  5. ->dateInterval($di1)
  6. ->isZero() // passes
  7. ->dateInterval($di2)
  8. ->isZero() // fails
  9. ;

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.

  1. $dt = new DateTime('1981-02-13');
  2.  
  3. $this
  4. ->dateTime($dt)
  5. ->hasDate('1981', '02', '13') // passes
  6. ->hasDate('1981', '2', '13') // passes
  7. ->hasDate(1981, 2, 13) // passes
  8. ;

2.1.8.2. hasDateAndTime

hasDateAndTime check the date and time of the DateTime object.

  1. $dt = new DateTime('1981-02-13 01:02:03');
  2.  
  3. $this
  4. ->dateTime($dt)
  5. // passes
  6. ->hasDateAndTime('1981', '02', '13', '01', '02', '03')
  7. // passes
  8. ->hasDateAndTime('1981', '2', '13', '1', '2', '3')
  9. // passes
  10. ->hasDateAndTime(1981, 2, 13, 1, 2, 3)
  11. ;

2.1.8.3. hasDay

hasDay checks the day of the DateTime object.

  1. $dt = new DateTime('1981-02-13');
  2.  
  3. $this
  4. ->dateTime($dt)
  5. ->hasDay(13) // passes
  6. ;

2.1.8.4. hasHours

hasHours checks the hours of the DateTime object.

  1. $dt = new DateTime('01:02:03');
  2.  
  3. $this
  4. ->dateTime($dt)
  5. ->hasHours('01') // passes
  6. ->hasHours('1') // passes
  7. ->hasHours(1) // passes
  8. ;

2.1.8.5. hasMinutes

hasMinutes checks the minutes of the DateTime object.

  1. $dt = new DateTime('01:02:03');
  2.  
  3. $this
  4. ->dateTime($dt)
  5. ->hasMinutes('02') // passes
  6. ->hasMinutes('2') // passes
  7. ->hasMinutes(2) // passes
  8. ;

2.1.8.6. hasMonth

hasMonth checks the month of the DateTime object.

  1. $dt = new DateTime('1981-02-13');
  2.  
  3. $this
  4. ->dateTime($dt)
  5. ->hasMonth(2) // passes
  6. ;

2.1.8.7. hasSeconds

hasSeconds checks the seconds of the DateTime object.

  1. $dt = new DateTime('01:02:03');
  2.  
  3. $this
  4. ->dateTime($dt)
  5. ->hasSeconds('03') // passes
  6. ->hasSeconds('3') // passes
  7. ->hasSeconds(3) // passes
  8. ;

2.1.8.8. hasTime

hasTime checks the time part of the DateTime object.

  1. $dt = new DateTime('01:02:03');
  2.  
  3. $this
  4. ->dateTime($dt)
  5. ->hasTime('01', '02', '03') // passes
  6. ->hasTime('1', '2', '3') // passes
  7. ->hasTime(1, 2, 3) // passes
  8. ;

2.1.8.9. hasTimezone

hasTimezone checks the timezone of the DateTime object.

  1. $dt = new DateTime();
  2.  
  3. $this
  4. ->dateTime($dt)
  5. ->hasTimezone('Europe/Paris')
  6. ;

2.1.8.10. hasYear

hasYear checks the year of the DateTime object.

  1. $dt = new DateTime('1981-02-13');
  2.  
  3. $this
  4. ->dateTime($dt)
  5. ->hasYear(1981) // passes
  6. ;

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.

  1. $this
  2. ->exception(
  3. function() use($myObject) {
  4. // this throws an exception: throw new \Exception;
  5. $myObject->doOneThing('wrongParameter');
  6. }
  7. )
  8. ;

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

  1. $this
  2. ->exception(
  3. function() use($myObject) {
  4. // this throws an exception: throw new \Exception('Message', 42);
  5. $myObject->doOneThing('wrongParameter');
  6. }
  7. )
  8. ->hasCode(42)
  9. ;

2.1.10.2. hasDefaultCode

hasDefaultCode checks that the exception code is the default value, 0.

  1. $this
  2. ->exception(
  3. function() use($myObject) {
  4. // this throws an exception: throw new \Exception;
  5. $myObject->doOneThing('wrongParameter');
  6. }
  7. )
  8. ->hasDefaultCode()
  9. ;

hasDefaultCode is equivalent to hasCode(0).

2.1.10.3. hasMessage

hasMessage checks the exception message

  1. $this
  2. ->exception(
  3. function() use($myObject) {
  4. // this throws an exception: throw new \Exception('Message');
  5. $myObject->doOneThing('wrongParameter');
  6. }
  7. )
  8. ->hasMessage('Message') // passes
  9. ->hasMessage('message') // fails
  10. ;

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.

  1. $this
  2. ->exception(
  3. function() use($myObject) {
  4. // this throws an exception: throw new \Exception('Message');
  5. $myObject->doOneThing('wrongParameter');
  6. }
  7. )
  8. ->hasNestedException() // fails
  9.  
  10. ->exception(
  11. function() use($myObject) {
  12. try {
  13. // this throws an exception: throw new \FirstException('Message 1', 42);
  14. $myObject->doOneThing('wrongParameter');
  15. }
  16. // ... the exception is catched
  17. catch(\FirstException $e) {
  18. // ... then thrown again, wrapped in a second exception
  19. throw new \SecondException('Message 2', 24, $e);
  20. }
  21. }
  22. )
  23. ->isInstanceOf('\FirstException') // fails
  24. ->isInstanceOf('\SecondException') // passes
  25.  
  26. ->hasNestedException() // passes
  27. ->hasNestedException(new \FirstException) // passes
  28. ->hasNestedException(new \SecondException) // fails
  29. ;

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

  1. $this
  2. ->exception(
  3. function() {
  4. throw new \Exception('My custom message to test');
  5. }
  6. )
  7. ->message
  8. ->contains('message')
  9. ;

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.

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2.  
  3. $this
  4. ->array($fibonacci)
  5. ->contains('1') // passes
  6. ->contains(1) // passes, because it does not ...
  7. ->contains('2') // ... check the type
  8. ->contains(10) // fails
  9. ;

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.

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2.  
  3. $this
  4. ->array($array)
  5. ->containsValues(array(1, 2, 3)) // passes
  6. ->containsValues(array('5', '8', '13')) // passes
  7. ->containsValues(array(0, 1, 2)) // fails
  8. ;

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.

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2. $atoum = array(
  3. 'name' => 'atoum',
  4. 'owner' => 'mageekguy',
  5. );
  6.  
  7. $this
  8. ->array($fibonacci)
  9. ->hasKey(0) // passes
  10. ->hasKey(1) // passes
  11. ->hasKey('1') // passes
  12. ->hasKey(10) // fails
  13.  
  14. ->array($atoum)
  15. ->hasKey('name') // passes
  16. ->hasKey('price') // fails
  17. ;

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.

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2. $atoum = array(
  3. 'name' => 'atoum',
  4. 'owner' => 'mageekguy',
  5. );
  6.  
  7. $this
  8. ->array($fibonacci)
  9. ->hasKeys(array(0, 2, 4)) // passes
  10. ->hasKeys(array('0', 2)) // passes
  11. ->hasKeys(array('4', 0, 3)) // passes
  12. ->hasKeys(array(0, 3, 10)) // fails
  13.  
  14. ->array($atoum)
  15. ->hasKeys(array('name', 'owner')) // passes
  16. ->hasKeys(array('name', 'price')) // fails
  17. ;

hasKeys does not search recursively.

hasKeys does not check the type.

2.1.11.5. hasSize

hasSize checks the array size.

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2.  
  3. $this
  4. ->array($fibonacci)
  5. ->hasSize(7) // passes
  6. ->hasSize(10) // fails
  7. ;

hasSize is not recursive.

2.1.11.6. isEmpty

isEmpty checks that the array is empty.

  1. $emptyArray = array();
  2. $nonEmptyArray = array(null, null);
  3.  
  4. $this
  5. ->array($emptyArray)
  6. ->isEmpty() // passes
  7.  
  8. ->array($nonEmptyArray)
  9. ->isEmpty() // fails
  10. ;

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.

  1. $emptyArray = array();
  2. $nonEmptyArray = array(null, null);
  3.  
  4. $this
  5. ->array($emptyArray)
  6. ->isNotEmpty() // fails
  7.  
  8. ->array($nonEmptyArray)
  9. ->isNotEmpty() // passes
  10. ;

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.

  1. $atoum = array(
  2. 'name' => 'atoum',
  3. 'owner' => 'mageekguy',
  4. );
  5.  
  6. $this
  7. ->array($atoum)
  8. ->keys
  9. ->isEqualTo(
  10. 'name',
  11. 'owner',
  12. )
  13. )
  14. ;

2.1.11.13. notContains

notContains checks that an array does not contains the given value.

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2.  
  3. $this
  4. ->array($fibonacci)
  5. ->notContains(null) // passes
  6. ->notContains(1) // fails
  7. ->notContains(10) // passes
  8. ;

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.

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2.  
  3. $this
  4. ->array($array)
  5. ->notContainsValues(array(1, 4, 10)) // fails
  6. ->notContainsValues(array(4, 10, 34)) // passes
  7. ->notContainsValues(array(1, '2', 3)) // fails
  8. ;

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.

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2. $atoum = array(
  3. 'name' => 'atoum',
  4. 'owner' => 'mageekguy',
  5. );
  6.  
  7. $this
  8. ->array($fibonacci)
  9. ->notHasKey(0) // fails
  10. ->notHasKey(1) // fails
  11. ->notHasKey('1') // fails
  12. ->notHasKey(10) // passes
  13.  
  14. ->array($atoum)
  15. ->notHasKey('name') // fails
  16. ->notHasKey('price') // passes
  17. ;

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.

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2. $atoum = array(
  3. 'name' => 'atoum',
  4. 'owner' => 'mageekguy',
  5. );
  6.  
  7. $this
  8. ->array($fibonacci)
  9. ->notHasKeys(array(0, 2, 4)) // fails
  10. ->notHasKeys(array('0', 2)) // fails
  11. ->notHasKeys(array('4', 0, 3)) // fails
  12. ->notHasKeys(array(10, 11, 12)) // passes
  13.  
  14. ->array($atoum)
  15. ->notHasKeys(array('name', 'owner')) // fails
  16. ->notHasKeys(array('foo', 'price')) // passes
  17. ;

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.

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2.  
  3. $this
  4. ->array($fibonacci)
  5. ->size
  6. ->isGreaterThan(5)
  7. ;

2.1.11.18. strictlyContains

strictlyContains checks that an array strictly contains the given value (same value and type).

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2.  
  3. $this
  4. ->array($fibonacci)
  5. ->strictlyContains('1') // passes
  6. ->strictlyContains(1) // fails
  7. ->strictlyContains('2') // fails
  8. ->strictlyContains(2) // passes
  9. ->strictlyContains(10) // fails
  10. ;

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).

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2.  
  3. $this
  4. ->array($array)
  5. ->strictlyContainsValues(array('1', 2, '3')) // passes
  6. ->strictlyContainsValues(array(1, 2, 3)) // fails
  7. ->strictlyContainsValues(array(5, '8', 13)) // passes
  8. ->strictlyContainsValues(array('5', '8', '13')) // fails
  9. ->strictlyContainsValues(array(0, '1', 2)) // fails
  10. ;

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).

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2.  
  3. $this
  4. ->array($fibonacci)
  5. ->strictlyNotContains(null) // passes
  6. ->strictlyNotContains('1') // fails
  7. ->strictlyNotContains(1) // passes
  8. ->strictlyNotContains(10) // passes
  9. ;

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).

  1. $fibonacci = array('1', 2, '3', 5, '8', 13, '21');
  2.  
  3. $this
  4. ->array($array)
  5. ->strictlyNotContainsValues(array('1', 4, 10)) // fails
  6. ->strictlyNotContainsValues(array(1, 4, 10)) // passes
  7. ->strictlyNotContainsValues(array(4, 10, 34)) // passes
  8. ->strictlyNotContainsValues(array('1', 2, '3')) // fails
  9. ->strictlyNotContainsValues(array(1, '2', 3)) // passes
  10. ;

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.

  1. $string = 'Hello world';
  2.  
  3. $this
  4. ->string($string)
  5. ->contains('ll') // passes
  6. ->contains(' ') // passes
  7. ->contains('php') // fails
  8. ;

2.1.12.2. hasLength

hasLength checks the string length.

  1. $string = 'Hello world';
  2.  
  3. $this
  4. ->string($string)
  5. ->hasLength(11) // passes
  6. ->hasLength(20) // fails
  7. ;

2.1.12.3. hasLengthGreaterThan

hasLengthGreaterThan checks that the string length is greater than the given value.

  1. $string = 'Hello world';
  2.  
  3. $this
  4. ->string($string)
  5. ->hasLengthGreaterThan(10) // passes
  6. ->hasLengthGreaterThan(20) // fails
  7. ;

2.1.12.4. hasLengthLessThan

hasLengthLessThan checks that the string length is less than the given value.

  1. $string = 'Hello world';
  2.  
  3. $this
  4. ->string($string)
  5. ->hasLengthLessThan(20) // passes
  6. ->hasLengthLessThan(10) // fails
  7. ;

2.1.12.5. isEmpty

isEmpty checks that the string is empty.

  1. $emptyString = '';
  2. $nonEmptyString = 'atoum';
  3.  
  4. $this
  5. ->string($emptyString)
  6. ->isEmpty() // passes
  7.  
  8. ->string($nonEmptyString)
  9. ->isEmpty() // fails
  10. ;

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.

  1. $this
  2. ->string($string)
  3. ->isEqualToContentsOfFile('/path/to/file')
  4. ;

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.

  1. $emptyString = '';
  2. $nonEmptyString = 'atoum';
  3.  
  4. $this
  5. ->string($emptyString)
  6. ->isNotEmpty() // fails
  7.  
  8. ->string($nonEmptyString)
  9. ->isNotEmpty() // passes
  10. ;

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.

  1. $string = 'atoum'
  2.  
  3. $this
  4. ->string($string)
  5. ->length
  6. ->isGreaterThanOrEqualTo(5)
  7. ;

2.1.12.13. match

match checks that the string matches a regular expression.

  1. $phone = '0102030405';
  2. $vdm = "Aujourd'hui, à 57 ans, mon père s'est fait tatouer une licorne sur l'épaule. VDM";
  3.  
  4. $this
  5. ->string($phone)
  6. ->match('#^0[1-9]\d{8}$#')
  7.  
  8. ->string($vdm)
  9. ->match("#^Aujourd'hui.*VDM$#")
  10. ;

2.1.12.14. notContains

notContains checks that the string does not contain the given string.

  1. $string = 'Hello world';
  2.  
  3. $this
  4. ->string($string)
  5. ->notContains('php') // passes
  6. ->notContains(';') // passes
  7. ->notContains('ll') // fails
  8. ->notContains(' ') // fails
  9. ;

2.1.13. castToString

This is the asserter for casting objects to sting.

  1. class AtoumVersion {
  2. private $version = '1.0';
  3.  
  4. public function __toString() {
  5. return 'atoum v' . $this->version;
  6. }
  7. }
  8.  
  9. $this
  10. ->castToString(new AtoumVersion())
  11. ->isEqualTo('atoum v1.0')
  12. ;

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.

  1. $hash = hash('md5', 'atoum');
  2. $notHash = 'atoum';
  3.  
  4. $this
  5. ->hash($hash)
  6. ->isMd5() // passes
  7. ->hash($notHash)
  8. ->isMd5() // fails
  9. ;

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.

  1. $hash = hash('sha1', 'atoum');
  2. $notHash = 'atoum';
  3.  
  4. $this
  5. ->hash($hash)
  6. ->isSha1() // passes
  7. ->hash($notHash)
  8. ->isSha1() // fails
  9. ;

2.1.14.9. isSha256

isSha256 checks that the string is a sha256, an hexadecimal string of 64 characters.

  1. $hash = hash('sha256', 'atoum');
  2. $notHash = 'atoum';
  3.  
  4. $this
  5. ->hash($hash)
  6. ->isSha256() // passes
  7. ->hash($notHash)
  8. ->isSha256() // fails
  9. ;

2.1.14.10. isSha512

isSha512 checks that the string is a sha512, an hexadecimal string of 128 characeters.

  1. $hash = hash('sha512', 'atoum');
  2. $notHash = 'atoum';
  3.  
  4. $this
  5. ->hash($hash)
  6. ->isSha512() // passes
  7. ->hash($notHash)
  8. ->isSha512() // fails
  9. ;

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.

  1. $this
  2. ->output(
  3. function() {
  4. echo 'Hello world';
  5. }
  6. )
  7. ;

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.

  1. $vdm = "Aujourd'hui, à 57 ans, mon père s'est fait tatouer une licorne sur l'épaule. VDM";
  2.  
  3. $this
  4. ->utf8String($vdm)
  5. ->match("#^Aujourd'hui.*VDM$#u")
  6. ;

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.

  1. $this
  2. ->afterDestructionOf($objectWithDestructor) // passes
  3. ->afterDestructionOf($objectWithoutDestructor) // fails
  4. ;

2.1.18. error

This is the asserter for errors.

  1. $this
  2. ->when(
  3. function() {
  4. trigger_error('message');
  5. }
  6. )
  7. ->error()
  8. ->exists() // or notExists
  9. ;

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.

  1. $this
  2. ->when(
  3. function() {
  4. trigger_error('message');
  5. }
  6. )
  7. ->error()
  8. ->exists() // passes
  9.  
  10. ->when(
  11. function() {
  12. // code sans erreur
  13. }
  14. )
  15. ->error()
  16. ->exists() // fails
  17. ;

2.1.18.2. notExists

notExists checks that no error has been raised when calling the anonymous function.

  1. $this
  2. ->when(
  3. function() {
  4. trigger_error('message');
  5. }
  6. )
  7. ->error()
  8. ->notExists() // fails
  9.  
  10. ->when(
  11. function() {
  12. // no error there
  13. }
  14. )
  15. ->error()
  16. ->notExists() // passes
  17. ;

2.1.18.3. withType

withType checks the raised error type.

  1. $this
  2. ->when(
  3. function() {
  4. trigger_error('message');
  5. }
  6. )
  7. ->error()
  8. ->withType(E_USER_NOTICE) // passes
  9. ->withType(E_USER_WARNING) // fails
  10. ;

2.1.19. class

This is the asserter for classes.

  1. $object = new \StdClass;
  2.  
  3. $this
  4. ->class(get_class($object))
  5.  
  6. ->class('\StdClass')
  7. ;

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.

  1. $this
  2. ->class('\ArrayIterator')
  3. ->hasInterface('Countable') // passes
  4.  
  5. ->class('\StdClass')
  6. ->hasInterface('Countable') // fails
  7. ;

2.1.19.2. hasMethod

hasMethod checks that the class contains the given method.

  1. $this
  2. ->class('\ArrayIterator')
  3. ->hasMethod('count') // passes
  4.  
  5. ->class('\StdClass')
  6. ->hasMethod('count') // fails
  7. ;

2.1.19.3. hasNoParent

hasNoParent checks that the class does not inherit from any class.

  1. $this
  2. ->class('\StdClass')
  3. ->hasNoParent() // passes
  4.  
  5. ->class('\FilesystemIterator')
  6. ->hasNoParent() // fails
  7. ;

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.

  1. $this
  2. ->class('\StdClass')
  3. ->hasParent() // fails
  4.  
  5. ->class('\FilesystemIterator')
  6. ->hasParent() // passes
  7. ;

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.

  1. $this
  2. ->class('\StdClass')
  3. ->isAbstract() // fails
  4. ;

2.1.19.6. isSubclassOf

isSubclassOf checks that the class inherits from the given class.

  1. $this
  2. ->class('\FilesystemIterator')
  3. ->isSubclassOf('\DirectoryIterator') // passes
  4. ->isSubclassOf('\SplFileInfo') // passes
  5. ->isSubclassOf('\StdClass') // fails
  6. ;

2.1.20. mock

This is the asserter for mocks.

  1. $mock = new \mock\MyClass;
  2.  
  3. $this
  4. ->mock($mock)
  5. ;

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

  1. $mock = new \mock\MyFirstClass;
  2.  
  3. $this
  4. ->object(new MySecondClass($mock))
  5.  
  6. ->mock($mock)
  7. ->call('myMethod')
  8. ->once()
  9. ;
2.1.20.1.1. atLeastOnce

atLeastOnce check that the tested method (see call) has been called at least once.

  1. $mock = new \mock\MyFirstClass;
  2.  
  3. $this
  4. ->object(new MySecondClass($mock))
  5.  
  6. ->mock($mock)
  7. ->call('myMethod')
  8. ->atLeastOnce()
  9. ;
2.1.20.1.2. exactly

exactly check that the tested method (see call) has been called a specific number of times.

  1. $mock = new \mock\MyFirstClass;
  2.  
  3. $this
  4. ->object(new MySecondClass($mock))
  5.  
  6. ->mock($mock)
  7. ->call('myMethod')
  8. ->exactly(2)
  9. ;
2.1.20.1.3. never

never check that the tested method (see call) has never been called.

  1. $mock = new \mock\MyFirstClass;
  2.  
  3. $this
  4. ->object(new MySecondClass($mock))
  5.  
  6. ->mock($mock)
  7. ->call('myMethod')
  8. ->never()
  9. ;

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
  1. $mock = new \mock\MyFirstClass;
  2.  
  3. $this
  4. ->object(new MySecondClass($mock))
  5.  
  6. ->mock($mock)
  7. ->call('myMethod')
  8. ->once()
  9. ->call('mySecondMethod')
  10. ->twice()
  11. ->call('myThirdMethod')
  12. ->thrice()
  13. ;

once, twice et thrice are respectively equivalent to exactly(1), exactly(2) et exactly(3).

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:

  1. $mock = new \mock\MyFirstClass;
  2.  
  3. $this
  4. ->object(new MySecondClass($mock))
  5.  
  6. ->mock($mock)
  7. ->call('myMethod')
  8. ->withArguments('first') ->once()
  9. ->withArguments('second') ->once()
  10. ->withAnyArguments()->exactly(2)
  11. ;
2.1.20.1.6. withArguments

withArguments let you specify the expected arguments that tested method should receive when called (see call).

  1. $mock = new \mock\MyFirstClass;
  2.  
  3. $this
  4. ->object(new MySecondClass($mock))
  5.  
  6. ->mock($mock)
  7. ->call('myMethod')
  8. ->withArguments('first', 'second')->once()
  9. ;

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).

  1. $mock = new \mock\MyFirstClass;
  2.  
  3. $this
  4. ->object(new MySecondClass($mock))
  5.  
  6. ->mock($mock)
  7. ->call('myMethod')
  8. ->withIdenticalArguments('first', 'second')->once()
  9. ;

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.

  1. $mock = new \mock\MyFirstClass;
  2.  
  3. $this
  4. ->object(new MySecondClass($mock))
  5.  
  6. ->mock($mock)
  7. ->wasCalled()
  8. ;

2.1.20.3. wasNotCalled

wasNotCalled checks that no method of the mock has been called.

  1. $mock = new \mock\MyFirstClass;
  2.  
  3. $this
  4. ->object(new MySecondClass($mock))
  5.  
  6. ->mock($mock)
  7. ->wasNotCalled()
  8. ;

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:

  1. $this
  2. ->if($computer = new computer()))
  3. ->and($computer->setFirstOperand(2))
  4. ->and($computer->setSecondOperand(2))
  5. ->then
  6. ->object($computer->add())
  7. ->isIdenticalTo($computer)
  8. ->integer($computer->getResult())
  9. ->isEqualTo(4)
  10. ;

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:

  1. $this
  2. ->and($computer = new computer()))
  3. ->and($computer->setFirstOperand(2))
  4. ->then
  5. ->if($computer->setSecondOperand(2))
  6. ->object($computer->add())
  7. ->isIdenticalTo($computer)
  8. ->integer($computer->getResult())
  9. ->isEqualTo(4)
  10. ;

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:

  1. $computer = new computer();
  2. $computer->setFirstOperand(2);
  3. $computer->setSecondOperand(2);
  4.  
  5. $this
  6. ->object($computer->add())
  7. ->isIdenticalTo($computer)
  8. ->integer($computer->getResult())
  9. ->isEqualTo(4)
  10. ;

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:

  1. $this
  2. ->if($object = new object($valueAtKey0 = uniqid()))
  3. ->and(unset($object[0]))
  4. ->then
  5. ->sizeOf($object)
  6. ->isZero()
  7. ;

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:

  1. $this
  2. ->if($object = new object($valueAtKey0 = uniqid()))
  3. ->when(
  4. function() use ($object) {
  5. unset($object[0]);
  6. }
  7. )
  8. ->then
  9. ->sizeOf($object)
  10. ->isZero()
  11. ;

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:

  1. $this
  2. ->if($foo = new \mock\foo())
  3. ->and($bar = new bar($foo))
  4. ->and($bar->doSomething())
  5. ->then
  6. ->mock($foo)
  7. ->call('doOtherThing')
  8. ->once()
  9.  
  10. ->if($bar->setValue(uniqid())
  11. ->then
  12. ->mock($foo)
  13. ->call('doOtherThing')
  14. ->exactly(2)
  15. ;

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).
  1. $this
  2. ->if($foo = new \mock\foo())
  3. ->and($bar = new bar($foo))
  4. ->and($bar->doSomething())
  5. ->then
  6. ->mock($foo)
  7. ->call('doOtherThing')
  8. ->once()
  9.  
  10. // 1st way
  11. ->if($foo->getMockController()->resetCalls())
  12. ->and($bar->setValue(uniqid())
  13. ->then
  14. ->mock($foo)
  15. ->call('doOtherThing')
  16. ->once()
  17.  
  18. // 2nd way
  19. ->if($this->resetMock($foo))
  20. ->and($bar->setValue(uniqid())
  21. ->then
  22. ->mock($foo)
  23. ->call('doOtherThing')
  24. ->once()
  25. ;

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.

  1. $this
  2. ->assert('Foo est vide')
  3. ->if($foo = new \mock\foo())
  4. ->and($bar = new bar($foo))
  5. ->and($bar->doSomething())
  6. ->then
  7. ->mock($foo)
  8. ->call('doOtherThing')
  9. ->once()
  10.  
  11. ->assert('Foo a une valeur')
  12. ->if($bar->setValue(uniqid())
  13. ->then
  14. ->mock($foo)
  15. ->call('doOtherThing')
  16. ->once()
  17. ;

The string will be used by atoum in atoum generated messages if one of the assertions fail.