diff --git a/src/Traits/EntityControlTrait.php b/src/Traits/EntityControlTrait.php index 545177e..5a919de 100755 --- a/src/Traits/EntityControlTrait.php +++ b/src/Traits/EntityControlTrait.php @@ -155,6 +155,30 @@ public function create(array $data): Model return $model; } + public function insert(array $data): bool + { + $timestamps = []; + + if ($this->model->timestamps) { + $now = now(); + + $timestamps = [ + $this->model::CREATED_AT => $now, + $this->model::UPDATED_AT => $now + ]; + } + + $data = array_map(function ($item) use ($timestamps) { + $fillableFields = Arr::only($item, $this->model->getFillable()); + + return array_merge($fillableFields, $timestamps); + }, $data); + + $this->postQueryHook(); + + return $this->model->insert($data); + } + /** * Update rows by condition or primary key * diff --git a/tests/EntityControlTraitTest.php b/tests/EntityControlTraitTest.php index fb03911..3a335be 100644 --- a/tests/EntityControlTraitTest.php +++ b/tests/EntityControlTraitTest.php @@ -3,19 +3,25 @@ namespace RonasIT\Support\Tests; use Illuminate\Support\Carbon; +use ReflectionProperty; use RonasIT\Support\Exceptions\InvalidModelException; use RonasIT\Support\Tests\Support\Mock\TestRepository; -use ReflectionProperty; use RonasIT\Support\Tests\Support\Mock\TestRepositoryNoPrimaryKey; +use RonasIT\Support\Tests\Support\Mock\TestRepositoryWithDifferentTimestampNames; +use RonasIT\Support\Tests\Support\Mock\TestRepositoryWithoutTimestamps; use RonasIT\Support\Tests\Support\Traits\SqlMockTrait; class EntityControlTraitTest extends HelpersTestCase { use SqlMockTrait; + protected string $mockedNow = '2020-01-01 00:00:00'; + protected static array $selectResult; protected static TestRepository $testRepositoryClass; + protected static TestRepositoryWithoutTimestamps $testRepositoryClassWithoutTimestamps; + protected static TestRepositoryWithDifferentTimestampNames $testRepositoryWithDifferentTimestampNames; protected ReflectionProperty $onlyTrashedProperty; protected ReflectionProperty $withTrashedProperty; @@ -28,6 +34,8 @@ public function setUp(): void parent::setUp(); self::$testRepositoryClass ??= new TestRepository(); + self::$testRepositoryClassWithoutTimestamps ??= new TestRepositoryWithoutTimestamps(); + self::$testRepositoryWithDifferentTimestampNames ??= new TestRepositoryWithDifferentTimestampNames(); $this->onlyTrashedProperty = new ReflectionProperty(TestRepository::class, 'onlyTrashed'); @@ -41,7 +49,7 @@ public function setUp(): void self::$selectResult ??= $this->getJsonFixture('select_query_result.json'); - Carbon::setTestNow('2020-01-01 00:00:00'); + Carbon::setTestNow($this->mockedNow); } public function testOnlyTrashed() @@ -196,6 +204,64 @@ public function testCreateOnlyFillable() $this->assertSettablePropertiesReset(self::$testRepositoryClass); } + public function testInsert() + { + $this->mockInsertData(); + + $result = self::$testRepositoryClass->insert([ + ['name' => 'test_name_1'], + ['name' => 'test_name_2'], + ['name' => 'test_name_3'], + ]); + + $this->assertTrue($result); + } + + public function testInsertWithSettableProperties() + { + $this->mockInsertData(); + + $result = self::$testRepositoryClass + ->withTrashed() + ->onlyTrashed() + ->force() + ->insert([ + ['name' => 'test_name_1'], + ['name' => 'test_name_2'], + ['name' => 'test_name_3'], + ]); + + $this->assertTrue($result); + + $this->assertSettablePropertiesReset(self::$testRepositoryClass); + } + + public function testInsertWithoutTimestamps() + { + $this->mockInsertDataWithoutTimestamps(); + + $result = self::$testRepositoryClassWithoutTimestamps->insert([ + ['name' => 'test_name_1'], + ['name' => 'test_name_2'], + ['name' => 'test_name_3'], + ]); + + $this->assertTrue($result); + } + + public function testInsertWithDifferentTimestampNames() + { + $this->mockInsertDataWithDifferentTimestampNames(); + + $result = self::$testRepositoryWithDifferentTimestampNames->insert([ + ['name' => 'test_name_1'], + ['name' => 'test_name_2'], + ['name' => 'test_name_3'], + ]); + + $this->assertTrue($result); + } + public function testUpdateMany() { $this->mockUpdateSqlQuery( diff --git a/tests/support/Mock/TestModelWithDifferentTimestampNames.php b/tests/support/Mock/TestModelWithDifferentTimestampNames.php new file mode 100644 index 0000000..79a737c --- /dev/null +++ b/tests/support/Mock/TestModelWithDifferentTimestampNames.php @@ -0,0 +1,31 @@ + 'array', + 'castable_field' => JSONCustomCast::class, + ]; +} diff --git a/tests/support/Mock/TestModelWithoutTimestamps.php b/tests/support/Mock/TestModelWithoutTimestamps.php new file mode 100644 index 0000000..e281b56 --- /dev/null +++ b/tests/support/Mock/TestModelWithoutTimestamps.php @@ -0,0 +1,28 @@ + 'array', + 'castable_field' => JSONCustomCast::class, + ]; +} diff --git a/tests/support/Mock/TestRepositoryWithDifferentTimestampNames.php b/tests/support/Mock/TestRepositoryWithDifferentTimestampNames.php new file mode 100644 index 0000000..44b0bd8 --- /dev/null +++ b/tests/support/Mock/TestRepositoryWithDifferentTimestampNames.php @@ -0,0 +1,13 @@ +setModel(TestModelWithDifferentTimestampNames::class); + } +} diff --git a/tests/support/Mock/TestRepositoryWithoutTimestamps.php b/tests/support/Mock/TestRepositoryWithoutTimestamps.php new file mode 100644 index 0000000..d99c5c8 --- /dev/null +++ b/tests/support/Mock/TestRepositoryWithoutTimestamps.php @@ -0,0 +1,13 @@ +setModel(TestModelWithoutTimestamps::class); + } +} diff --git a/tests/support/Traits/SqlMockTrait.php b/tests/support/Traits/SqlMockTrait.php index 8016121..9953e50 100644 --- a/tests/support/Traits/SqlMockTrait.php +++ b/tests/support/Traits/SqlMockTrait.php @@ -157,6 +157,41 @@ protected function mockCreate(array $selectResult, $notFillableValue): void ); } + protected function mockInsertData(): void + { + $query = 'insert into "test_models" ("created_at", "name", "updated_at") values (?, ?, ?), (?, ?, ?), (?, ?, ?)'; + + $values = [ + $this->mockedNow, 'test_name_1', $this->mockedNow, + $this->mockedNow, 'test_name_2', $this->mockedNow, + $this->mockedNow, 'test_name_3', $this->mockedNow, + ]; + + $this->getPdo()->shouldInsert($query, $values); + } + + protected function mockInsertDataWithoutTimestamps(): void + { + $query = 'insert into "test_models" ("name") values (?), (?), (?)'; + + $values = ['test_name_1', 'test_name_2', 'test_name_3']; + + $this->getPdo()->shouldInsert($query, $values); + } + + protected function mockInsertDataWithDifferentTimestampNames(): void + { + $query = 'insert into "test_models" ("creation_date", "name", "updated_date") values (?, ?, ?), (?, ?, ?), (?, ?, ?)'; + + $values = [ + $this->mockedNow, 'test_name_1', $this->mockedNow, + $this->mockedNow, 'test_name_2', $this->mockedNow, + $this->mockedNow, 'test_name_3', $this->mockedNow, + ]; + + $this->getPdo()->shouldInsert($query, $values); + } + protected function mockUpdate(array $selectResult, $notFillableValue): void { $this->mockSelectById(