После обновления до laravel 5.8 / PHPUnit 8 у меня было несколько тестов, которые начали давать сбой. Например, следующий тест.
public function testAdminCanPromoteUsers()
{
// While using a admin account try to promote non-admin user
$this->actingAs($this->admin)
->post('user/promote', ['userPromoteId' => $this->user->id, 'name' => $this->user->name]);
// check if user was promoted to admin
$user = User::find($this->user->id);
$this->assertTrue((bool) $user->isAdmin);
}
Маршруты
| POST | user/allow | App\Http\Controllers\UserController@allow | web,admin|
| POST | user/demote | App\Http\Controllers\UserController@demote | web,admin|
| POST | user/promote | App\Http\Controllers\UserController@promote | web,admin|
| POST | user/reset | App\Http\Controllers\UserController@reset | web,admin|
| POST | user/restrict | App\Http\Controllers\UserController@restrict | web,admin|
модель пользователя
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Auth;
class User extends Authenticatable
{
use Notifiable;
public static $snakeAttributes = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
миграция пользователя
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email', 191)->unique();
$table->string('password');
$table->boolean('isAdmin')->default(false);
$table->boolean('hasAccess')->default(true);
$table->rememberToken();
$table->timestamps();
});
}
Функция UserController
/**
* Make an user as admin
*/
public function promote(Request $request) {
// Create the collection name
$thisUsr = User::findOrFail($request->userPromoteId);
if (strcasecmp($thisUsr->name, $request->name) == 0) {
$thisUsr->isAdmin = true;
$thisUsr->save();
return redirect()->route('userIndex');
}
// Error message
return redirect()->route('userIndex')->withErrors("Failed to Promote User to Admin. User wasn't found.");
}
Результаты PHPUnit
1) UserControllerTest :: testAdminCanPromoteUsers
Не удалось подтвердить, что значение false равно true.
/ var / www / tests / Feature /Controllers / UserControllerTest.php: 61
В браузере все по-прежнему работает, тест не пройден. Любые идеи о том, что могло измениться, могут привести к сбою.