Added CleanupModel stub

This commit is contained in:
Igor Scheller 2020-10-21 00:11:33 +02:00 committed by msquare
parent 00f812fb77
commit f8d9cddcc6
4 changed files with 127 additions and 0 deletions

View File

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

View File

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

View File

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

View File

@ -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',
];
}