【问题标题】:Lumen is not migrating package tables when executing phpunitLumen 在执行 phpunit 时不会迁移包表
【发布时间】:2019-05-05 18:06:30
【问题描述】:

在我的 Lumen 应用中,当我执行时

php artisan migrate --seed

效果很好。

但是当我尝试使用 phpunit 运行测试时,它不会从我编码的 Laravel 包运行迁移,因此所有测试都失败

我在我的测试中运行我的迁移:

Artisan::call('migrate');

我用于内存测试以加快运行速度。

这是我的 Lumen 应用 Testcase.php

abstract class TestCase extends Laravel\Lumen\Testing\TestCase
{
    /** @var array */
    protected $dispatchedNotifications = [];
    protected static $applicationRefreshed = false;
    /**
     * Creates the application.
     *
     * @return \Laravel\Lumen\Application
     */
    /**
     * Creates the application.
     *
     */
    public function createApplication()
    {
        return self::initialize();
    }

    private static $configurationApp = null;

    public static function initialize()
    {
        $app = require __DIR__ . '/../bootstrap/app.php';
        if (is_null(self::$configurationApp)) {
            $app->environment('testing');

            if (config('database.default') == 'sqlite') {

                $db = app()->make('db');
                $db->connection()->getPdo()->exec("pragma foreign_keys=1");
            }

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

            self::$configurationApp = $app;
        }

        return $app;
    }


    /**
     * Refresh the application instance.
     *
     * @return void
     */
    protected function forceRefreshApplication()
    {
        if (!is_null($this->app)) {
            $this->app->flush();
        }
        $this->app = null;
        self::$configurationApp = null;
        self::$applicationRefreshed = true;
        parent::refreshApplication();
    }
    ...

在我的包中,我在服务提供者的启动方法中使用:

$this->loadMigrationsFrom(__DIR__.'/../database/migrations');

然后是一个测试示例:

class TournamentsTest extends TestCase
{

    use DatabaseTransactions, AttachJwtToken;
    protected $initialTournamentNum = 6;
    protected $defaultPagintation = 25;
    protected $user;


    /** @test */
    public function user_can_see_tournament_list()
    {
        $response = $this
            ->call('GET', '/tournaments');
        $this->assertEquals(HttpResponse::HTTP_OK, $response->status());
    }
    ...

我所有的测试都失败了:

PDOException: SQLSTATE[HY000]: General error: 1 no such table: ken_venue

ken_venue 是一张来自 laravel 包的表格

事实上,我有同样的包在 Laravel 5.7 应用程序中运行良好。但我正在将此应用程序迁移到 Lumen 应用程序。

知道为什么会这样吗?

【问题讨论】:

  • 您是否将包中的迁移文件发布到您的迁移文件夹中?
  • 没有。我认为目前没有必要

标签: laravel migration lumen


【解决方案1】:

先说几点:

你的测试函数不是以 test 开头的,对我来说,在 Laravel 中这样的测试不会执行。

第二次尝试运行种子,而不是手动调用 migrate 和 seed

namespace Tests\Unit;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;

class ShippingCostTest extends TestCase
{
    use DatabaseMigrations; // call migrations this way

    public function testDoesApply() // start your function with the word 'test'

        $this->assertTrue(true); // call assert functions
    }

    protected function setUp() // use this function for setup
    {
        parent::setUp();
        $this->seed(); // call this for seeding
    }
}

第三:你是否使用相同的数据库类型(例如 MySQL 在这两种情况下),因为你使用 sqlite 来测试语法可能会因为系统之间的差异而突然中断。

【讨论】:

  • 它不是以 test_ 开头但它有 /** @test */ 所以它会触发
猜你喜欢
  • 2015-12-29
  • 2018-11-10
  • 2017-04-12
  • 2019-12-19
  • 1970-01-01
  • 2019-04-07
  • 2020-05-23
  • 2016-03-22
  • 2020-12-01
相关资源
最近更新 更多