1

I've set up a fixture, but Codeception can't find it. What am I missing or doing wrong?

% php vendor/bin/codecept run functional
...                                                                    
  [Error] Class 'myname\tests\fixtures\MyFixture' not found

In the sample code the namespaces are under myname but I've also tried using just tests\fixtures, tests\unit\fixtures, and myname\tests\unit\fixtures.

Thinking it's something to do with the namespace or most likely something to do with the Craft fixtures docs saying

Fixtures…can be defined in the fixturesMethod defined in the codeception.yml file.

My codeception.yml doesn't have fixturesMethod. I can imagine that's a way of doing some of this but haven't found any documentation describing what that configuration should look like.

I've also tried loading the fixture on the command line

% ./craft fixture/load ImageAssetFixture
Exception 'yii\base\InvalidConfigException' with message 'Invalid fixture namespace: "tests\unit\fixtures". Please, check your FixtureController::namespace parameter'


% ./craft fixture/load ImageAssetFixture --namespace='myname\tests\fixtures'
Exception 'yii\base\InvalidConfigException' with message 'Invalid fixture namespace: "myname\tests\fixtures". Please, check your FixtureController::namespace parameter'

Here's the setup:

tests/functional/AssetFixtureCest.php

<?php

use FunctionalTester;
use myname\tests\fixtures\MyFixture;

class AssetFixtureCest
{
    public function _fixtures()
    {
        return [
            'myFixture' => [
                'class' => MyFixture::className(),
                'dataFile' => codecept_data_dir() . 'my-fixture.php',
            ],
        ];
    }

    public function testMyFixture(FunctionalTester $I)
    {
        $myFixture = $I->grabFixture('myFixture');
        codecept_debug($myFixture);
    }
}

tests/fixtures/MyFixture.php

<?php

namespace myname\tests\fixtures;

use Craft;
use craft\test\fixtures\elements\...;

class MyFixture extends ...
{
    /**
     * {@inheritdoc}
     */
    public $dataFile = __DIR__ . '/data/my-fixture.php';
}

test/fixtures/data/my-fixture.php

<?php

return [
    ...
];
1

I needed to define the namespace. In composer.json:

"autoload": {
    "psr-4": {
        "myname\\tests\\fixtures": "tests/fixtures"
    }
}

and then

composer install

That uncovered some other problems with the above code (to anyone else who's trying to figure out fixtures, looking through the Craft repo's own tests directory is helpful), but that's separate from this question.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.