bevalorous
(Belorussov Vadim)
August 19, 2014, 5:58am
1
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:
A fixture may depend on other fixtures, specified via its yii\test\Fixture::$depends property. When a fixture is being loaded, the fixtures it depends on will be automatically loaded BEFORE the fixture; and when the fixture is being unloaded, the dependent fixtures will be unloaded AFTER the fixture.
…
As we described earlier, a fixture may depend on other fixtures. For example, UserProfileFixture depends on UserFixture because the user profile table contains a foreign key pointing to the user table. The dependency is specified via the yii\test\Fixture::$depends property, like the following,
…
class UserProfileFixture extends ActiveFixture
{
public $modelClass = 'app\models\UserProfile';
public $depends = ['app\tests\fixtures\UserFixture'];
}
…
Also, you do not need to specify the data for auto-incremental columns . Yii will automatically fill the actual values into the rows when the fixture is being loaded.
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?
bevalorous
(Belorussov Vadim)
September 17, 2014, 7:03am
2
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.
ajaxian
(Dorioajax)
November 23, 2014, 6:49am
3
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,
],
];