How to test models in Yii 2?


(Aleksey Bykov) #1

For example, in the database there is a table tbl_identitydocument, which is not linked with others:


CREATE TABLE [dbo].[tbl_identitydocument](

    [ID] [int] IDENTITY(1,1) NOT NULL,

    [Name] [varchar](100) NOT NULL,

    [FISID] [int] NULL,

 CONSTRAINT [PK_tbl_identitydocument] PRIMARY KEY CLUSTERED 

(

    [ID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

This table corresponds to the model IdentityDocument.php. In the model there is the validation of the fields, for it is written a simple Unit test.

The test fails with the error:

There was 1 error: [yii\base\InvalidParamException] Table not found: []

How to test models in Yii 2? Maybe I’m doing something wrong. And for the test, and for the Web application I use the same database.


(Aleksey Bykov) #2

This is what is written in README.md:

Well, I’ll try. :)


(Aleksey Bykov) #3

As I understand it, by using the migration I need to replicate the structure of the industrial database to the test database and then use fixtures to populate created by using migrations tables? And only after that Unit testing of models will be possible?..


#4

Fixtures are optional but your database and tables should exist.


(Aleksey Bykov) #5

Thank you for you reply! Great, I will try do it now…


(Aleksey Bykov) #6

I copied the structure of the industrial database to the test database, changed the data source name in config.php and in main-local.php. However, the error appears again.

Here is a simple “smoke test” that I’m trying to run:


<?php

namespace tests\codeception\backend;


use tests\codeception\backend\unit\DbTestCase;

use backend\modules\persons\models\IdentityDocument;


class SmokeTest extends DbTestCase

{

    /**

     * @var IdentityDocument

     */

    protected $identityDocument;


    protected function _before()

    {

        $this->identityDocument = new IdentityDocument();

    }


    protected function _after() { }


    // tests

    public function testMe() { }

} 

The model itself:


<?php


namespace backend\modules\persons\models;


use Yii;


/**

 * This is the model class for table "{{%identitydocument}}".

 *

 * @property integer $ID

 * @property string $Name

 * @property integer $FISID

 */

class IdentityDocument extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return '{{%identitydocument}}';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['Name'], 'required'],

            [['Name'], 'string'],

            [['FISID'], 'integer']

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'ID' => Module::t('ML', 'ID'),

            'Name' => Module::t('ML', 'Name'),

            'FISID' => Module::t('ML', 'FISID'),

        ];

    }

}


(Aleksey Bykov) #7

Solved. If in the Unit test fixtures are not used, then it is possible to extend the TestCase class instead of DbTestCase, otherwise the fixture loader will cause errors.