Skip to content
AugustusKling edited this page Nov 13, 2011 · 1 revision

By default only a very limited set of assertions is provided. Namely these are:

  • assertTrue (Asserts a condition is TRUE)
  • assertEquals (Type-safe check for equality)
  • assertEqualsLoose (Loose-typed check for equality)
  • fail (Always failing assertion)

These assertions work for many checks however you want to have more assertions available. It is possible to define more sophisticated assertions using the provided assertions in a base class for your tests. Unless you have multiple independent this approach works fine and should be used.

In case several independent test base classes are required, they can share assertions. In order to share assertions you need to create them as subclass of RudimentaryPhpTest_Assertions_Abstract and register it in your base classes.
First create the base class:

class DummyAssertionProvider extends RudimentaryPhpTest_Assertions_Abstract {
    public function assertInRange($start, $end, $value, $message = 'Value not in range'){
        // Check if condition is met
        $inRange = ($start <= $value) && ($value <= $end);
        // Use base assertions for logging
        $this->assertTrue($inRange, $message);
    }
}

Then register the new assertion provider within your test base classes:

class YourBaseTest extends RudimentaryPhpTest_BaseTest {
    public function __construct(){
        // Register assertion provider
        $this->addAssertionProvider(
            new DummyAssertionProvider($this)
        );
    }
}

Any class that extends YourBaseTest is now free to use the assertInRange assertion.

Clone this wiki locally