diff --git a/src/Testing/TableTestState.php b/src/Testing/TableTestState.php index c1c6c196..7dc5ed51 100644 --- a/src/Testing/TableTestState.php +++ b/src/Testing/TableTestState.php @@ -64,6 +64,8 @@ protected function getChanges(): array $changes = array_diff_assoc($updatedItem, $originItem); if (!empty($changes)) { + $changes = Arr::map($changes, fn ($field) => (!mb_check_encoding($field, 'UTF-8')) ? bin2hex($field) : $field); + $updatedRecords[] = array_merge(['id' => $originItem['id']], $changes); } @@ -88,7 +90,7 @@ protected function prepareChanges(array $changes): array return array_map(function ($item) use ($jsonFields) { foreach ($jsonFields as $jsonField) { - if (Arr::has($item, $jsonField)) { + if (Arr::has($item, $jsonField) && is_string($item[$jsonField]) && json_validate($item[$jsonField])) { $item[$jsonField] = json_decode($item[$jsonField], true); } } diff --git a/tests/BaseRequestTest.php b/tests/BaseRequestTest.php index d96ba904..b5657d53 100644 --- a/tests/BaseRequestTest.php +++ b/tests/BaseRequestTest.php @@ -14,7 +14,7 @@ public function testGetOrderableFields() { $result = $this->callEncapsulatedMethod(new BaseRequest(), 'getOrderableFields', TestModel::class); - $expectedResult = 'id,name,json_field,castable_field,*,created_at,updated_at'; + $expectedResult = 'id,name,json_field,castable_field,binary_field,*,created_at,updated_at'; $this->assertEquals($expectedResult, $result); } @@ -28,7 +28,7 @@ public function testGetOrderableFieldsWithAdditionalFields() $result = $this->callEncapsulatedMethod(new BaseRequest(), 'getOrderableFields', ...$args); - $expectedResult = 'id,name,json_field,castable_field,*,created_at,updated_at,additional_field_1,additional_field_2'; + $expectedResult = 'id,name,json_field,castable_field,binary_field,*,created_at,updated_at,additional_field_1,additional_field_2'; $this->assertEquals($expectedResult, $result); } diff --git a/tests/ModelTestStateTest.php b/tests/ModelTestStateTest.php index a865e9d3..ac4a2ce3 100644 --- a/tests/ModelTestStateTest.php +++ b/tests/ModelTestStateTest.php @@ -35,7 +35,7 @@ public function testInitialization() $jsonFields = $this->getProtectedProperty($reflectionClass, 'jsonFields', $modelTestState); $state = $this->getProtectedProperty($reflectionClass, 'state', $modelTestState); - $this->assertEquals(['json_field', 'castable_field'], $jsonFields); + $this->assertEquals(['json_field', 'castable_field', 'binary_field'], $jsonFields); $this->assertEquals($originRecords, $state); } @@ -88,6 +88,42 @@ public function testAssertChangesWithoutJsonFields() $modelTestState->assertChangesEqualsFixture('assertion_fixture_without_json_fields.json'); } + public function testAssertChangesBinaryString() + { + $initialDatasetMock = collect([[ + 'id' => 1, + 'binary_field' => null, + ]]); + + $changedDatasetMock = collect([[ + 'id' => 1, + 'binary_field' => md5('some_string', true), + ]]); + + $this->mockGettingDatasetForChanges($changedDatasetMock, $initialDatasetMock, 'test_models'); + + $modelTestState = new ModelTestState(TestModel::class); + $modelTestState->assertChangesEqualsFixture('null_to_binary_string_changes'); + } + + public function testAssertChangesBinaryToNull() + { + $initialDatasetMock = collect([[ + 'id' => 1, + 'binary_field' => md5('some_string', true), + ]]); + + $changedDatasetMock = collect([[ + 'id' => 1, + 'binary_field' => null, + ]]); + + $this->mockGettingDatasetForChanges($changedDatasetMock, $initialDatasetMock, 'test_models'); + + $modelTestState = new ModelTestState(TestModel::class); + $modelTestState->assertChangesEqualsFixture('binary_string_to_null_changes'); + } + public function testAssertNoChanges() { $datasetMock = collect($this->getJsonFixture('get_without_changes/dataset.json')); diff --git a/tests/fixtures/ModelTestStateTest/db_changes/test_models/binary_string_to_null_changes.json b/tests/fixtures/ModelTestStateTest/db_changes/test_models/binary_string_to_null_changes.json new file mode 100644 index 00000000..2b8b3bac --- /dev/null +++ b/tests/fixtures/ModelTestStateTest/db_changes/test_models/binary_string_to_null_changes.json @@ -0,0 +1,10 @@ +{ + "updated": [ + { + "id": 1, + "binary_field": null + } + ], + "created": [], + "deleted": [] +} \ No newline at end of file diff --git a/tests/fixtures/ModelTestStateTest/db_changes/test_models/null_to_binary_string_changes.json b/tests/fixtures/ModelTestStateTest/db_changes/test_models/null_to_binary_string_changes.json new file mode 100644 index 00000000..b8ec10d1 --- /dev/null +++ b/tests/fixtures/ModelTestStateTest/db_changes/test_models/null_to_binary_string_changes.json @@ -0,0 +1,10 @@ +{ + "updated": [ + { + "id": 1, + "binary_field": "31ee76261d87fed8cb9d4c465c48158c" + } + ], + "created": [], + "deleted": [] +} \ No newline at end of file diff --git a/tests/support/Mock/Casts/BinaryCast.php b/tests/support/Mock/Casts/BinaryCast.php new file mode 100644 index 00000000..a8113bba --- /dev/null +++ b/tests/support/Mock/Casts/BinaryCast.php @@ -0,0 +1,18 @@ + 'array', 'castable_field' => JSONCustomCast::class, + 'binary_field' => BinaryCast::class, ]; public function relation(): HasMany