Added CleanupModel stub
This commit is contained in:
parent
00f812fb77
commit
f8d9cddcc6
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Controllers;
|
||||
|
||||
use ArrayAccess;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
trait CleanupModel
|
||||
{
|
||||
/**
|
||||
* Used to replace null values with en empty string
|
||||
*
|
||||
* Required because isset on a null value returns false which gets interpreted as missing properties by Twig
|
||||
*
|
||||
* @param Model[]|ArrayAccess|Model $models
|
||||
* @param string[] $attributes
|
||||
*/
|
||||
protected function cleanupModelNullValues($models, array $attributes = [])
|
||||
{
|
||||
$models = $models instanceof Model ? [$models] : $models;
|
||||
foreach ($models as $model) {
|
||||
/** @var Model $model */
|
||||
$attributes = $attributes ?: array_merge(
|
||||
array_keys($model->getAttributes()),
|
||||
array_keys($model->getCasts()),
|
||||
$model->getFillable(),
|
||||
$model->getDates()
|
||||
);
|
||||
foreach ($attributes as $attribute) {
|
||||
$model->$attribute = is_null($model->$attribute) ? '' : $model->$attribute;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Controllers;
|
||||
|
||||
use Engelsystem\Test\Unit\Controllers\Stub\CleanupModelImplementation;
|
||||
use Engelsystem\Test\Unit\Controllers\Stub\TestModel;
|
||||
use Engelsystem\Test\Unit\HasDatabase;
|
||||
use Engelsystem\Test\Unit\TestCase;
|
||||
|
||||
class CleanupModelTest extends TestCase
|
||||
{
|
||||
use HasDatabase;
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Controllers\CleanupModel::cleanupModelNullValues
|
||||
*/
|
||||
public function testCleanupModelNullValues()
|
||||
{
|
||||
$cleanup = new CleanupModelImplementation();
|
||||
|
||||
$model = new TestModel();
|
||||
$model->foo = null;
|
||||
$model2 = new TestModel();
|
||||
$model3 = new TestModel();
|
||||
|
||||
$cleanup->cleanup($model);
|
||||
$cleanup->cleanup([$model2]);
|
||||
$cleanup->cleanup($model3, ['text']);
|
||||
|
||||
$this->assertTrue(isset($model->text));
|
||||
$this->assertTrue(isset($model->created_at));
|
||||
$this->assertTrue(isset($model->foo));
|
||||
$this->assertEquals('', $model->text);
|
||||
|
||||
$this->assertTrue(isset($model2->text));
|
||||
|
||||
$this->assertTrue(isset($model3->text));
|
||||
$this->assertNull($model3->another_text);
|
||||
$this->assertNull($model3->foo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the DB
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->initDatabase();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Controllers\Stub;
|
||||
|
||||
use ArrayAccess;
|
||||
use Engelsystem\Controllers\CleanupModel;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class CleanupModelImplementation
|
||||
{
|
||||
use CleanupModel;
|
||||
|
||||
/**
|
||||
* @param Model|ArrayAccess|Model[] $model
|
||||
* @param array $attributes
|
||||
*/
|
||||
public function cleanup($model, array $attributes = []): void
|
||||
{
|
||||
$this->cleanupModelNullValues($model, $attributes);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Controllers\Stub;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property string $text
|
||||
* @property string $another_text
|
||||
* @property Carbon|null $created_at
|
||||
* @property Carbon|null $updated_at
|
||||
*/
|
||||
class TestModel extends Model
|
||||
{
|
||||
/** @var string[] */
|
||||
protected $fillable = [
|
||||
'text',
|
||||
'another_text',
|
||||
];
|
||||
}
|
Loading…
Reference in New Issue