From f8d9cddcc69499999477da11a03112d287eee90d Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 21 Oct 2020 00:11:33 +0200 Subject: [PATCH] Added CleanupModel stub --- src/Controllers/CleanupModel.php | 34 +++++++++++++ tests/Unit/Controllers/CleanupModelTest.php | 51 +++++++++++++++++++ .../Stub/CleanupModelImplementation.php | 21 ++++++++ tests/Unit/Controllers/Stub/TestModel.php | 21 ++++++++ 4 files changed, 127 insertions(+) create mode 100644 src/Controllers/CleanupModel.php create mode 100644 tests/Unit/Controllers/CleanupModelTest.php create mode 100644 tests/Unit/Controllers/Stub/CleanupModelImplementation.php create mode 100644 tests/Unit/Controllers/Stub/TestModel.php diff --git a/src/Controllers/CleanupModel.php b/src/Controllers/CleanupModel.php new file mode 100644 index 00000000..b1f28f8b --- /dev/null +++ b/src/Controllers/CleanupModel.php @@ -0,0 +1,34 @@ +getAttributes()), + array_keys($model->getCasts()), + $model->getFillable(), + $model->getDates() + ); + foreach ($attributes as $attribute) { + $model->$attribute = is_null($model->$attribute) ? '' : $model->$attribute; + } + } + } +} diff --git a/tests/Unit/Controllers/CleanupModelTest.php b/tests/Unit/Controllers/CleanupModelTest.php new file mode 100644 index 00000000..3a25f136 --- /dev/null +++ b/tests/Unit/Controllers/CleanupModelTest.php @@ -0,0 +1,51 @@ +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(); + } +} diff --git a/tests/Unit/Controllers/Stub/CleanupModelImplementation.php b/tests/Unit/Controllers/Stub/CleanupModelImplementation.php new file mode 100644 index 00000000..b62efb3f --- /dev/null +++ b/tests/Unit/Controllers/Stub/CleanupModelImplementation.php @@ -0,0 +1,21 @@ +cleanupModelNullValues($model, $attributes); + } +} diff --git a/tests/Unit/Controllers/Stub/TestModel.php b/tests/Unit/Controllers/Stub/TestModel.php new file mode 100644 index 00000000..dc054753 --- /dev/null +++ b/tests/Unit/Controllers/Stub/TestModel.php @@ -0,0 +1,21 @@ +