Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration tests for jsonDecode & jsonEncode expressions #550

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
}
}
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
);
}
}
Loading