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 string related expressions #552

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,85 @@
<?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\Exception\RuntimeException;
use Flow\ETL\Flow;
use Flow\ETL\Memory\ArrayMemory;
use PHPUnit\Framework\TestCase;

final class SizeTest extends TestCase
{
public function test_size_on_array() : void
{
(new Flow())
->read(
From::array(
[
['array' => [1, 2, 3]],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('size', ref('array')->size())
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['array' => [1, 2, 3], 'size' => 3],
],
$memory->data
);
}

public function test_size_on_non_string_key() : void
{
$this->expectException(RuntimeException::class);

(new Flow())
->read(
From::array(
[
['id' => 1],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('size', ref('id')->size())
->run();
}

public function test_size_on_string() : void
{
(new Flow())
->read(
From::array(
[
['key' => 'value'],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('size', ref('key')->size())
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['key' => 'value', 'size' => 5],
],
$memory->data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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 SprintfTest extends TestCase
{
public function test_sprintf() : void
{
(new Flow())
->read(
From::array(
[
['key' => 'test %s'],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('sprintf', ref('key')->sprintf(lit('value')))
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['key' => 'test %s', 'sprintf' => 'test value'],
],
$memory->data
);
}

public function test_sprintf_on_non_string_key() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('sprintf', ref('id')->sprintf(lit('1')))
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'sprintf' => null],
],
$memory->data
);
}

public function test_sprintf_on_null_value() : void
{
(new Flow())
->read(
From::array(
[
['id' => '1'],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('sprintf', ref('id')->sprintf(lit(null)))
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => '1', 'sprintf' => null],
],
$memory->data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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 StrPadTest extends TestCase
{
public function test_strpad() : void
{
(new Flow())
->read(
From::array(
[
['key' => 'value'],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('strpad', ref('key')->strPad(10, '*'))
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['key' => 'value', 'strpad' => 'value*****'],
],
$memory->data
);
}

public function test_strpad_on_non_string_key() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('strpad', ref('id')->strPad(10))
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'strpad' => null],
],
$memory->data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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 StrReplaceTest extends TestCase
{
public function test_str_replace() : void
{
(new Flow())
->read(
From::array(
[
['key' => 'value'],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('str_replace', ref('key')->strReplace('e', 'es'))
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['key' => 'value', 'str_replace' => 'values'],
],
$memory->data
);
}

public function test_str_replace_on_non_string_key() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('str_replace', ref('id')->strReplace('', ''))
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'str_replace' => null],
],
$memory->data
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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 ToLowerTest extends TestCase
{
public function test_to_lower() : void
{
(new Flow())
->read(
From::array(
[
['key' => 'VALUE'],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('to_lower', ref('key')->lower())
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['key' => 'VALUE', 'to_lower' => 'value'],
],
$memory->data
);
}

public function test_to_lower_on_non_string_key() : void
{
(new Flow())
->read(
From::array(
[
['id' => 1],
]
)
)
->withEntry('row', ref('row')->unpack())
->renameAll('row.', '')
->drop('row')
->withEntry('to_lower', ref('id')->lower())
->write(To::memory($memory = new ArrayMemory()))
->run();

$this->assertSame(
[
['id' => 1, 'to_lower' => 1],
],
$memory->data
);
}
}
Loading
Loading