【发布时间】:2015-03-03 10:19:00
【问题描述】:
我有一个使用 DB 的包,我想创建一些测试,这些测试将在内存中使用 sqlite 运行以进行测试。
现在我有了这个基础测试类:
use Illuminate\Database\Capsule\Manager;
class TestCaseDb extends \PHPUnit_Framework_TestCase {
protected $db;
protected $tbmsg;
public function setUp()
{
parent::setUp(); // //DONT CARE
League\FactoryMuffin\Facade::getFaker()->unique($reset = true);//DONT CARE
$this->initDb();
$this->initTbmsg(); //DONT CARE
}
protected function initDb() {
//confi gfor the sqlite
$capsule = new Manager();
$capsule->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
$this->db = $capsule->getDatabaseManager();
//loading simple DB tables creation
$importSql = file_get_contents(__DIR__.'/dumps/dump.sql');
$this->db->statement($importSql);
}
}
现在你可以在这里看到,我创建了 sqlite 数据库并创建了 eloquent DB 对象来处理它。
但是,现在如果我用
$this->db->select("whatever");
效果很好。
但是当我尝试使用 Eloquent 对象时,它会告诉我特定的表不存在。 (它100%存在于第一个Db中)
所以我认为 eloquent 模型试图连接到另一个数据库连接,而不是我创建的那个。
IE - 这是给出错误的测试:
class SimpleTest extends TestCaseDb {
/**
* @test
*/
public function first() {
//the below row works!
//$this->db->insert('insert into conv_users (conv_id, user_id) values (?, ?)', array(1, 2));
//the insert with Eloquent fails....
$data = League\FactoryMuffin\Facade::create('Tzookb\TBMsg\Models\Eloquent\Conversation', []);
$this->assertTrue(true);
}
}
你也可以在我的 github 包中看到代码:(branch dev) https://github.com/tzookb/tbmsg/tree/dev/tests
【问题讨论】:
标签: php sqlite laravel phpunit eloquent