77use RonasIT \Support \Testing \ModelTestState ;
88use RonasIT \Support \Tests \Support \Mock \Models \TestModel ;
99use RonasIT \Support \Tests \Support \Mock \Models \TestModelNonIdPrimaryKey ;
10- use RonasIT \Support \Tests \Support \Mock \Models \TestModelWithoutJsonFields ;
10+ use RonasIT \Support \Tests \Support \Mock \Models \TestModelWithCastInstance ;
11+ use RonasIT \Support \Tests \Support \Mock \Models \TestModelWithCustomJsonCast ;
12+ use RonasIT \Support \Tests \Support \Mock \Models \TestModelWithCustomNonJsonCast ;
13+ use RonasIT \Support \Tests \Support \Mock \Models \TestModelWithNativeJsonCasts ;
14+ use RonasIT \Support \Tests \Support \Mock \Models \TestModelWithPrimitiveCasts ;
1115use RonasIT \Support \Tests \Support \Traits \TableTestStateMockTrait ;
1216
1317class ModelTestStateTest extends TestCase
@@ -23,20 +27,23 @@ public function setUp(): void
2327 putenv ('FAIL_EXPORT_JSON=false ' );
2428 }
2529
26- public function testInitialization ()
30+ public function testInitialization (): void
2731 {
28- $ datasetMock = collect ($ this ->getJsonFixture ('initialization/dataset.json ' ));
29- $ originRecords = collect ($ this ->getJsonFixture ('initialization/origin_records.json ' ));
32+ $ datasetMock = collect ($ this ->getJsonFixture ('initialization/dataset ' ));
33+ $ originRecords = collect ($ this ->getJsonFixture ('initialization/origin_records ' ));
3034
3135 $ this ->mockGettingDataset ($ datasetMock );
3236
3337 $ modelTestState = new ModelTestState (TestModel::class);
3438 $ reflectionClass = new ReflectionClass ($ modelTestState );
3539
3640 $ jsonFields = $ this ->getProtectedProperty ($ reflectionClass , 'jsonFields ' , $ modelTestState );
41+ $ classCastFields = $ this ->getProtectedProperty ($ reflectionClass , 'classCastFields ' , $ modelTestState );
3742 $ state = $ this ->getProtectedProperty ($ reflectionClass , 'state ' , $ modelTestState );
3843
39- $ this ->assertEquals (['json_field ' , 'castable_field ' ], $ jsonFields );
44+ $ this ->assertEquals (['json_field ' ], $ jsonFields );
45+ $ this ->assertEquals (['custom_cast_field ' , 'castable_field ' ], $ classCastFields );
46+
4047 $ this ->assertEquals ($ originRecords , $ state );
4148 }
4249
@@ -53,60 +60,81 @@ public static function getInitializationViaPrepareModelTestStateFilters(): array
5360 }
5461
5562 #[DataProvider('getInitializationViaPrepareModelTestStateFilters ' )]
56- public function testInitializationViaPrepareTableTestState (bool $ testCaseGlobalExportMode )
63+ public function testInitializationViaPrepareTableTestState (bool $ testCaseGlobalExportMode ): void
5764 {
58- $ datasetMock = collect ($ this ->getJsonFixture ('initialization/dataset.json ' ));
65+ $ datasetMock = collect ($ this ->getJsonFixture ('initialization/dataset ' ));
5966 $ this ->mockGettingDataset ($ datasetMock );
6067
6168 $ actualGlobalExportModeValue = $ this ->mockTestStateCreationSetGlobalExportMode ('prepareModelTestState ' , TestModel::class, $ testCaseGlobalExportMode );
6269
6370 $ this ->assertEquals ($ actualGlobalExportModeValue , $ testCaseGlobalExportMode );
6471 }
6572
66- public function testAssertChangesEqualsFixture ()
73+ public static function getAssertChangesData (): array
6774 {
68- $ initialDatasetMock = collect ($ this ->getJsonFixture ('changes_equals_fixture/initial_dataset.json ' ));
69- $ changedDatasetMock = collect ($ this ->getJsonFixture ('changes_equals_fixture/changed_dataset.json ' ));
70-
71- $ this ->mockGettingDatasetForChanges ($ changedDatasetMock , $ initialDatasetMock , 'test_models ' );
72-
73- $ modelTestState = new ModelTestState (TestModel::class);
74- $ modelTestState ->assertChangesEqualsFixture ('assertion_fixture.json ' );
75+ return [
76+ 'mixed native, custom and castable casts ' => [
77+ 'fixtureDir ' => 'changes_equals_fixture ' ,
78+ 'table ' => 'test_models ' ,
79+ 'modelClass ' => TestModel::class,
80+ ],
81+ 'cast defined as an instance ' => [
82+ 'fixtureDir ' => 'changes_equals_fixture_with_cast_instance ' ,
83+ 'table ' => 'test_model_with_cast_instances ' ,
84+ 'modelClass ' => TestModelWithCastInstance::class,
85+ ],
86+ 'primitive casts ' => [
87+ 'fixtureDir ' => 'changes_equals_fixture_with_primitive_casts ' ,
88+ 'table ' => 'test_model_with_primitive_casts ' ,
89+ 'modelClass ' => TestModelWithPrimitiveCasts::class,
90+ ],
91+ 'native json casts ' => [
92+ 'fixtureDir ' => 'changes_equals_fixture_with_native_json_casts ' ,
93+ 'table ' => 'test_model_with_native_json_casts ' ,
94+ 'modelClass ' => TestModelWithNativeJsonCasts::class,
95+ ],
96+ 'custom json cast ' => [
97+ 'fixtureDir ' => 'changes_equals_fixture_with_custom_json_cast ' ,
98+ 'table ' => 'test_model_with_custom_json_casts ' ,
99+ 'modelClass ' => TestModelWithCustomJsonCast::class,
100+ ],
101+ 'custom non-json cast ' => [
102+ 'fixtureDir ' => 'changes_equals_fixture_with_custom_non_json_cast ' ,
103+ 'table ' => 'test_model_with_custom_non_json_casts ' ,
104+ 'modelClass ' => TestModelWithCustomNonJsonCast::class,
105+ ],
106+ 'custom primary key ' => [
107+ 'fixtureDir ' => 'changes_equals_fixture_primary_key ' ,
108+ 'table ' => 'test_model_non_id_primary_keys ' ,
109+ 'modelClass ' => TestModelNonIdPrimaryKey::class,
110+ 'uniqueKey ' => 'name ' ,
111+ ],
112+ ];
75113 }
76114
77- public function testAssertChangesWithoutJsonFields ()
78- {
79- $ initialDatasetMock = collect (
80- value: $ this ->getJsonFixture ('changes_equals_fixture_without_json_fields/initial_dataset.json ' ),
81- );
82- $ changedDatasetMock = collect (
83- value: $ this ->getJsonFixture ('changes_equals_fixture_without_json_fields/changed_dataset.json ' ),
84- );
115+ #[DataProvider('getAssertChangesData ' )]
116+ public function testAssertChanges (
117+ string $ fixtureDir ,
118+ string $ table ,
119+ string $ modelClass ,
120+ string $ uniqueKey = 'id ' ,
121+ ): void {
122+ $ initialDatasetMock = collect ($ this ->getJsonFixture ("{$ fixtureDir }/initial_dataset " ));
123+ $ changedDatasetMock = collect ($ this ->getJsonFixture ("{$ fixtureDir }/changed_dataset " ));
85124
86- $ this ->mockGettingDatasetForChanges ($ changedDatasetMock , $ initialDatasetMock , ' test_model_without_json_fields ' );
125+ $ this ->mockGettingDatasetForChanges ($ changedDatasetMock , $ initialDatasetMock , $ table , $ uniqueKey );
87126
88- $ modelTestState = new ModelTestState (TestModelWithoutJsonFields::class );
89- $ modelTestState ->assertChangesEqualsFixture ('assertion_fixture_without_json_fields.json ' );
127+ $ modelTestState = new ModelTestState ($ modelClass );
128+ $ modelTestState ->assertChangesEqualsFixture ('assertion_fixture ' );
90129 }
91130
92- public function testAssertNoChanges ()
131+ public function testAssertNoChanges (): void
93132 {
94- $ datasetMock = collect ($ this ->getJsonFixture ('get_without_changes/dataset.json ' ));
133+ $ datasetMock = collect ($ this ->getJsonFixture ('get_without_changes/dataset ' ));
95134
96135 $ this ->mockGettingDatasetForChanges ($ datasetMock , $ datasetMock , 'test_models ' );
97136
98137 $ modelTestState = new ModelTestState (TestModel::class);
99138 $ modelTestState ->assertNotChanged ();
100139 }
101-
102- public function testAssertChangesWithCustomPrimaryKey ()
103- {
104- $ initialDatasetMock = collect ($ this ->getJsonFixture ('changes_equals_fixture_primary_key/initial_dataset ' ));
105- $ changedDatasetMock = collect ($ this ->getJsonFixture ('changes_equals_fixture_primary_key/changed_dataset ' ));
106-
107- $ this ->mockGettingDatasetForChanges ($ changedDatasetMock , $ initialDatasetMock , 'test_model_non_id_primary_keys ' , 'name ' );
108-
109- $ modelTestState = new ModelTestState (TestModelNonIdPrimaryKey::class);
110- $ modelTestState ->assertChangesEqualsFixture ('assertion_fixture_primary_key ' );
111- }
112140}
0 commit comments