Skip to content

Commit

Permalink
Merge pull request #102 from RonasIT/add-nova-test-trait
Browse files Browse the repository at this point in the history
chore: rewrite model test state assertion.
  • Loading branch information
DenTray authored Dec 19, 2023
2 parents 78bc60d + d31ee75 commit 259760f
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Tests/ModelTestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use PHPUnit\Framework\Assert;
use RonasIT\Support\Traits\FixturesTrait;

Expand Down Expand Up @@ -109,12 +110,11 @@ protected function isJsonCast(string $cast): bool

protected function getFixturePath(string $fixtureName): string
{
$class = get_class($this);
$explodedClass = explode('\\', $class);
$className = Arr::last($explodedClass);
$table = $this->model->getTable();
$testClassTrace = Arr::first(debug_backtrace(), fn ($trace) => str_ends_with($trace['file'], 'Test.php'));
$testFileName = Arr::last(explode('/', $testClassTrace['file']));
$testClass = Str::remove('.php', $testFileName);

return base_path("tests/fixtures/{$className}/changes/{$table}/{$fixtureName}");
return base_path("tests/fixtures/{$testClass}/{$fixtureName}");
}

protected function getDataSet(string $table, string $orderField = 'id'): Collection
Expand Down
11 changes: 11 additions & 0 deletions tests/ModelTestStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ReflectionClass;
use RonasIT\Support\Tests\Support\Mock\TestModel;
use RonasIT\Support\Tests\Support\Mock\TestModelWithoutJsonFields;
use RonasIT\Support\Tests\Support\Traits\ModelTestStateMockTrait;
use RonasIT\Support\Tests\Support\Traits\MockTrait;

Expand Down Expand Up @@ -49,6 +50,16 @@ public function testAssertChangesEqualsFixture()
$modelTestState->assertChangesEqualsFixture('assertion_fixture.json');
}

public function testAssertChangesWithoutJsonFields()
{
$initialDatasetMock = collect($this->getJsonFixture('changes_equals_fixture_without_json_fields/initial_dataset.json'));
$changedDatasetMock = collect($this->getJsonFixture('changes_equals_fixture_without_json_fields/changed_dataset.json'));
$this->mockGettingDatasetForChanges($changedDatasetMock, $initialDatasetMock);

$modelTestState = new ModelTestState(TestModelWithoutJsonFields::class);
$modelTestState->assertChangesEqualsFixture('assertion_fixture_without_json_fields.json');
}

public function testAssertNoChanges()
{
$datasetMock = collect($this->getJsonFixture('get_without_changes/dataset.json'));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"updated": [
{
"id": 1,
"name": "name1 updated"
}
],
"created": [
{
"id": 4,
"name": "name44",
"created_at": "2018-10-10 10:10:10",
"updated_at": "2018-10-10 10:10:10"
}
],
"deleted": [
{
"id": 5,
"name": "name5",
"created_at": "2018-10-10 10:10:10",
"updated_at": "2018-10-10 10:10:10"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"id": 1,
"name": "name1 updated",
"created_at": "2018-10-10 10:10:10",
"updated_at": "2018-10-10 10:10:10"
},
{
"id": 2,
"name": "name2",
"created_at": "2018-10-10 10:10:10",
"updated_at": "2018-10-10 10:10:10"
},
{
"id": 3,
"name": "name3",
"created_at": "2018-10-10 10:10:10",
"updated_at": "2018-10-10 10:10:10"
},
{
"id": 4,
"name": "name44",
"created_at": "2018-10-10 10:10:10",
"updated_at": "2018-10-10 10:10:10"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"id": 1,
"name": "name1",
"created_at": "2018-10-10 10:10:10",
"updated_at": "2018-10-10 10:10:10"
},
{
"id": 2,
"name": "name2",
"created_at": "2018-10-10 10:10:10",
"updated_at": "2018-10-10 10:10:10"
},
{
"id": 3,
"name": "name3",
"created_at": "2018-10-10 10:10:10",
"updated_at": "2018-10-10 10:10:10"
},
{
"id": 5,
"name": "name5",
"created_at": "2018-10-10 10:10:10",
"updated_at": "2018-10-10 10:10:10"
}
]
16 changes: 16 additions & 0 deletions tests/support/Mock/TestModelWithoutJsonFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace RonasIT\Support\Tests\Support\Mock;

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

class TestModelWithoutJsonFields extends Model
{
use ModelTrait, SoftDeletes;

protected $fillable = [
'name',
];
}

0 comments on commit 259760f

Please sign in to comment.