Skip to content

Commit

Permalink
Merge pull request #174 from RonasIT/165-implement-insert-method
Browse files Browse the repository at this point in the history
#165: Implement insert method
  • Loading branch information
DenTray authored Dec 25, 2024
2 parents 4b76c17 + 30b3d08 commit a3e5465
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/Traits/EntityControlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
70 changes: 68 additions & 2 deletions tests/EntityControlTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');

Expand All @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
31 changes: 31 additions & 0 deletions tests/support/Mock/TestModelWithDifferentTimestampNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace RonasIT\Support\Tests\Support\Mock;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use RonasIT\Support\Traits\ModelTrait;

class TestModelWithDifferentTimestampNames extends Model
{
use ModelTrait;
use SoftDeletes;

public const string CREATED_AT = 'creation_date';
public const string UPDATED_AT = 'updated_date';

public $timestamps = true;

protected $table = 'test_models';

protected $fillable = [
'name',
'json_field',
'castable_field',
];

protected $casts = [
'json_field' => 'array',
'castable_field' => JSONCustomCast::class,
];
}
28 changes: 28 additions & 0 deletions tests/support/Mock/TestModelWithoutTimestamps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace RonasIT\Support\Tests\Support\Mock;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use RonasIT\Support\Traits\ModelTrait;

class TestModelWithoutTimestamps extends Model
{
use ModelTrait;
use SoftDeletes;

public $timestamps = false;

protected $table = 'test_models';

protected $fillable = [
'name',
'json_field',
'castable_field',
];

protected $casts = [
'json_field' => 'array',
'castable_field' => JSONCustomCast::class,
];
}
13 changes: 13 additions & 0 deletions tests/support/Mock/TestRepositoryWithDifferentTimestampNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RonasIT\Support\Tests\Support\Mock;

use RonasIT\Support\Repositories\BaseRepository;

class TestRepositoryWithDifferentTimestampNames extends BaseRepository
{
public function __construct()
{
$this->setModel(TestModelWithDifferentTimestampNames::class);
}
}
13 changes: 13 additions & 0 deletions tests/support/Mock/TestRepositoryWithoutTimestamps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RonasIT\Support\Tests\Support\Mock;

use RonasIT\Support\Repositories\BaseRepository;

class TestRepositoryWithoutTimestamps extends BaseRepository
{
public function __construct()
{
$this->setModel(TestModelWithoutTimestamps::class);
}
}
35 changes: 35 additions & 0 deletions tests/support/Traits/SqlMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit a3e5465

Please sign in to comment.