HasUserModel: Moved user method to trait

This commit is contained in:
Igor Scheller 2019-11-03 00:25:35 +01:00
parent 4c81fee548
commit a514ba3adc
4 changed files with 36 additions and 31 deletions

View File

@ -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);
}
} }

View File

@ -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);
}
}

View File

@ -1,9 +0,0 @@
<?php
namespace Engelsystem\Test\Unit\Models\User\Stub;
use Engelsystem\Models\User\HasUserModel;
class HasUserModelImplementation extends HasUserModel
{
}

View File

@ -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());
} }