diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 283976f..90513b3 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -6,7 +6,7 @@ jobs: test: runs-on: ubuntu-latest strategy: - fail-fast: true + fail-fast: false matrix: php: [8.0] laravel: [8.*] @@ -23,9 +23,7 @@ jobs: env: MYSQL_ALLOW_EMPTY_PASSWORD: yes MYSQL_DATABASE: laravel_schemaless_attributes - MYSQL_ROOT_PASSWORD: root_password - MYSQL_USER: username - MYSQL_PASSWORD: password + MYSQL_ROOT_PASSWORD: null ports: - 3306 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 @@ -38,7 +36,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} - extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl, exif, iconv coverage: none - name: Install dependencies diff --git a/src/Casts/SchemalessAttributes.php b/src/Casts/SchemalessAttributes.php index 162c721..8f483bc 100644 --- a/src/Casts/SchemalessAttributes.php +++ b/src/Casts/SchemalessAttributes.php @@ -32,6 +32,19 @@ public function get($model, $key, $value, $attributes) */ public function set($model, $key, $value, $attributes) { + if ($this->isJson($value)) { + return $value; + } + return json_encode($value); } + + protected function isJson($value): bool + { + if (! is_string($value)) { + return false; + } + + return $value === json_encode(json_decode($value)); + } } diff --git a/tests/HasSchemalessAttributesTest.php b/tests/HasSchemalessAttributesTest.php index 4791fbe..557d801 100644 --- a/tests/HasSchemalessAttributesTest.php +++ b/tests/HasSchemalessAttributesTest.php @@ -41,6 +41,14 @@ public function an_schemaless_attribute_can_be_set() $this->assertEquals('value', $this->testModel->schemaless_attributes->name); } + /** @test */ + public function an_schemaless_attribute_can_be_set_from_json() + { + $this->testModel->schemaless_attributes = json_encode(['name' => 'value']); + + $this->assertEquals('value', $this->testModel->schemaless_attributes->name); + } + /** @test */ public function it_can_determine_if_it_has_a_schemaless_attribute() { @@ -245,6 +253,19 @@ public function it_can_add_and_save_schemaless_attributes_in_one_go() $this->assertEquals($array, $testModel->schemaless_attributes->all()); } + /** @test */ + public function it_can_and_save_schemaless_attributes_from_json() + { + $array = [ + 'name' => 'value', + 'name2' => 'value2', + ]; + + $testModel = TestModel::create(['schemaless_attributes' => json_encode($array)]); + + $this->assertEquals($array, $testModel->schemaless_attributes->all()); + } + /** @test */ public function it_has_a_scope_to_get_models_with_the_given_schemaless_attributes() { diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml index 1e852a6..84f6bed 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -4,10 +4,8 @@ services: restart: always image: mysql/mysql-server:8.0 environment: - MYSQL_ROOT_PASSWORD: "root_password" + MYSQL_ROOT_PASSWORD: null MYSQL_DATABASE: "laravel_schemaless_attributes" - MYSQL_USER: "username" - MYSQL_PASSWORD: "password" MYSQL_ROOT_HOST: "0.0.0.0" MYSQL_ALLOW_EMPTY_PASSWORD: "yes" ports: