Skip to content

Instantly share code, notes, and snippets.

@kenjis
Created November 10, 2011 10:34
Show Gist options
  • Save kenjis/1354574 to your computer and use it in GitHub Desktop.
Save kenjis/1354574 to your computer and use it in GitHub Desktop.
DbTestCase for FuelPHP
<?php
/**
* DbTestCase
*
* @package Fuel
* @author Kenji Suzuki
* @copyright 2011 Kenji Suzuki
* @link https://github.com/kenjis
*/
class DbTestCase extends TestCase
{
// fixture data
// array of table name and yaml file name
protected $tables = array(
// table_name => yaml_file_name
);
protected function setUp()
{
parent::setUp();
if ( ! empty($this->tables))
{
$this->dbfixt($this->tables);
}
}
protected function dbfixt($tables)
{
// support $this->dbfixt('table1', 'table2', ...) format
$tables = is_string($tables) ? func_get_args() : $tables;
foreach ($tables as $table => $yaml)
{
// read yaml file
$file_name = $yaml . '_fixt.yml';
$file = APPPATH . 'tests/fixture/' . $file_name;
if ( ! file_exists($file))
{
exit('No such file: ' . $file . PHP_EOL);
}
$data = file_get_contents($file);
$table = is_int($table) ? $yaml : $table;
$fixt_name = $table . '_fixt';
$this->$fixt_name = Format::forge($data, 'yaml')->to_array();
// truncate table
if (DBUtil::table_exists($table))
{
DBUtil::truncate_table($table);
}
else
{
exit('No such table: ' . $table . PHP_EOL);
}
// insert data
foreach ($this->$fixt_name as $row)
{
list($insert_id, $rows_affected) = DB::insert($table)->set($row)->execute();
}
$ret = Log::info('Table Fixture ' . $file_name . ' -> ' . $fixt_name . ' loaded', __METHOD__);
}
}
public function test_foo() {}
}
/* End of file app/classes/dbtestcase.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment