TSM
TSM

Reputation: 189

Yii2 + Codeception: How to use fixtures?

I wrote a simple test for my Yii2 application using Codeception. Instead of using the real MySQL db, I want to use fixtures.

Here is the code:

tests/PersonTest.php:

namespace app\tests\unit\models;

use tests\fixtures;
use app\controllers;

class PersonTest extends \Codeception\Test\Unit
{
    protected $tester;
    public $appConfig = '@app/config/main.php';

    protected function _before(){ }
    protected function _after(){ }

    public function _fixtures()
    {
        return [ 'Person' => fixtures\PersonFixture::className() ];
    }

    public function testUser(){
        $person = Person::findOne( [ "id" => 1 ] );
        $userId = isset( $person->id ) ? $person->id : false;
        $this->assertEquals( 1, $userId );
    }
}

tests/fixtures/data/Person.php

return [
    'person1' => [
        'id'            => 1,
        'firstname'     => 'Foo',
        'lastname'      => 'Bar',

    ],
];

tests/fixtures/Person.php

namespace tests\fixtures;

use yii\test\ActiveFixture;

class PersonFixture extends ActiveFixture
{
    public $modelClass = 'app\models\Person';
}

When I run the test, I just get the error:

[Error] Class 'tests\fixtures\PersonFixture' not found

I tried 100 different things, but I can not make it work. If this simple example would work for me, I could create real tests.

Upvotes: 6

Views: 7506

Answers (4)

Mariusz Fornal
Mariusz Fornal

Reputation: 1

You have to change fixture file name from Person.php to PersonFixture.php and it will start working.

Upvotes: 0

JAK programmer
JAK programmer

Reputation: 21

with codeception 4.0.3 you can run your fixture by following the steps...
create fixtures folder inside test
[fixture folder][1]

  [1]: https://i.sstatic.net/uK9Cy.png
inside your fixtures/data/book.php

<?php
return [
    'book1' => [
        'title' => 'lmayert',
        'isbn' => 'Ibn-098',
    ],
    'user2' => [
        'title' => 'napoleon69',
        'isbn' => 'Ibn-042',        
    ],
];

your fixtures/BookFixture be like this:

<?php

namespace app\tests\fixtures;

use yii\test\ActiveFixture;

/**
 * 
 */
class BookFixture extends ActiveFixture
{
    public $modelClass = 'app\models\Book';
}

Now the tests/unit/BookTest be like this

<?php 
use app\tests\unit\fixtures\BookFixture;
class BookTest extends \Codeception\Test\Unit
{
    /**
     * @var \UnitTester
     */
    protected $tester;

    protected function _before()
    {
    }

    protected function _after()
    {
    }

    public function _fixtures() {
        return [
            'books' => 'app\tests\fixtures\BookFixture',
        ];
    }
    // tests
    public function testBook()
    {
        $book1 = $this->tester->grabFixture('books','book1');
        $this->assertEquals(1,$book1->id);
    }
}

I hope this will help

Upvotes: 2

mae
mae

Reputation: 15656

With Codeception 2.3.8 you can do it like this:

Define your fixture (and have the data file just like you have in your question)

namespace app\tests\fixtures;

class PersonFixture extends \yii\test\ActiveFixture {

    public $modelClass = 'app\models\Person';

}

And write your test

namespace app\tests\unit;

class PersonTest extends \Codeception\Test\Unit {

    public function _fixtures() {
        return [
            'persons'   => 'app\tests\fixtures\PersonFixture',
        ];
    }

    public function testUser() {
        $person1 = $this->tester->grabFixture('persons', 'person1');
        $this->assertEquals(1, $person1->id);
    }

}

That's it.

Upvotes: 3

xReprisal
xReprisal

Reputation: 820

You have to be using yii2-codeception extension which would autoload fixtures for you.

After installing it you would have class yii\codeception\DbTestCase available, PersonTest should extend it.

Person fixture should have namespace as follows: app\tests\fixtures.

Upvotes: -1

Related Questions