-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(database): Mimic save (timestamps, exists, generated id) for saf…
…e unique save action
- Loading branch information
Showing
5 changed files
with
133 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Database\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
/** | ||
* @property int $test | ||
* @property Carbon|null $deleted_at | ||
*/ | ||
class TestNoDates extends Model | ||
{ | ||
use HasFactory; | ||
use SoftDeletes; | ||
|
||
final public const CREATED_AT = null; | ||
|
||
final public const UPDATED_AT = null; | ||
|
||
final public const AttributeTest = 'test'; | ||
|
||
final public const AttributeDeletedAt = 'deleted_at'; | ||
|
||
protected $fillable = [self::AttributeTest]; | ||
} |
95 changes: 95 additions & 0 deletions
95
tests/Feature/Testing/Database/Contracts/SafeUniqueSaveActionContractAssertTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Testing\Database\Contracts; | ||
|
||
use LaraStrict\Testing\Database\Contracts\SafeUniqueSaveActionContractAssert; | ||
use LaraStrict\Testing\Database\Contracts\SafeUniqueSaveActionContractExpectation; | ||
use Tests\LaraStrict\Feature\Database\Models\Test; | ||
use Tests\LaraStrict\Feature\Database\Models\TestNoDates; | ||
use Tests\LaraStrict\Feature\TestCase; | ||
|
||
class SafeUniqueSaveActionContractAssertTest extends TestCase | ||
{ | ||
public function testExecuteOneTry(): void | ||
{ | ||
$model = new Test(); | ||
$expectations = [new SafeUniqueSaveActionContractExpectation($model)]; | ||
$expectedResult = 'test1'; | ||
|
||
$this->assertExecute($expectations, $model, $expectedResult); | ||
} | ||
|
||
public function testExecuteOneTryNoDates(): void | ||
{ | ||
$model = new TestNoDates(); | ||
$expectations = [new SafeUniqueSaveActionContractExpectation($model)]; | ||
$expectedResult = 'test1'; | ||
|
||
$this->assertExecute($expectations, $model, $expectedResult); | ||
} | ||
|
||
public function testExecuteOneTryWithSetId(): void | ||
{ | ||
$model = new Test(); | ||
$expectations = [new SafeUniqueSaveActionContractExpectation($model, setId: 1)]; | ||
$expectedResult = 'test1'; | ||
|
||
$this->assertExecute($expectations, $model, $expectedResult, 1); | ||
} | ||
|
||
public function testExecuteTwoTries(): void | ||
{ | ||
$model = new Test(); | ||
$expectations = [ | ||
new SafeUniqueSaveActionContractExpectation($model, fail: true), | ||
new SafeUniqueSaveActionContractExpectation($model, tries: 2), | ||
]; | ||
$expectedResult = 'test2'; | ||
|
||
$this->assertExecute($expectations, $model, $expectedResult); | ||
} | ||
|
||
public function testExecuteTwoTriesWithId(): void | ||
{ | ||
$model = new Test(); | ||
$expectations = [ | ||
new SafeUniqueSaveActionContractExpectation($model, fail: true, setId: 2), | ||
new SafeUniqueSaveActionContractExpectation($model, tries: 2, setId: 1), | ||
]; | ||
$expectedResult = 'test2'; | ||
|
||
$this->assertExecute($expectations, $model, $expectedResult, 1); | ||
} | ||
|
||
protected function assertExecute( | ||
array $expectations, | ||
Test|TestNoDates $model, | ||
string $expectedResult, | ||
?int $expectedId = null | ||
): void { | ||
$assert = new SafeUniqueSaveActionContractAssert($expectations); | ||
|
||
$calls = 0; | ||
$result = $assert->execute($model, function (Test|TestNoDates $model, int $tries) use (&$calls) { | ||
++$calls; | ||
$this->assertEquals($calls, $tries); | ||
return 'test' . $calls; | ||
}); | ||
|
||
$this->assertEquals($expectedResult, $result); | ||
|
||
$createdAt = $model->getAttribute('created_at'); | ||
$updatedAt = $model->getAttribute('updated_at'); | ||
if ($model instanceof TestNoDates) { | ||
$this->assertNull($createdAt); | ||
$this->assertNull($updatedAt); | ||
} else { | ||
$this->assertNotNull($createdAt); | ||
$this->assertNotNull($updatedAt); | ||
} | ||
|
||
$this->assertEquals($expectedId, $model->getKey()); | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
tests/Unit/Testing/Database/Contracts/SafeUniqueSaveActionContractAssertTest.php
This file was deleted.
Oops, something went wrong.