-
Notifications
You must be signed in to change notification settings - Fork 14
[263]: fix ModelTestState to support custom Eloquent casts over JSON columns
#265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 29 commits
22c9d7f
2c0f541
240637a
0356a54
075f157
d64496a
6b1b92a
19b6717
f3f6048
0b09513
3af9add
4422f48
a93a96b
42de2c8
289c6b7
6813e61
1d247d5
28d4337
3a99466
f94fc40
28ee396
b02d347
095e3e1
88d2de3
0b16c8e
a280046
1d2ac74
0c21109
5403811
d753cc4
cdc6d97
e56fb4f
af43832
3408629
8733454
120127f
0f4ed1d
029980a
2232ded
2fc683c
1d6ca46
d6523be
38c1dbd
6cfa25a
08f0169
069eca0
3c36461
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,12 @@ | |
| use RonasIT\Support\Testing\ModelTestState; | ||
| use RonasIT\Support\Tests\Support\Mock\Models\TestModel; | ||
| use RonasIT\Support\Tests\Support\Mock\Models\TestModelNonIdPrimaryKey; | ||
| use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithoutJsonFields; | ||
| use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithCastable; | ||
| use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithCrossAttributeCast; | ||
| use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithCustomCast; | ||
| use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithNativeJsonCasts; | ||
| use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithParameterizedCast; | ||
| use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithPrimitiveCasts; | ||
| use RonasIT\Support\Tests\Support\Traits\TableTestStateMockTrait; | ||
|
|
||
| class ModelTestStateTest extends TestCase | ||
|
|
@@ -23,20 +28,21 @@ public function setUp(): void | |
| putenv('FAIL_EXPORT_JSON=false'); | ||
| } | ||
|
|
||
| public function testInitialization() | ||
| public function testInitialization(): void | ||
| { | ||
| $datasetMock = collect($this->getJsonFixture('initialization/dataset.json')); | ||
| $originRecords = collect($this->getJsonFixture('initialization/origin_records.json')); | ||
| $datasetMock = collect($this->getJsonFixture('initialization/dataset')); | ||
| $originRecords = collect($this->getJsonFixture('initialization/origin_records')); | ||
|
|
||
| $this->mockGettingDataset($datasetMock); | ||
|
|
||
| $modelTestState = new ModelTestState(TestModel::class); | ||
| $reflectionClass = new ReflectionClass($modelTestState); | ||
|
|
||
| $jsonFields = $this->getProtectedProperty($reflectionClass, 'jsonFields', $modelTestState); | ||
| $customCastFields = $this->getProtectedProperty($reflectionClass, 'castFields', $modelTestState); | ||
| $state = $this->getProtectedProperty($reflectionClass, 'state', $modelTestState); | ||
|
|
||
| $this->assertEquals(['json_field', 'castable_field'], $jsonFields); | ||
| $this->assertEquals(['id', 'settings', 'deleted_at'], $customCastFields); | ||
|
|
||
| $this->assertEquals($originRecords, $state); | ||
| } | ||
|
|
||
|
|
@@ -53,60 +59,86 @@ public static function getInitializationViaPrepareModelTestStateFilters(): array | |
| } | ||
|
|
||
| #[DataProvider('getInitializationViaPrepareModelTestStateFilters')] | ||
| public function testInitializationViaPrepareTableTestState(bool $testCaseGlobalExportMode) | ||
| public function testInitializationViaPrepareTableTestState(bool $testCaseGlobalExportMode): void | ||
| { | ||
| $datasetMock = collect($this->getJsonFixture('initialization/dataset.json')); | ||
| $datasetMock = collect($this->getJsonFixture('initialization/dataset')); | ||
| $this->mockGettingDataset($datasetMock); | ||
|
|
||
| $actualGlobalExportModeValue = $this->mockTestStateCreationSetGlobalExportMode('prepareModelTestState', TestModel::class, $testCaseGlobalExportMode); | ||
|
|
||
| $this->assertEquals($actualGlobalExportModeValue, $testCaseGlobalExportMode); | ||
| } | ||
|
|
||
| public function testAssertChangesEqualsFixture() | ||
| public static function getChangeScenarios(): array | ||
| { | ||
| $initialDatasetMock = collect($this->getJsonFixture('changes_equals_fixture/initial_dataset.json')); | ||
| $changedDatasetMock = collect($this->getJsonFixture('changes_equals_fixture/changed_dataset.json')); | ||
|
|
||
| $this->mockGettingDatasetForChanges($changedDatasetMock, $initialDatasetMock, 'test_models'); | ||
|
|
||
| $modelTestState = new ModelTestState(TestModel::class); | ||
| $modelTestState->assertChangesEqualsFixture('assertion_fixture.json'); | ||
| return [ | ||
| 'base' => [ | ||
| 'fixtureDir' => 'changes_equals_fixture', | ||
| 'table' => 'test_models', | ||
| 'modelClass' => TestModel::class, | ||
| ], | ||
| 'primitive casts' => [ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dropping the Fixed on the branch: |
||
| 'fixtureDir' => 'changes_equals_fixture_with_primitive_casts', | ||
| 'table' => 'test_model_with_primitive_casts', | ||
| 'modelClass' => TestModelWithPrimitiveCasts::class, | ||
| ], | ||
| 'native json casts' => [ | ||
| 'fixtureDir' => 'changes_equals_fixture_with_native_json_casts', | ||
| 'table' => 'test_model_with_native_json_casts', | ||
| 'modelClass' => TestModelWithNativeJsonCasts::class, | ||
| ], | ||
| 'custom cast' => [ | ||
| 'fixtureDir' => 'changes_equals_fixture_with_custom_cast', | ||
| 'table' => 'test_model_with_custom_casts', | ||
| 'modelClass' => TestModelWithCustomCast::class, | ||
| ], | ||
| 'parameterized cast' => [ | ||
| 'fixtureDir' => 'changes_equals_fixture_with_parameterized_cast', | ||
| 'table' => 'test_model_with_parameterized_casts', | ||
| 'modelClass' => TestModelWithParameterizedCast::class, | ||
| ], | ||
| 'castable' => [ | ||
| 'fixtureDir' => 'changes_equals_fixture_with_castable', | ||
| 'table' => 'test_model_with_castables', | ||
| 'modelClass' => TestModelWithCastable::class, | ||
| ], | ||
| 'cross attribute cast' => [ | ||
| 'fixtureDir' => 'changes_equals_fixture_with_cross_attribute_cast', | ||
| 'table' => 'test_model_with_cross_attribute_casts', | ||
| 'modelClass' => TestModelWithCrossAttributeCast::class, | ||
| ], | ||
| 'custom primary key' => [ | ||
| 'fixtureDir' => 'changes_equals_fixture_primary_key', | ||
| 'table' => 'test_model_non_id_primary_keys', | ||
| 'modelClass' => TestModelNonIdPrimaryKey::class, | ||
| 'uniqueKey' => 'name', | ||
| ], | ||
| ]; | ||
| } | ||
|
|
||
| public function testAssertChangesWithoutJsonFields() | ||
| { | ||
| $initialDatasetMock = collect( | ||
| value: $this->getJsonFixture('changes_equals_fixture_without_json_fields/initial_dataset.json'), | ||
| ); | ||
| $changedDatasetMock = collect( | ||
| value: $this->getJsonFixture('changes_equals_fixture_without_json_fields/changed_dataset.json'), | ||
| ); | ||
| #[DataProvider('getChangeScenarios')] | ||
| public function testAssertChanges( | ||
| string $fixtureDir, | ||
| string $table, | ||
| string $modelClass, | ||
| string $uniqueKey = 'id', | ||
| ): void { | ||
| $initialDatasetMock = collect($this->getJsonFixture("{$fixtureDir}/initial_dataset")); | ||
| $changedDatasetMock = collect($this->getJsonFixture("{$fixtureDir}/changed_dataset")); | ||
|
|
||
| $this->mockGettingDatasetForChanges($changedDatasetMock, $initialDatasetMock, 'test_model_without_json_fields'); | ||
| $this->mockGettingDatasetForChanges($changedDatasetMock, $initialDatasetMock, $table, $uniqueKey); | ||
|
|
||
| $modelTestState = new ModelTestState(TestModelWithoutJsonFields::class); | ||
| $modelTestState->assertChangesEqualsFixture('assertion_fixture_without_json_fields.json'); | ||
| $modelTestState = new ModelTestState($modelClass); | ||
| $modelTestState->assertChangesEqualsFixture('assertion_fixture'); | ||
| } | ||
|
|
||
| public function testAssertNoChanges() | ||
| public function testAssertNoChanges(): void | ||
| { | ||
| $datasetMock = collect($this->getJsonFixture('get_without_changes/dataset.json')); | ||
| $datasetMock = collect($this->getJsonFixture('get_without_changes/dataset')); | ||
|
|
||
| $this->mockGettingDatasetForChanges($datasetMock, $datasetMock, 'test_models'); | ||
|
|
||
| $modelTestState = new ModelTestState(TestModel::class); | ||
| $modelTestState->assertNotChanged(); | ||
| } | ||
|
|
||
| public function testAssertChangesWithCustomPrimaryKey() | ||
| { | ||
| $initialDatasetMock = collect($this->getJsonFixture('changes_equals_fixture_primary_key/initial_dataset')); | ||
| $changedDatasetMock = collect($this->getJsonFixture('changes_equals_fixture_primary_key/changed_dataset')); | ||
|
|
||
| $this->mockGettingDatasetForChanges($changedDatasetMock, $initialDatasetMock, 'test_model_non_id_primary_keys', 'name'); | ||
|
|
||
| $modelTestState = new ModelTestState(TestModelNonIdPrimaryKey::class); | ||
| $modelTestState->assertChangesEqualsFixture('assertion_fixture_primary_key'); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DenTray @yburlakov We can convert the state to a HashMap to improve lookup speed from O(n) to O(1). Should we include this in the current MR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vitgrams I think it makes sence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yburlakov Let me do this in the next MR as an optimization/refactoring