From d18205ef9e0b44510b4b1091d3061e6949129c1b Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Mon, 9 Oct 2023 12:36:30 +0200 Subject: [PATCH] Add integration tests for `jsonDecode` & `jsonEncode` expressions --- .../Reference/Expression/JsonDecodeTest.php | 41 ++++++++++++ .../Reference/Expression/JsonEncodeTest.php | 66 +++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/JsonDecodeTest.php create mode 100644 src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/JsonEncodeTest.php diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/JsonDecodeTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/JsonDecodeTest.php new file mode 100644 index 000000000..e81638e76 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/JsonDecodeTest.php @@ -0,0 +1,41 @@ +read( + From::array( + [['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]]], + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->drop('row') + ->withEntry('json', lit('{"d": 4}')) + ->withEntry('array', ref('array')->arrayMerge(ref('json')->jsonDecode())) + ->drop('json') + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/JsonEncodeTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/JsonEncodeTest.php new file mode 100644 index 000000000..f2f88ad37 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/JsonEncodeTest.php @@ -0,0 +1,66 @@ +read( + From::array([['id' => 1]]) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->drop('row') + ->withEntry('json', lit(['id' => 1, 'name' => 'test'])) + ->withEntry('json', ref('json')->jsonEncode(\JSON_FORCE_OBJECT)) + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + [ + 'id' => 1, + 'json' => '{"id":1,"name":"test"}', + ], + ], + $memory->data + ); + } + + public function test_adding_json_from_string_entry() : void + { + (new Flow()) + ->read( + From::array([['id' => 1]]) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->drop('row') + ->withEntry('json', lit([1, 2, 3])) + ->withEntry('json', ref('json')->jsonEncode()) + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + [ + 'id' => 1, + 'json' => '[1,2,3]', + ], + ], + $memory->data + ); + } +}