Where to load/unload data for series of tests

I have ~50 tests inside one class, all doing SELECT statements on a set of data. I want to load data once for all the tests and then delete after all 50 have run (NOT after each test).

I have tried using setUpBeforeClass/tearDownAfterClass methods (as below) but it seems like the Yii application is not initialised until after this method as I get a "Trying to get property of non-object" on Yii::$app.

I looked at the fixtures documentation but this just seems to be for setup/teardown for each test, not a series of them.

Any help gratefully received.




public static function setUpBeforeClass()

{

	self::$app = Yii::$app;// store as class property so it is still available in tearDown method

	$db = self::$app->db;

	list($dbData, $dbName) = explode(';', $db->dsn);

	$dbName = str_replace('dbname=', '', $dbName);

	$dbFile = __DIR__ . '/data/db.sql.gz';

	$command = "gunzip -cf $dbFile | " . sprintf('mysql -u %s -p%s %s', $db->username, $db->password, $dbName);

	exec($command, $output, $retVar);

}


public static function tearDownAfterClass()

{

	$db = self::$app->db;

	$command = $db->createCommand('SET FOREIGN_KEY_CHECKS=0')->execute();

	$command = $db->createCommand("SHOW FULL TABLES WHERE TABLE_TYPE LIKE '%TABLE'");

	$res = $command->queryAll();

	foreach ($res as $row) {

		$command = $db->createCommand('TRUNCATE TABLE `' . $row['Tables_in_db'] . '`')->execute();

	}

	$command = $db->createCommand('SET FOREIGN_KEY_CHECKS=1')->execute();

}



Not very elegant but this is my working solution at the moment and gets around the aforementioned bootstrap order issue




public static $app;

public static function setUpBeforeClass()

{

	// store as class property so it is still available in tearDown method

	self::$app = new yii\web\Application(require(dirname(dirname(__DIR__)) . '/config/frontend/functional.php'));

	$db = self::$app->db;

	list($dbData, $dbName) = explode(';', $db->dsn);

	$dbName = str_replace('dbname=', '', $dbName);

	$dbFile = __DIR__ . '/fixtures/data/db.sql.gz';

	if (file_exists($dbFile) === false) {

		throw new \Exception('Unable to find dbfile');

	}

	$command = "gunzip -cf $dbFile | " . sprintf('mysql -u %s -p%s %s', $db->username, $db->password, $dbName);

	exec($command, $output, $retVar);

}


public static function tearDownAfterClass()

{

	$db = self::$app->db;

	$command = $db->createCommand('SET FOREIGN_KEY_CHECKS=0')->execute();

	$command = $db->createCommand("SHOW FULL TABLES WHERE TABLE_TYPE LIKE '%TABLE'");

	$res = $command->queryAll();

	foreach ($res as $row) {

		$command = $db->createCommand('TRUNCATE TABLE `' . $row['Tables_in_rais_test'] . '`')->execute();

	}

	$command = $db->createCommand('SET FOREIGN_KEY_CHECKS=1')->execute();

}