BaseModel: Removed methods as already provided by Eloquent Builder

This commit is contained in:
Igor Scheller 2019-11-06 12:15:14 +01:00
parent 416c49ae0b
commit 109f112131
3 changed files with 9 additions and 109 deletions

View File

@ -3,49 +3,13 @@
namespace Engelsystem\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
/**
* @mixin Builder
*/
abstract class BaseModel extends Model
{
/** @var bool Disable timestamps by default because of "Datensparsamkeit" */
public $timestamps = false;
/**
* Create a new model
*
* @param array $attributes
* @return BaseModel
*/
public function create(array $attributes = [])
{
$instance = new static($attributes);
$instance->save();
return $instance;
}
/**
* Find a model by its primary key
*
* @param mixed $id
* @param array $columns
* @return Builder|Builder[]|Collection|static|null
*/
public static function find($id, $columns = ['*'])
{
return static::query()->find($id, $columns);
}
/**
* Find a model by its attributes or create a new one
*
* @param mixed $id
* @param array $columns
* @return static|Model
*/
public static function findOrNew($id, $columns = ['*'])
{
return static::query()->findOrNew($id, $columns);
}
}

View File

@ -18,7 +18,9 @@ class EngelsystemLoggerTest extends ServiceProviderTest
public function testLog()
{
/** @var LogEntry|MockObject $logEntry */
$logEntry = $this->createMock(LogEntry::class);
$logEntry = $this->getMockBuilder(LogEntry::class)
->addMethods(['create'])
->getMock();
$logEntry->expects($this->once())
->method('create')
->with(['level' => LogLevel::INFO, 'message' => 'I\'m an information!']);
@ -48,7 +50,9 @@ class EngelsystemLoggerTest extends ServiceProviderTest
public function testInterpolate()
{
/** @var LogEntry|MockObject $logEntry */
$logEntry = $this->createMock(LogEntry::class);
$logEntry = $this->getMockBuilder(LogEntry::class)
->addMethods(['create'])
->getMock();
$logEntry->expects($this->exactly(3))
->method('create')
->withConsecutive(

View File

@ -1,68 +0,0 @@
<?php
namespace Engelsystem\Test\Unit\Models;
use Engelsystem\Test\Unit\Models\Stub\BaseModelImplementation;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class BaseModelTest extends TestCase
{
/**
* @covers \Engelsystem\Models\BaseModel::create
*/
public function testCreate()
{
$model = new BaseModelImplementation();
$newModel = $model->create(['foo' => 'bar']);
$this->assertNotEquals($model, $newModel);
$this->assertEquals('bar', $newModel->foo);
$this->assertEquals(1, $newModel->saveCount);
}
/**
* @covers \Engelsystem\Models\BaseModel::find
*/
public function testFind()
{
/** @var QueryBuilder|MockObject $queryBuilder */
$queryBuilder = $this->createMock(QueryBuilder::class);
BaseModelImplementation::$queryBuilder = $queryBuilder;
$anotherModel = new BaseModelImplementation();
$queryBuilder->expects($this->once())
->method('find')
->with(1337, ['foo', 'bar'])
->willReturn($anotherModel);
$model = new BaseModelImplementation();
$newModel = $model->find(1337, ['foo', 'bar']);
$this->assertEquals($anotherModel, $newModel);
}
/**
* @covers \Engelsystem\Models\BaseModel::findOrNew
*/
public function testFindOrNew()
{
/** @var QueryBuilder|MockObject $queryBuilder */
$queryBuilder = $this->createMock(QueryBuilder::class);
BaseModelImplementation::$queryBuilder = $queryBuilder;
$anotherModel = new BaseModelImplementation();
$queryBuilder->expects($this->once())
->method('findOrNew')
->with(31337, ['lorem', 'ipsum'])
->willReturn($anotherModel);
$model = new BaseModelImplementation();
$newModel = $model->findOrNew(31337, ['lorem', 'ipsum']);
$this->assertEquals($anotherModel, $newModel);
}
}