Я пытаюсь написать простое тестирование PHPUnit, которое проверит мои User class
и некоторые DB
запросы.
Я получаю A facade has not been set
при каждом вызове функции запроса к БД, и я не знаю, почему.
Я забываю импортировать или включить файл, или невозможно запустить PHP единица с laravel DB
классом?
UserTest.php
<?php
namespace Tests\Unit;
use App\User;
use Tests\TestCase;
class UserTest extends TestCase
{
const TABLENAME = 'user';
protected static $userid;
public static function setUpBeforeClass(): void
{
$user = new \stdClass();
$user->name = "Test 1";
$user->email = "test@test.com";
$user->password = "Password";
self::$userid = User::createNewUser($user);
}
/**
* Create a user
*
* @return void
*/
public function testGetUser(): void
{
$user = new User(self::$userid);
var_dump($user);
if (empty($user)) {
$this->assertTrue(false);
} else {
$this->assertTrue(true);
}
}
}
User.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use StdClass;
class User extends Authenticatable
{
use Notifiable;
/**
* 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'];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [];
private $id = 0;
private $username = null;
private $email = null;
private $createdAt = null;
private $updatedAt = null;
const TABLENAME = 'user';
public function __construct(int $userId)
{
$user = DB::table(self::TABLENAME)->where('id', $userId)->get();
$this->id = $user->id;
$this->username = $user->name;
$this->email = $user->email;
$this->createdAt = $user->createdAt;
$this->updatedAt = $user->updatedAt;
}
public function get(string $field): string
{
return $this->{$field};
}
public function updateUser(stdClass $user): boolean
{
$user->id = $this->id;
$result = DB::table(self::TABLENAME)->update((array) $user);
return ($result >= 1) ? true : false;
}
public static function createNewUser(stdClass $user): int
{
return DB::table(self::TABLENAME)->insertGetId((array) $user);
}
}