Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Yii2 db fixtures from array
<?php
namespace tests\codeception\common\unit;
use Codeception\Specify;
use yii\di\Instance;
use yii\test\InitDbFixture;
/**
* usage:
* $this->loadLocalFixtures([
* PlaceFixture::className() => [
* ['id' => 1, 'filling' => Place::FILL_PARTIAL, 'shipmentId' => 1],
* ],
* TaskFixture::className() => [
* ['placeId' => 1],
* ['placeId' => 2],
* ],
* ]);
*/
class DbTestCase extends \yii\codeception\DbTestCase
{
use Specify;
public $appConfig = '@tests/codeception/config/common/unit.php';
public function loadLocalFixtures($fixtures)
{
foreach ($fixtures as $className => &$fixture) {
$fixturePrepared = [];
$fixturePrepared['data'] = $fixture;
$fixturePrepared['dataFile'] = false;
$fixturePrepared['class'] = $className;
$fixture = $fixturePrepared;
}
unset($fixture);
array_unshift($fixtures, InitDbFixture::className()); // disables db integrity check
parent::loadFixtures($this->createFixtures($fixtures));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment