【问题标题】:phpunit testing with sqlite and laravel eloquent使用 sqlite 和 laravel eloquent 进行 phpunit 测试
【发布时间】: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


    【解决方案1】:

    你的所作所为是不必要的。

    Laravel 为您提供了开箱即用的工具来完成您已经尝试做的事情。

    将此添加到您的setUp()TestCase

    Artisan::call('migrate');
    
    $this->seed();
    

    它将迁移您在 SQLite 内存数据库中的迁移。

    那么你应该有这样的东西:

    <?php
    
    class TestCase extends \Illuminate\Foundation\Testing\TestCase {
    
        public function setUp()
        {
            parent::setUp();
    
            Artisan::call('migrate');
    
            $this->seed();
        }
    
        /**
         * Creates the application.
         *
         * @return \Symfony\Component\HttpKernel\HttpKernelInterface
         */
        public function createApplication()
        {
            $unitTesting = true;
    
            $testEnvironment = 'testing';       
    
            return require __DIR__.'/../../bootstrap/start.php';
        }
    
    }
    

    【讨论】:

    • 谢谢,但这不是 laravel 内部的测试,它是 laravel 的一个包。我使用 sqlite 来测试我的包
    • 谢谢我知道 :) 但我不在 laravel 项目的范围内,这仅用于测试包,与 laravel 无关,它是 laravel 的包并使用它数据库单元,但测试在它的范围之外运行。
    猜你喜欢
    • 2016-06-04
    • 2015-08-25
    • 2017-09-29
    • 2019-03-06
    • 2015-09-01
    • 2013-09-13
    • 2011-10-10
    • 2019-01-27
    • 2015-06-15
    相关资源
    最近更新 更多