Yii Boilerplate And Unit Test - Errors And Configuration

I haven’t found documents about configuration of Unit Test on Yii Boilerplate.

I tried to create a test in which i create an object. I’ve to require classes within the test class, and, anyway, it gives me some error:




require('/../../common/bootstrap.php');

require('/../../common/config/main.php');

require('/../../common/models/Annuncio.php');

class AnnuncioTest extends PHPUnit_Framework_TestCase {

    public function testNuovoAnnuncio() {

        $annuncio = new Annuncio;

        ....

    }

}



Attached the error…

Someone knows hot to use unit and functional test with Yii Boilerplate? Do they overwrite the original testing system based on CTestCase and CDbTestCase?

No way guys?

“We” did not overwrite the original testing system, as you can clearly see from the codebase of YiiBoilerplate. From your console output it’s clearly seen that you don’t have the “db” component in Yii application, because there’s no Yii application in your environment.

All these require statements in your code are waste. When you instantiate the CActiveRecord instance (which your Annuncio supposedly is), you basically require your tests to have the whole Yii application instantiated beforehand. It means your tests are not "unit" anymore, by the way.

As it’s stated in the README.md, you instantiate the Yii application in your tests manually. There is the prepared bootstrap file for you in the carcass folder. You use it like this:




bin/phpunit --bootstrap carcass/phpunit.bootstrap.php



This will run all of your tests in the environment where YiiBase class definition is loaded and some imports were done. So you can instantiate the Yii application when you need it in some of your tests.

That’s how you do the testing on CActiveRecord instances using the proper technique:




class UserTest extends CTestCase

{

    public function setUp()

    {

        parent::setUp();


        Yii::createWebApplication(ROOT_DIR.'/frontend/config/main.php');

    }


    /**

     * @test

     * @group YiiDependent

     */

    public function CanFindUser()

    {

        $user = User::model()->find(['limit' => '1']);


        $this->assertInstanceOf('User', $user);

        $this->assertEquals('admin', $user->username);

    }

}



You execute this tests as follows:




bin/phpunit --bootstrap carcass/phpunit.bootstrap.php --group YiiDependent



Seriously, why it’s so hard to understand? The important part about the prepared bootstrap file was placed into the README. How to use the bootstrap files in PHPUnit is written in documentation. How Yii application work you should already know. How automated testing is done you should already know. How CActiveRecord works you should already know. How you could write such a nonsense as the test code you presented is a mystery for me.

Please, everyone, understand that YiiBoilerplate is NOT FOR BEGINNERS. Its purpose is to relieve the tech lead from the burden of configuring the new project from scratch. Tech lead knows already how the gears are moving inside Yii, he just don’t want to repeat the same work over and over with each new project!