Using fixtures
Unit tests perform their job just fine when you test the class logic. However, when it comes to classes that work with both the environment and data, it becomes a little complicated. What data should we test against? How do we get the same environment each time a test is being executed?
In order to be useful, a unit test should be repeatable. That is why we need to reset the data state on every test run. In PHPUnit, we can do this by using a feature named fixtures. When it comes to database, Yii has a helpful database fixtures addition.
For our example, we will test a coupon system that handles the coupon code registration. The coupon will be stored in a database table named coupon consisting of two columns: id and description. For simplicity, the coupon component simply deletes the already registered coupon record and echoes its description.
Getting ready
Make sure that you have a ready-to-use application and the required testing tools as described in the Setting up the testing...