Skip to content

Commit

Permalink
Add integration tests for array related expression (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Oct 8, 2023
1 parent ff197ab commit 951bf41
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ function array_sort(Expression $expression, string $function = null, int $flags
return new Expression\ArraySort($expression, $function ? Sort::fromString($function) : Sort::sort, $flags, $recursive);
}

function array_reverse(Expression $expression, bool $preserveKeys = false) : Expression
{
return new Expression\ArrayReverse($expression, $preserveKeys);
}

function now(\DateTimeZone $time_zone = new \DateTimeZone('UTC')) : Expression
{
return new Expression\Now($time_zone);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Integration\Row\Reference\Expression;

use function Flow\ETL\DSL\array_exists;
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 ArrayExistsTest extends TestCase
{
public function test_array_exists() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]],
['id' => 2],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->withEntry('has_array', array_exists(ref('array'), 'a'))
->drop('row', 'array')
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'has_array' => true],
['id' => 2, 'has_array' => false],
],
$memory->data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Integration\Row\Reference\Expression;

use function Flow\ETL\DSL\array_get_collection;
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 ArrayGetCollectionTest extends TestCase
{
public function test_array_get_collection() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1, 'array' => [
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 1, 'b' => 2, 'c' => 3],
['a' => 1, 'b' => 2, 'c' => 3],
]],
['id' => 2],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->withEntry('result', array_get_collection(ref('array'), 'a', 'c'))
->drop('row', 'array')
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'result' => [['a' => 1, 'c' => 3], ['a' => 1, 'c' => 3], ['a' => 1, 'c' => 3]]],
['id' => 2, 'result' => null],
],
$memory->data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Integration\Row\Reference\Expression;

use function Flow\ETL\DSL\array_get;
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 ArrayGetTest extends TestCase
{
public function test_array_get() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]],
['id' => 2],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->withEntry('result', array_get(ref('array'), 'b'))
->drop('row', 'array')
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'result' => 2],
['id' => 2, 'result' => null],
],
$memory->data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Integration\Row\Reference\Expression;

use function Flow\ETL\DSL\array_key_rename;
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 ArrayKeyRenameTest extends TestCase
{
public function test_array_key_rename() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]],
['id' => 2],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->withEntry('array', array_key_rename(ref('array'), 'a', 'd'))
->drop('row')
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'array' => ['b' => 2, 'c' => 3, 'd' => 1]],
['id' => 2, 'array' => null],
],
$memory->data
);
}
}
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\array_keys_style_convert;
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 ArrayKeysStyleConvertTest extends TestCase
{
public function test_array_keys_style_convert() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1, 'array' => ['camelCased' => 1, 'snake_cased' => 2, 'space word' => 3]],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->withEntry('array', array_keys_style_convert(ref('array'), 'kebab'))
->drop('row')
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'array' => ['camel-cased' => 1, 'snake-cased' => 2, 'space-word' => 3]],
],
$memory->data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Integration\Row\Reference\Expression;

use function Flow\ETL\DSL\array_merge_collection;
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 ArrayMergeCollectionTest extends TestCase
{
public function test_array_merge_collection() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1, 'array' => [
['a' => 1],
['b' => 2],
['c' => 3],
]],
['id' => 2],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->withEntry('result', array_merge_collection(ref('array')))
->drop('row', 'array')
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'result' => ['a' => 1, 'b' => 2, 'c' => 3]],
['id' => 2, 'result' => null],
],
$memory->data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Integration\Row\Reference\Expression;

use function Flow\ETL\DSL\array_merge;
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 ArrayMergeTest extends TestCase
{
public function test_array_merge() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1, 'first' => ['a' => 1, 'b' => 2], 'second' => ['c' => 3]],
['id' => 2],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->withEntry('array', array_merge(ref('first'), ref('second')))
->drop('row', 'first', 'second')
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]],
['id' => 2, 'array' => null],
],
$memory->data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Integration\Row\Reference\Expression;

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 ArrayReverseTest extends TestCase
{
public function test_array_reverse() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]],
['id' => 2],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('array', ref('array')->arrayReverse())
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'array' => ['c' => 3, 'b' => 2, 'a' => 1]],
['id' => 2, 'array' => null],
],
$memory->data
);
}
}
Loading

0 comments on commit 951bf41

Please sign in to comment.