diff --git a/src/Model.php b/src/Model.php index 4da680f..f95427c 100644 --- a/src/Model.php +++ b/src/Model.php @@ -16,7 +16,7 @@ abstract class Model implements \ArrayAccess, \IteratorAggregate final public function __construct(?array $properties = null) { - if ($this->hasProperties()) { + if (! empty($properties)) { $this->setProperties($properties); } diff --git a/tests/ModelTest.php b/tests/ModelTest.php index ee7c553..0b6105c 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -2,6 +2,8 @@ namespace ipl\Tests\Orm; +use ipl\Orm\Model; + class ModelTest extends \PHPUnit\Framework\TestCase { public function testInitIsCalledAfterConstruction() @@ -21,4 +23,48 @@ public function testOnReturnsQueryWithModelAndDatabaseConnectionAssociated() /** @noinspection PhpParamsInspection */ $this->assertInstanceOf(TestModel::class, $query->getModel()); } + + public function testModelsCanBeInitializedWithProperties(): void + { + $model = new class (['foo' => 'bar']) extends Model { + public function getTableName() + { + return 'test'; + } + + public function getKeyName() + { + return 'id'; + } + + public function getColumns() + { + return ['foo']; + } + }; + + $this->assertSame('bar', $model->foo); + } + + public function testModelsCanBeInitializedWithoutProperties(): void + { + $model = new class extends Model { + public function getTableName() + { + return 'test'; + } + + public function getKeyName() + { + return 'id'; + } + + public function getColumns() + { + return ['foo']; + } + }; + + $this->assertFalse(isset($model->foo)); + } }