Enhance PHP Unit Testing And Mocking Framework improve the quality

Assertions

There is support for a wide range of assertions in your test, including some assertions that allow you to make up any rule you wish. Here is a run down of all the different assert calls you can make.

You can also make various type-assertions: isArray, isNotArray, isBool, isNotBool, isFloat, isNotFloat, isInt, isNotInt, isNumeric, isNotNumeric, isObject, isNotObject, isScalar, isNotScalar, isString, isNotString.

For integration tests, you can also use isResource, isNotResource

\Enhance\Assert::areIdentical($expected, $actual);

This call verifies that the types and values are identical. For example:

\Enhance\Assert::areIdentical(1, 1); // Passes
\Enhance\Assert::areIdentical(1, '1'); // Fails - different types
\Enhance\Assert::areIdentical(1, 2); // Fails - different values

\Enhance\Assert::areNotIdentical($expected, $actual);

This call verifies that the types and/or values are not identical. For example:

\Enhance\Assert::areNotIdentical(1, 1); // Fails - same type and value
\Enhance\Assert::areNotIdentical(1, '1'); // Passes - different types
\Enhance\Assert::areNotIdentical(1, 2); // Passes - different values

\Enhance\Assert::isTrue($actual);

This call verifies that the type is boolean and that the value is true:

\Enhance\Assert::isTrue(true); // Passes
\Enhance\Assert::isTrue(1); // Fails - wrongtype (although evaluates to true)
\Enhance\Assert::isTrue(false); // Fails - wrong value
\Enhance\Assert::isTrue('true'); // Fails - wrong type

\Enhance\Assert::isFalse($actual);

This call verifies that the type is boolean and that the value is false:

\Enhance\Assert::isFalse(false); // Passes
\Enhance\Assert::isFalse(0); // Fails - wrongtype (although evaluates to false)
\Enhance\Assert::isFalse(true); // Fails - wrong value
\Enhance\Assert::isFalse('false'); // Fails - wrong type

\Enhance\Assert::contains($expected, $actual);

This call verifies that the actual string contains the expected string:

\Enhance\Assert::contains('hello', 'hello world'); // Passes
\Enhance\Assert::contains('hello', 'goodbye all'); // Fails - not in string

\Enhance\Assert::notContains($expected, $actual);

This call verifies that the actual string does not contain the expected string:

\Enhance\Assert::notContains('hello', 'goodbye all'); // Passes
\Enhance\Assert::notContains('hello', 'hello world'); // Fails - match in string

\Enhance\Assert::isNull($actual);

This call verifies that the value is null:

\Enhance\Assert::isNull(null); // Passes
\Enhance\Assert::isNull('Value'); // Fails - not null

\Enhance\Assert::isNotNull($actual);

This call verifies that the value is not null:

\Enhance\Assert::isNotNull('Value'); // Passes
\Enhance\Assert::isNotNull(null); // Fails - null

\Enhance\Assert::isInstanceOfType($expected, $actual);

This call verifies that the object is of the correct type:

\Enhance\Assert::isInstanceOfType('SomeType', $someObject); // Passes
\Enhance\Assert::isInstanceOfType('SomeType', $differentObject); // Fails - not the correct type

\Enhance\Assert::isNotInstanceOfType($expected, $actual);

This call verifies that the object is not the specified type:

\Enhance\Assert::isNotInstanceOfType('SomeType', $differentObject); // Passes
\Enhance\Assert::isNotInstanceOfType('SomeType', $someObject); // Fails - same type

\Enhance\Assert::fail();

This call will fail every time - you could use this after some custom logic to fail a test based on your own custom conditions:

\Enhance\Assert::fail(); // Fails every time!

\Enhance\Assert::inconclusive();

This call will fail every time - it indicates that a test should fail or has not yet been implemented:

\Enhance\Assert::inconclusive(); // Fails every time!

\Enhance\Assert::throws();

This call will fail if an exception is not thrown by the target.

\Enhance\Assert::throws($someClass, 'MethodName');

You can also pass arguments to be used in the target method call:

\Enhance\Assert::throws($someClass, 'MethodName', array(2, 'Arg2', 5));