From 8cf7d8e7e3f8370da672fdba3e2f8f0701214e5c Mon Sep 17 00:00:00 2001 From: Erik Niebla Date: Wed, 27 Oct 2021 18:04:07 -0500 Subject: [PATCH 1/2] Support json string on cast setter --- .github/workflows/run-tests.yml | 6 +++--- src/Casts/SchemalessAttributes.php | 13 +++++++++++++ tests/HasSchemalessAttributesTest.php | 21 +++++++++++++++++++++ tests/docker-compose.yml | 4 ++-- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 283976f..bd19a16 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,9 @@ jobs: env: MYSQL_ALLOW_EMPTY_PASSWORD: yes MYSQL_DATABASE: laravel_schemaless_attributes - MYSQL_ROOT_PASSWORD: root_password + MYSQL_ROOT_PASSWORD: null MYSQL_USER: username - MYSQL_PASSWORD: password + MYSQL_PASSWORD: null ports: - 3306 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 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..8895367 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -4,10 +4,10 @@ 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_PASSWORD: null MYSQL_ROOT_HOST: "0.0.0.0" MYSQL_ALLOW_EMPTY_PASSWORD: "yes" ports: From c01e504542335c51291e2661609191d498de7913 Mon Sep 17 00:00:00 2001 From: Erik Niebla Date: Wed, 3 Nov 2021 12:34:32 -0500 Subject: [PATCH 2/2] Github test fixes --- .github/workflows/run-tests.yml | 4 +--- tests/docker-compose.yml | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index bd19a16..90513b3 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -24,8 +24,6 @@ jobs: MYSQL_ALLOW_EMPTY_PASSWORD: yes MYSQL_DATABASE: laravel_schemaless_attributes MYSQL_ROOT_PASSWORD: null - MYSQL_USER: username - MYSQL_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/tests/docker-compose.yml b/tests/docker-compose.yml index 8895367..84f6bed 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -6,8 +6,6 @@ services: environment: MYSQL_ROOT_PASSWORD: null MYSQL_DATABASE: "laravel_schemaless_attributes" - MYSQL_USER: "username" - MYSQL_PASSWORD: null MYSQL_ROOT_HOST: "0.0.0.0" MYSQL_ALLOW_EMPTY_PASSWORD: "yes" ports: