dsunj08246 2015-01-05 20:02
浏览 99

用sqlite和laravel eloquent测试phpunit

I have a package that uses DB, and I wanted to create some tests that will run with sqlite in memory for the tests.

Now I have this base test class:

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);


    }
}

Now as you can see here I create the sqlite database and create the eloquent DB object for handling it.

But, now if I query it with the

 $this->db->select("whatever");

it works great.

But when I try to use an Eloquent object so it will tell me that the specific table doesn't exist. (It exists 100% in the first Db)

So I think that eloquent model tries to connect to another DB connection and not for the one I created.

IE - this is the test that gives the error:

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);
    }
}

You can see the code in my github package as well: (branch dev) https://github.com/tzookb/tbmsg/tree/dev/tests

  • 写回答

1条回答 默认 最新

  • dora1989 2015-01-05 20:14
    关注

    What you are doing is unnecessary.

    Laravel provides you with tools out of the box to do what you are trying to do already.

    Add this in your setUp() of TestCase.

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

    It will migrate your migrations in your SQLite memory database.

    You should have something like this then:

    <?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';
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码