简体   繁体   English

PHPUnit似乎没有运行Laravel Migration

[英]PHPUnit doesn't seem to be running Laravel Migration

I've got an issue where I'm running some tests in laravel 5.4 via phpunit 我有一个问题,我通过phpunit在laravel 5.4中运行一些测试

I'm using an in memory sqlite database for testing 我正在使用内存sqlite数据库进行测试

I've got a test class which i've removed bunch of other stuff from so it effectively looks like 我有一个测试类,我已经删除了一堆其他的东西,所以它实际上看起来像

<?php

namespace Tests\Unit;

use App\User;
use App\Order;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class OrderTest extends TestCase
{
    use DatabaseMigrations;

    /** @test */
    function can_update_status()
    {
         // This is empty, it fails on this test because its alphabetically the first test in the whole suite.
    }
}

I've recently created a new migration which adds the "paid" column 我最近创建了一个新的迁移,它添加了“付费”列

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddStatusToOrders extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->dropColumn('completed');
            $table->boolean('paid')->default(0);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->boolean('completed')->default(0);
            $table->dropColumn('paid');
        });
    }
}

However whenever I run this test I get an error saying the paid column doesn't exist - even after a composer du 但是,每当我运行此测试时,我都会收到一条错误消息,指出付费列不存在 - 即使在composer du之后也是如此

PHPUnit 6.0.7 by Sebastian Bergmann and contributors.

...................................E

Time: 10.69 seconds, Memory: 46.00MB

There was 1 error:

1) Tests\Unit\OrderTest::can_mark_as_paid
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such column: paid (SQL: update "orders" set "paid" = 1, "updated_at" = 2017-04-05 15:27:11 where "id" = 1)

/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:647
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:607
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:477
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:416
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2145
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:768
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:581
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:501
/Users/owen/Sites/1st-choice-spares/app/Order.php:62
/Users/owen/Sites/1st-choice-spares/tests/Unit/OrderTest.php:95

Caused by
Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000]: General error: 1 no such column: paid

/Users/owen/Sites/1st-choice-spares/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:79
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:470
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:640
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:607
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:477
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:416
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2145
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:768
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:581
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:501
/Users/owen/Sites/1st-choice-spares/app/Order.php:62
/Users/owen/Sites/1st-choice-spares/tests/Unit/OrderTest.php:95

Caused by
PDOException: SQLSTATE[HY000]: General error: 1 no such column: paid

/Users/owen/Sites/1st-choice-spares/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:77
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:470
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:640
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:607
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:477
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Connection.php:416
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2145
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:768
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:581
/Users/owen/Sites/1st-choice-spares/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:501
/Users/owen/Sites/1st-choice-spares/app/Order.php:62
/Users/owen/Sites/1st-choice-spares/tests/Unit/OrderTest.php:95

Does anybody have any ideas why this would be happening, and how I can resolve it? 有没有人知道为什么会发生这种情况,以及我如何解决它? It might be worth adding I've tried changing the column names etc and the same issue is happening with that 可能值得添加我已尝试更改列名等,同样的问题正在发生

Thanks 谢谢

UPDATE UPDATE

If I comment out the lines in the down migration eg $table->dropColumn('paid'); 如果我注释掉向下迁移中的行,例如$table->dropColumn('paid');

Then it continues to run - however I'm struggling to understand why the down method would be running before the up has been run? 然后它继续运行 - 但是我很难理解为什么在运行up之前down方法会运行?

Update 2 更新2

It seems the above finding was due to the column not getting created in the first place, if i suppress that error, the original error appears that the column doesn't exist - which suggests the migration failed to create it. 似乎上面的发现是由于列没有首先创建,如果我抑制该错误,原始错误出现该列不存在 - 这表明迁移无法创建它。

According to laravel documentation 根据laravel文档

Dropping or modifying multiple columns within a single migration while using a SQLite database is not supported. 不支持在使​​用SQLite数据库时在单个迁移中删除或修改多个列。

And although you not trying to modify or drop multiple columns ,you are trying to drop and create in one single migration and in both cases ALTER TABLE query is executed ,and the problem here is the limitations of ALTER TABLE query of sqlite . 虽然您没有尝试修改或删除多个列,但您尝试在一次迁移中删除并创建,并且在两种情况下都执行ALTER TABLE查询,此处的问题是sqlite的ALTER TABLE查询限制

You can separate each statement like this: 你可以像这样分开每个语句:

 /**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('orders', function (Blueprint $table) {
        $table->dropColumn('completed');
    });

   Schema::table('orders', function (Blueprint $table) {
         $table->boolean('paid')->default(0);
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('orders', function (Blueprint $table) {
        $table->boolean('completed')->default(0);
    });
   Schema::table('orders', function (Blueprint $table) {
     $table->dropColumn('paid');
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM