Yii2 codeception and fixtures guide


(Sukabo) #1

Hi All,

I manage to setup my codeception environment and able to run command like "codecept run". However, I am stuck as I do not know how to access fixtures data and properly test it. I tried to follow the Yii 2.0 doc guides but there are no examples how to run fixtures test. If you have ideas or there are already similar discussion, please point me to that. Thanks very much.

My test case class:




namespace tests\codeception\unit\models;


use Yii;

use yii\codeception\DbTestCase;

use yii\codeception\TestCase;

use tests\codeception\unit\fixtures\Config2Fixture;

use Codeception\Specify;


class Config2Test extends DbTestCase

{

    use Specify;    

    

    protected function setUp()

    {

        parent::setUp();

    }

    

    public function fixtures()

    {

        return [

            'config' => Config2Fixture::className(),

        ];

    }


    public function testData()

    {

        Yii::log(__METHOD__.' test');

        $this->specify('config id exists', function () {

            expect('id=1 exist', is_array($this->config['config1']))->true();

        });

    }

}



my fixture data file:




return [

    'config1' => [

        'id' => 1,

        'name' => 'field1',

        'value' => 'value1',

    ],

    'config2' => [

        'id' => 2,

        'name' => 'field2',

        'value' => 'field2',

    ],

];



my fixture class:




namespace tests\codeception\unit\fixtures;


use yii\test\ActiveFixture;


class Config2Fixture extends ActiveFixture

{

    public $modelClass = 'app\modules\v1\models\Config2';

}



I try to run "codecept run unit" and get errors:





Unit Tests (1) ---------------------------------------------------------------------------------------------------

Test data | con (tests\codeception\unit\models\Config2Test::testData | config id exists | examples index 0) Fail

Test data (tests\codeception\unit\models\Config2Test::testData)                                             Ok

------------------------------------------------------------------------------------------------------------------




Time: 164 ms, Memory: 15.50Mb


There was 1 failure:


---------

1) tests\codeception\unit\models\Config2Test::testData | config id exists | examples index 0

id=1 exist

Failed asserting that false is true.


#1  /path/tests/codeception/unit/models/Config2Test.php:29

#2  tests\codeception\unit\models\Config2Test->tests\codeception\unit\models\{closure}

#3  /path/tests/codeception/unit/models/Config2Test.php:30

#4  tests\codeception\unit\models\Config2Test->testData


FAILURES!

Tests: 1, Assertions: 1, Failures: 1.

PHP Fatal error:  Call to a member function has() on a non-object in /path/yii2/basic/vendor/yiisoft/yii2/web/ErrorHandler.php on line 70




There are a couple of observation here:

  1. Where do I check the command "Yii::log()" is executed and which log file?

  2. How to access fixture data do run test?

  3. What is the last 'PHP Fatal error" about at the end of line? but it seems not affecting my test script.