HasUserModel: Moved user method to trait
This commit is contained in:
parent
4c81fee548
commit
a514ba3adc
|
@ -3,17 +3,11 @@
|
||||||
namespace Engelsystem\Models\User;
|
namespace Engelsystem\Models\User;
|
||||||
|
|
||||||
use Engelsystem\Models\BaseModel;
|
use Engelsystem\Models\BaseModel;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @property integer $user_id
|
|
||||||
*
|
|
||||||
* @property-read \Illuminate\Database\Query\Builder|\Engelsystem\Models\User\User $user
|
|
||||||
*
|
|
||||||
* @method static \Illuminate\Database\Query\Builder|static whereUserId($value)
|
|
||||||
*/
|
|
||||||
abstract class HasUserModel extends BaseModel
|
abstract class HasUserModel extends BaseModel
|
||||||
{
|
{
|
||||||
|
use UsesUserModel;
|
||||||
|
|
||||||
/** @var string The primary key for the model */
|
/** @var string The primary key for the model */
|
||||||
protected $primaryKey = 'user_id';
|
protected $primaryKey = 'user_id';
|
||||||
|
|
||||||
|
@ -24,12 +18,4 @@ abstract class HasUserModel extends BaseModel
|
||||||
|
|
||||||
/** The relationships that should be touched on save */
|
/** The relationships that should be touched on save */
|
||||||
protected $touches = ['user'];
|
protected $touches = ['user'];
|
||||||
|
|
||||||
/**
|
|
||||||
* @return BelongsTo
|
|
||||||
*/
|
|
||||||
public function user()
|
|
||||||
{
|
|
||||||
return $this->belongsTo(User::class);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Engelsystem\Models\User;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property integer $user_id
|
||||||
|
*
|
||||||
|
* @property-read \Illuminate\Database\Query\Builder|\Engelsystem\Models\User\User $user
|
||||||
|
*
|
||||||
|
* @method static \Illuminate\Database\Query\Builder|static whereUserId($value)
|
||||||
|
*/
|
||||||
|
trait UsesUserModel
|
||||||
|
{
|
||||||
|
// protected $fillable = ['user_id'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BelongsTo
|
||||||
|
*/
|
||||||
|
public function user()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Engelsystem\Test\Unit\Models\User\Stub;
|
|
||||||
|
|
||||||
use Engelsystem\Models\User\HasUserModel;
|
|
||||||
|
|
||||||
class HasUserModelImplementation extends HasUserModel
|
|
||||||
{
|
|
||||||
}
|
|
|
@ -2,23 +2,26 @@
|
||||||
|
|
||||||
namespace Engelsystem\Test\Unit\Models;
|
namespace Engelsystem\Test\Unit\Models;
|
||||||
|
|
||||||
use Engelsystem\Models\User\HasUserModel;
|
use Engelsystem\Models\BaseModel;
|
||||||
|
use Engelsystem\Models\User\UsesUserModel;
|
||||||
use Engelsystem\Test\Unit\HasDatabase;
|
use Engelsystem\Test\Unit\HasDatabase;
|
||||||
use Engelsystem\Test\Unit\Models\User\Stub\HasUserModelImplementation;
|
|
||||||
use Engelsystem\Test\Unit\TestCase;
|
use Engelsystem\Test\Unit\TestCase;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
|
||||||
class HasUserModelTest extends TestCase
|
class UsesUserModelTest extends TestCase
|
||||||
{
|
{
|
||||||
use HasDatabase;
|
use HasDatabase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers \Engelsystem\Models\User\HasUserModel::user
|
* @covers \Engelsystem\Models\User\UsesUserModel::user
|
||||||
*/
|
*/
|
||||||
public function testHasOneRelations()
|
public function testHasOneRelations()
|
||||||
{
|
{
|
||||||
/** @var HasUserModel $contact */
|
/** @var UsesUserModel $contact */
|
||||||
$model = new HasUserModelImplementation();
|
$model = new class extends BaseModel
|
||||||
|
{
|
||||||
|
use UsesUserModel;
|
||||||
|
};
|
||||||
|
|
||||||
$this->assertInstanceOf(BelongsTo::class, $model->user());
|
$this->assertInstanceOf(BelongsTo::class, $model->user());
|
||||||
}
|
}
|
Loading…
Reference in New Issue