使用现有迁移表进行laravel单元测试

时间:2015-10-27 18:35:39

标签: php unit-testing laravel laravel-5

我想使用一组现有的迁移来为我创建表,以便将种子数据填充到其中。我正在使用Orchestra Testbench进行测试。 Examples like this显示即时创建表:

(foo) (bar)

但我想使用现有的迁移。

TestCase.php:

DB::schema()->create('oauth_identities', function($table) {...

DBTestCase.php:

类DBTestCase扩展了TestCase {     protected $ artisan;

use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
    protected function getBasePath()
    {
        // reset base path to point to our package's src directory
        return __DIR__ . '/../vendor/orchestra/testbench/fixture';
    }
}

UserTest.php:

/*
 * Bootstrap the application
 */
public function setUp()
{
    parent::setUp();

    $this->artisan = $this->app->make('Illuminate\Contracts\Console\Kernel');

    $this->artisan('migrate', [
            '--database' => 'testbench',
            '--realpath' => realpath(__DIR__.'/../database/migrations'),
        ]
    );

}

protected function getEnvironmentSetUp($app)
{
    parent::getEnvironmentSetUp($app);

    $app['config']->set('database.default', 'testbench');
    $app['config']->set('database.connections.testbench', [
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => ''
        ]
    );
}

错误:

  

在\ App \ Tests \ UserTest :: testCreateUser中   一般错误:1没有这样的表:用户

但是在我的迁移中,我肯定在创建表格:

class UserTest extends DBTestCase { use DatabaseMigrations; private function seedUser() { return [ 'email' => 'myemail', ... ]; ... } ... public function testCreateUser() { $user = User::create($this->seedUser()); die(print_r($user)); ...

那么我如何使用现有的迁移表并为迁移生成种子数据呢?

编辑:现在在DBTestCase Schema::create('users', function (Blueprint $table) {...中设置我的数据库驱动程序后运行phpunit时,它会抛出错误:

  

Class' Doctrine \ DBAL \ Driver \ PDOSqlite \ Driver'在... / Database / SQLiteConnection.php

中找不到

导航到'driver' => 'sqlite',SQLiteConnection.php中的PDOSqlite以红色突出显示,表示它是未定义的命名空间。

我在database.php中将其作为驱动程序提供:

database.php中:

use Doctrine\DBAL\Driver\PDOSqlite\Driver as DoctrineDriver;

2 个答案:

答案 0 :(得分:0)

如果您使用的是Laravel 5,您可以设置测试用例以使用这样的数据迁移:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    use DatabaseMigrations;

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $this->visit('/')
             ->see('Laravel 5');
    }
}

通过使用DatabaseMigration特征,数据库在每个测试用例之前迁移,在之后回滚。如果您尝试在不同的测试用例中测试相同的数据,请记住这一点。

答案 1 :(得分:0)

假设您已经在软件包中正确设置了ServiceProvider,并且在loadMigrationsFrom方法中使用boot指向了迁移文件,那么只需更改测试类,例如这个:

class TestCase extends \Orchestra\Testbench\TestCase
{
    public function setUp(): void
    {
        parent::setUp();

        $this->artisan('migrate', ['--database' => 'testbench'])->run();
    }

    /**
     * add the package provider
     *
     * @param $app
     * @return array
     */
    protected function getPackageProviders($app)
    {
        return [\Vendor\Your-Packagename::class];
    }


    /**
     * Define environment setup.
     *
     * @param  \Illuminate\Foundation\Application  $app
     * @return void
     */
    protected function getEnvironmentSetUp($app)
    {
        // Setup default database to use sqlite :memory:
        $app['config']->set('database.default', 'testbench');
        $app['config']->set('database.connections.testbench', [
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => '',
        ]);
    }