diff --git a/src/Tests/ModelTestState.php b/src/Tests/ModelTestState.php index df769dae..defbe502 100644 --- a/src/Tests/ModelTestState.php +++ b/src/Tests/ModelTestState.php @@ -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; @@ -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 diff --git a/tests/ModelTestStateTest.php b/tests/ModelTestStateTest.php index 6fb72ce2..409b78b0 100644 --- a/tests/ModelTestStateTest.php +++ b/tests/ModelTestStateTest.php @@ -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; @@ -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')); diff --git a/tests/fixtures/ModelTestState/changes/test_models/assertion_fixture.json b/tests/fixtures/ModelTestStateTest/assertion_fixture.json similarity index 100% rename from tests/fixtures/ModelTestState/changes/test_models/assertion_fixture.json rename to tests/fixtures/ModelTestStateTest/assertion_fixture.json diff --git a/tests/fixtures/ModelTestStateTest/assertion_fixture_without_json_fields.json b/tests/fixtures/ModelTestStateTest/assertion_fixture_without_json_fields.json new file mode 100644 index 00000000..32c13633 --- /dev/null +++ b/tests/fixtures/ModelTestStateTest/assertion_fixture_without_json_fields.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/tests/fixtures/ModelTestStateTest/changes_equals_fixture_without_json_fields/changed_dataset.json b/tests/fixtures/ModelTestStateTest/changes_equals_fixture_without_json_fields/changed_dataset.json new file mode 100644 index 00000000..08ab11f4 --- /dev/null +++ b/tests/fixtures/ModelTestStateTest/changes_equals_fixture_without_json_fields/changed_dataset.json @@ -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" + } +] diff --git a/tests/fixtures/ModelTestStateTest/changes_equals_fixture_without_json_fields/initial_dataset.json b/tests/fixtures/ModelTestStateTest/changes_equals_fixture_without_json_fields/initial_dataset.json new file mode 100644 index 00000000..dbb38b87 --- /dev/null +++ b/tests/fixtures/ModelTestStateTest/changes_equals_fixture_without_json_fields/initial_dataset.json @@ -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" + } +] diff --git a/tests/support/Mock/TestModelWithoutJsonFields.php b/tests/support/Mock/TestModelWithoutJsonFields.php new file mode 100644 index 00000000..1ca67a03 --- /dev/null +++ b/tests/support/Mock/TestModelWithoutJsonFields.php @@ -0,0 +1,16 @@ +