微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHPUnit似乎没有运行Laravel Migration

我有一个问题,我通过PHPunit在laravel 5.4中运行一些测试

我正在使用内存sqlite数据库进行测试

我有一个测试类,我已经删除了一堆其他的东西,所以它实际上看起来像

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

我最近创建了一个新的迁移,它添加了“付费”列

<?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');
        });
    }
}

但是,每当我运行此测试时,我都会收到一条错误消息,指出付费列不存在 – 即使在作曲家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

有没有人知道为什么会发生这种情况,以及我如何解决它?可能值得添加我已尝试更改列名等,同样的问题正在发生

谢谢

UPDATE

如果我注释掉向下迁移中的行,例如$table-> dropColumn(‘paid’);

然后它继续运行 – 但是我很难理解为什么在运行up之前down方法会运行?

更新2

似乎上面的发现是由于列没有首先创建,如果我抑制该错误,原始错误出现该列不存在 – 这表明迁移无法创建它.

解决方法:

根据laravel文档

Dropping or modifying multiple columns within a single migration while using a sqlite database is not supported.

虽然您没有尝试修改删除多个列,但您尝试在一次迁移中删除并创建,在这两种情况下都会执行ALTER TABLE查询,此处的问题是limitations of ALTER TABLE query of sqlite.

你可以像这样分开每个语句:

 /**
 * 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');
    });
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐