Laravel 5 在测试之间重新播种数据库以进行单元测试

2024-03-20

我从一个种子数据库开始,并尝试在 Laravel 5 的单元测试之间重新种子数据库。在 Laravel 4 中,我知道您可以简单地使用 Illuminate\Support\Facades\Artisan 并运行命令

Artisan::call('migrate');
Artisan::call('db:seed');

或者你应该可以这样做:

$this->seed('DatabaseSeeder');

每次测试前。在 Laravel 5 中,这似乎已被替换为

use DatabaseMigrations;

or

use DatabaseTransactions;

我尝试过使用这些并成功地进行了迁移数据库的测试;但是,它实际上并没有重新播种表中的数据。我已经阅读了几个抱怨此问题的论坛,并尝试了几种不同的方法,从测试用例和每个测试中调用这些方法...添加

    $this->beforeApplicationDestroyed(function () {
        Artisan::call('migrate');
        Artisan::call('migrate:reset');
        Artisan::call('db:seed');
        DB::disconnect();
    });

到 TestCase.phptearDown()...

我也尝试过添加

$this->createApplication();

到 TestCase.php 中每个测试中调用的方法

有时它会把我的桌子完全抹掉。我在 Laravel 网站或博客中发现的任何内容似乎都不起作用。部分原因可能是因为我可能正在 Laravel 5 中尝试 Laravel 4 方法。在 Laravel 5 中有什么方法可以做到这一点吗?

我的 testcase.php 代码如下所示:

<?php

use Illuminate\Support\Facades\Artisan as Artisan;

class TestCase extends Illuminate\Foundation\Testing\TestCase{

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

    protected $baseUrl = 'http://localhost';


    public function initializeTests(){

        $this->createApplication();

       Artisan::call('migrate');
       $this->artisan('migrate');
       Artisan::call('db:seed');
       $this->artisan('db:seed');
       $this->seed('DatabaseSeeder');
       $this->session(['test' => 'session']);
       $this->seed('DatabaseSeeder');

    }

    public function tearDown()
    {
      Mockery::close();
      Artisan::call('migrate:reset');
      $this->artisan('migrate:reset');
      Artisan::call('migrate:rollback');
      $this->artisan('migrate:rollback');
      Artisan::call('migrate');
      $this->artisan('migrate');
      Artisan::call('db:seed');
      $this->artisan('db:seed');
      $this->seed('DatabaseSeeder');
      DB::disconnect();

         foreach (\DB::getConnections() as $connection) {
             $connection->disconnect();
         }

      $this->beforeApplicationDestroyed(function () {
         Artisan::call('migrate:reset');
         $this->artisan('migrate:reset');
         Artisan::call('migrate:rollback');
         $this->artisan('migrate:rollback');
         Artisan::call('migrate');
         $this->artisan('migrate');
         Artisan::call('db:seed');
         $this->artisan('db:seed');
         $this->seed('DatabaseSeeder');
         DB::disconnect();
         foreach (\DB::getConnections() as $connection) {
             $connection->disconnect();
         }
      });

       $this->flushSession();
       parent::tearDown();


    }

    public function getConnection()
    {
        $Connection = mysqli_connect($GLOBALS['DB_DSN'], $GLOBALS['DB_USERNAME'], $GLOBALS['DB_PASSWORD'], $GLOBALS['DB_DATABASE']);
        $this->createDefaultDBConnection();
        return $this->Connection;
    }

    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

        return $app;
    }

    /**
     * Magic helper method to make running requests simpler.
     *
     * @param $method
     * @param $args
     * @return \Illuminate\Http\Response
     */
    public function __call($method, $args)
    {
        if (in_array($method, ['get', 'post', 'put', 'patch', 'delete']))
        {
            return $this->call($method, $args[0]);
        }

        throw new BadMethodCallException;
    }

    /**
     * Create a mock of a class as well as an instance.
     *
     * @param $class
     * @return \Mockery\MockInterface
     */
    public function mock($class)
    {
        $mock = Mockery::mock($class);

        $this->app->instance($class, $mock);

        return $mock;
    }

}

我的测试看起来像

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Artisan;

class CustomerRegistrationControllerTest extends TestCase
{

    use DatabaseMigrations;

    protected static $db_inited = false;

    protected static function initDB()
    {
        echo "\n---Customer Registration Controller Tests---\n"; // proof it only runs once per test TestCase class
        Artisan::call('migrate');
        Artisan::call('db:seed');
    }

    public function setUp()
    {

        parent::setUp();

        if (!static::$db_inited) {
            static::$db_inited = true;
            static::initDB();
        }

//        $this->app->refreshApplication();
        $this->artisan('migrate:refresh');
        $this->seed();
        $this->seed('DatabaseSeeder');

        $this->initializeTests();

);

    }


    public function testSomething()

    {


        $this->Mock
            ->shouldReceive('destroy')
            ->with('1')
            ->andReturn();


        $this->RegistrationController->postRegistration();
//      $this->assertResponseStatus(200);

    }

}

只需运行这个:

    $this->artisan('migrate:refresh', [
        '--seed' => '1'
    ]);

为了避免在测试之间持续对数据库进行更改,请添加use DatabaseTransactions到您访问数据库的测试。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laravel 5 在测试之间重新播种数据库以进行单元测试 的相关文章

随机推荐