Skip to content

Commit

Permalink
Add integration tests for string related expressions (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Oct 9, 2023
1 parent 5de2aa3 commit 5acf960
Show file tree
Hide file tree
Showing 7 changed files with 577 additions and 0 deletions.
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

0 comments on commit 5acf960

Please sign in to comment.