From 109f112131fd13b5b7c7f26bc9413071dad566f1 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 6 Nov 2019 12:15:14 +0100 Subject: [PATCH] BaseModel: Removed methods as already provided by Eloquent Builder --- src/Models/BaseModel.php | 42 +------------ tests/Unit/Logger/EngelsystemLoggerTest.php | 8 ++- tests/Unit/Models/BaseModelTest.php | 68 --------------------- 3 files changed, 9 insertions(+), 109 deletions(-) delete mode 100644 tests/Unit/Models/BaseModelTest.php diff --git a/src/Models/BaseModel.php b/src/Models/BaseModel.php index 49255905..6251929c 100644 --- a/src/Models/BaseModel.php +++ b/src/Models/BaseModel.php @@ -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); - } } diff --git a/tests/Unit/Logger/EngelsystemLoggerTest.php b/tests/Unit/Logger/EngelsystemLoggerTest.php index 0f4c8c32..3b67aa29 100644 --- a/tests/Unit/Logger/EngelsystemLoggerTest.php +++ b/tests/Unit/Logger/EngelsystemLoggerTest.php @@ -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( diff --git a/tests/Unit/Models/BaseModelTest.php b/tests/Unit/Models/BaseModelTest.php deleted file mode 100644 index 963ea64a..00000000 --- a/tests/Unit/Models/BaseModelTest.php +++ /dev/null @@ -1,68 +0,0 @@ -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); - } -}