Velaro
Velaro

Reputation: 501

codeception acceptance test with fixtures in yii2

Sorry for maybe silly question, but I cannot find answer through googling.

My question is: I created file TaskCest.php under backend\acceptance, In that file have following declaration

use yii\test\FixtureTrait;
    public function fixtures() {
      return ['tasks' => TasksFixture::className()];
    }

I have that fixture class with data in data directory.

But when I run script I get following error:

[yii\base\ErrorException] ltrim() expects parameter 1 to be string, object given

Error is obvious, but I cant understand, in file yii2\test\FixtureTrait.php:145 I have function which expects name parameter to be string but object passed automatically [I dont call getFixture]. What's problem. Did someone faced the same?

-vvv output

Test tests/acceptance/TaskCest.php:getFixture

[yii\base\ErrorException] ltrim() expects parameter 1 to be string, object given  
                                                                                    
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Lib/Di.php:123
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Lib/Di.php:123
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Test/Cest.php:136
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Test/Cest.php:148
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Test/Cest.php:82
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Test/Test.php:90
/home/nginx/www/planning-back/vendor/phpunit/phpunit/src/Framework/TestSuite.php:728
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/PHPUnit/Runner.php:98
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/SuiteManager.php:154
/home/velaro/.config/composer/vendor/codeception/codeception/src/Codeception/Codecept.php:183
/home/velaro/.config/composer/vendor/codeception/codeception/src/Codeception/Codecept.php:152
/home/velaro/.config/composer/vendor/codeception/codeception/src/Codeception/Command/Run.php:282
/home/velaro/.config/composer/vendor/symfony/console/Command/Command.php:255
/home/velaro/.config/composer/vendor/symfony/console/Application.php:829
/home/velaro/.config/composer/vendor/symfony/console/Application.php:191
/home/velaro/.config/composer/vendor/symfony/console/Application.php:122
/home/velaro/.config/composer/vendor/codeception/codeception/src/Codeception/Application.php:103
/home/velaro/.config/composer/vendor/codeception/codeception/codecept:34

Upvotes: 2

Views: 995

Answers (1)

Pavel Shorokhov
Pavel Shorokhov

Reputation: 4974

Codeception recognize trait's methods like tests (it search all public methods of Cest-class include trait's methods and run it). You should extract trait to another class FixtureLoader, and include it into your Cest file.

class FixtureLoader
{
    use \yii\test\FixtureTrait;

    public $fixtures;

    public function fixtures()
    {
        return $this->fixtures;
    }
}

abstract class ApiCest
{
    /**
     * @var FixtureLoader
     */
    protected $fixtureLoader;

    public function __construct()
    {
        $this->fixtureLoader = new FixtureLoader();
    }

    protected function fixtures()
    {
        return [];
    }

    public function _before(\FunctionalTester $I)
    {
        $this->fixtureLoader->fixtures = $this->fixtures();
        $this->fixtureLoader->loadFixtures();
    }

    public function _after(\FunctionalTester $I)
    {
        $this->fixtureLoader->unloadFixtures();
    }
}


class UserCest extends ApiCest
{
    protected function fixtures()
    {
        return [
            'users' => UserFixture::className(),
        ];
    }

    public function testSomething(\FunctionalTester $I)
    {
        $I->sendGET('/user');
        $I->seeResponseCodeIs(200);
    }
}

Upvotes: 1

Related Questions