| Start |
|---|
| Assert an Exception is Thrown PHPUnit provides the following functions (opens new window) to watch for thrown exceptions, which were released with 5.2.0:
These are used to watch for an exception to be thrown and inspect the properties of that exception. Let's start with a math function that divides (just for simplicity). It will raise an exception if the denominator is zero. Now for the test code. The Next, the expectations for the upcoming exception are set. Notice, they are set before the code that will throw the exception. All four assertions are shown for demonstration purposes, but this is normally not necessary. The But note that the code |
| Assert an Object is an Instance of a Class PHPUnit provides the following function to assert whether an object is an instance of a class: The first parameter Let's start with a simple Foo class: Somewhere else in a namespace Now, let's write a basic test case that would check an object for these classes Note that PHPUnit gets grumpy if you send in a classname that doesn't exist. |
| Assert the Value of a Public, Protected and Private property PHPUnit has two assertions to check values of class properties: These methods will check the value of a object property regardless of the visibility. Let's start with a class to be tested. It is a simplified class that has three properties, each with a different visibility: Now, to test the value of each property: As you can see, the assertion works for any visibility, making it easy to peer into protected and private methods. In addition, there is assertAttributeEquals, assertAttributeContains, assertAttributeContainsOnly, assertAttributeEmpty...etc, matching most assertions involving comparison. |
Read Article
https://develop.horje.com/learn/1434/reference