执行phpunit

时间:2018-12-04 10:27:57

标签: laravel migration lumen

在我的Lumen应用程序中,当我执行

php artisan migrate --seed

效果很好。

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

我在测试中使用以下命令运行迁移:

Artisan::call('migrate');

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

这是我的流明应用程序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应用程序中使用了相同的软件包。但我正在将此应用迁移到流明应用。

知道为什么会发生吗?

1 个答案:

答案 0 :(得分:0)

首先要说几句话:

您的测试功能不是以test开头,对我而言,在Laravel中此类测试不会执行。

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

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

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