PHPUnit 似乎没有运行 Laravel 迁移
Posted
技术标签:
【中文标题】PHPUnit 似乎没有运行 Laravel 迁移【英文标题】:PHPUnit doesn't seem to be running Laravel Migration 【发布时间】:2017-08-31 06:39:16 【问题描述】:我遇到了一个问题,我通过 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');
);
但是,每当我运行此测试时,我都会收到一条错误消息,指出付费列不存在 - 即使在 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
有没有人知道为什么会发生这种情况,我该如何解决?可能值得添加我已经尝试更改列名等,并且同样的问题正在发生
谢谢
更新
如果我注释掉向下迁移中的行,例如 $table->dropColumn('paid');
然后它继续运行 - 但是我很难理解为什么 down 方法会在 up 运行之前运行?
更新 2
似乎上述发现是由于一开始没有创建该列,如果我抑制该错误,则原始错误似乎表明该列不存在 - 这表明迁移无法创建它。
【问题讨论】:
我看到失败的测试是 Tests\Unit\BasketTest::can_get_quotes 你能发布这个测试吗? 它只是失败了,因为 BasketTest 是按字母顺序排列的第一个,它在实际测试中没有任何内容,因为我已经将它们注释掉了。所以它就像它在迁移之后发生的那样 - 或者迁移甚至没有运行 Laravel 文档说“不支持在使用 SQLite 数据库时在单个迁移中删除或修改多个列。”也许这就是原因? 嗯我想知道,虽然我没有删除或修改多个列 :D 我正在添加新列。但是如果我再次遇到这个问题,我会试试这个,目前我已经将旧迁移改装到新架构,因为它还没有投入生产,所以可以从头开始手动重新运行它们 【参考方案1】:根据 laravel 文档
不支持在使用 SQLite 数据库时在单个迁移中删除或修改多个列。
虽然您没有尝试修改或删除多个列,但您尝试在一个迁移中删除和创建,并且在这两种情况下执行 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');
);
【讨论】:
这确实合乎逻辑,并且会尝试一下 - 我会投票,但在可以确认之前不会标记为“答案”!但是感谢您的建议:) @Owen 你可以把它标记为答案,我可以确认。以上是关于PHPUnit 似乎没有运行 Laravel 迁移的主要内容,如果未能解决你的问题,请参考以下文章
Laravel PHPUnit测试运行产生异常创建调度程序后无法设置默认工厂
Laravel 5:PHPUnit 和没有可用的代码覆盖驱动程序
guard-laravel 报告“你的机器上没有安装 phpunit”