How To Define Dependable Fixtures Without Using Concrete Values Of Foreign Keys?

I have two tables: user and profile. Each user has only one profile. So each profile related to some user, so profile’s fixture depends on user’s fixture. I need the fixtures for both these tables to test profile related functionality.

In article Yii 2.0 Guide: Fixtures I found this:

Is there any way to define ProfileFixture and link it with UserFixture without using concrete values of primary keys (in UserFixture) and foreign keys (in ProfileFixture)? Something like this:

@app/tests/fixtures/data/user.php:




<?php

return [

    'user1' => [

        'username' => 'johnsmith',

        'email' => 'john.smith@mail.com',

        'password' => '$2y$13$WSyE5hHsG1rWN2jV8LRHzubilrCLI5Ev/iK0r3jRuwQEs2ldRu.a2',

    ],

];



@app/tests/fixtures/data/profile.php:




return [

    'user1profile' => [

        'user_id' => 'user1',

        'full_name' => 'John Smith',

    ],

];



Of course, I might use concrete values of fields ‘id’ and ‘user_id’ in my data files. But is there any other way?

Solved this way:

@app/tests/fixtures/data/user.php:




<?php

return [

    'user1' => [

        'username' => 'johnsmith',

        'email' => 'john.smith@mail.com',

        'password' => '$2y$13$WSyE5hHsG1rWN2jV8LRHzubilrCLI5Ev/iK0r3jRuwQEs2ldRu.a2',

    ],

];



@app/tests/fixtures/data/profile.php:




return [

    'user1profile' => [

        'user_id' => User::findOne(['username'=>'johnsmith'])->id,

        'full_name' => 'John Smith',

    ],

];



Note: User is AR-model for users table, and username is unique identifier of user.

Hi,

What is your set up for fixtures ?

When I run yii fixture User I get a Error: The template path "@tests/unit/templates/fixtures" does not exist

Thx

i use this solution

<?php
namespace tests\unit\fixtures;

use Codeception\Util\Fixtures;
use yii\test\ActiveFixture;

/**
 * Class
 */
class ClientContactFixture extends ActiveFixture
{
    public $modelClass = ClientContact::class;

    public function load(): void
    {
        parent::load();
        Fixtures::add(self::class, $this->data);
    }
}

<?php

namespace tests\unit\fixtures;

use yii\test\ActiveFixture;

/**
 * Class
 */
class ClientContactAgreementFixture extends ActiveFixture
{
    public $modelClass = ClientContactAgreement::class;

    public $depends = [
        ClientContactFixture::class,
    ];
}

//data file
<?php

use Codeception\Util\Fixtures;
use tests\unit\fixtures\ClientContactFixture;

$clientContacts = Fixtures::get(ClientContactFixture::class);

return [
    'ClientContactAgreement-1' => [
        'client_contact_id' => $clientContacts['ClientContact-1']['id'],
        'value' => 1,
    ],
];