-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests for
jsonDecode
& jsonEncode
expressions
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/JsonDecodeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Integration\Row\Reference\Expression; | ||
|
||
use function Flow\ETL\DSL\lit; | ||
use function Flow\ETL\DSL\ref; | ||
use Flow\ETL\DSL\From; | ||
use Flow\ETL\DSL\To; | ||
use Flow\ETL\Flow; | ||
use Flow\ETL\Memory\ArrayMemory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class JsonDecodeTest extends TestCase | ||
{ | ||
public function test_add_json_string_into_existing_reference() : void | ||
{ | ||
(new Flow()) | ||
->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 | ||
); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/JsonEncodeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Integration\Row\Reference\Expression; | ||
|
||
use function Flow\ETL\DSL\lit; | ||
use function Flow\ETL\DSL\ref; | ||
use Flow\ETL\DSL\From; | ||
use Flow\ETL\DSL\To; | ||
use Flow\ETL\Flow; | ||
use Flow\ETL\Memory\ArrayMemory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class JsonEncodeTest extends TestCase | ||
{ | ||
public function test_adding_json_as_object_from_string_entry() : void | ||
{ | ||
(new Flow()) | ||
->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 | ||
); | ||
} | ||
} |