Skip to content

Commit

Permalink
Improve and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Feb 20, 2025
1 parent 07ca473 commit dd625f9
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Schema/Data/AbstractLazyArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use ArrayAccess;
use Countable;
use InvalidArgumentException;
use JsonSerializable;
use IteratorAggregate;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Schema\Column\ColumnInterface;

use function array_map;
Expand Down Expand Up @@ -94,7 +94,7 @@ protected function prepareValue(): void
$value = $this->parse($this->value);

if ($value === null) {
throw new InvalidConfigException('Array value must be a valid string representation.');
throw new InvalidArgumentException('Array value must be a valid string representation.');
}

$this->value = $this->phpTypecast($value);
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/Data/AbstractLazyArrayStructured.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use ArrayAccess;
use Countable;
use InvalidArgumentException;
use JsonSerializable;
use IteratorAggregate;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Schema\Column\ColumnInterface;

use function is_string;
Expand Down Expand Up @@ -102,7 +102,7 @@ protected function prepareValue(): void
$value = $this->parse($this->value);

if ($value === null) {
throw new InvalidConfigException('Structured value must be a valid string representation.');
throw new InvalidArgumentException('Structured value must be a valid string representation.');
}

$this->value = $this->phpTypecast($value);
Expand Down
10 changes: 9 additions & 1 deletion src/Schema/Data/LazyArrayJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use ArrayAccess;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;
use JsonSerializable;

use function is_array;
use function is_string;
use function json_decode;

Expand Down Expand Up @@ -44,7 +46,13 @@ public function __construct(
protected function prepareValue(): void
{
if (is_string($this->value)) {
$this->value = json_decode($this->value, true, 512, JSON_THROW_ON_ERROR);
$value = json_decode($this->value, true, 512, JSON_THROW_ON_ERROR);

if (!is_array($value)) {
throw new InvalidArgumentException('JSON value must be a valid string array representation.');
}

$this->value = $value;
}
}
}
17 changes: 17 additions & 0 deletions tests/Db/Expression/ArrayExpressionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Yiisoft\Db\Expression\ArrayExpression;
use Yiisoft\Db\Expression\ArrayExpressionBuilder;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\Data\LazyArray;
use Yiisoft\Db\Schema\Data\LazyArrayInterface;
use Yiisoft\Db\Schema\Data\LazyArrayJson;
use Yiisoft\Db\Tests\Support\TestTrait;
Expand All @@ -28,8 +29,11 @@ public static function buildProvider(): array
return [
[[1, 2, 3], '[1,2,3]'],
[new ArrayIterator(['a', 'b', 'c']), '["a","b","c"]'],
[new LazyArray('[1,2,3]'), '[1,2,3]'],
[new LazyArrayJson('[1,2,3]'), '[1,2,3]'],
[['a' => 1, 'b' => null], '{"a":1,"b":null}'],
['[1,2,3]', '[1,2,3]'],
['{"a":1,"b":null}', '{"a":1,"b":null}'],
];
}

Expand All @@ -47,6 +51,19 @@ public function testBuild(iterable|LazyArrayInterface|string $value, string $exp
$this->assertEquals([':qp0' => new Param($expected, DataType::STRING)], $params);
}

public function testBuildNull(): void
{
$db = $this->getConnection();
$qb = $db->getQueryBuilder();

$params = [];
$builder = new ArrayExpressionBuilder($qb);
$expression = new ArrayExpression(null);

$this->assertSame('NULL', $builder->build($expression, $params));
$this->assertSame([], $params);
}

public function testBuildQueryExpression(): void
{
$db = $this->getConnection();
Expand Down
6 changes: 6 additions & 0 deletions tests/Db/Expression/JsonExpressionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use Yiisoft\Db\Expression\JsonExpression;
use Yiisoft\Db\Expression\JsonExpressionBuilder;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\Data\LazyArray;
use Yiisoft\Db\Schema\Data\LazyArrayJson;
use Yiisoft\Db\Schema\Data\LazyArrayStructured;
use Yiisoft\Db\Tests\Support\TestTrait;

/**
Expand All @@ -30,8 +32,12 @@ public static function buildProvider(): array
[true, 'true'],
[[1, 2, 3], '[1,2,3]'],
[new ArrayIterator(['a', 'b', 'c']), '["a","b","c"]'],
[new LazyArray('[1,2,3]'), '[1,2,3]'],
[new LazyArrayJson('[1,2,3]'), '[1,2,3]'],
[new LazyArrayStructured('["5","USD"]'), '["5","USD"]'],
[['a' => 1, 'b' => null, 'c' => ['d' => 'e']], '{"a":1,"b":null,"c":{"d":"e"}}'],
['[1,2,3]', '[1,2,3]'],
['{"a":1,"b":null,"c":{"d":"e"}}', '{"a":1,"b":null,"c":{"d":"e"}}'],
];
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Db/Expression/StructuredExpressionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Schema\Column\AbstractStructuredColumn;
use Yiisoft\Db\Schema\Column\ColumnBuilder;
use Yiisoft\Db\Schema\Data\LazyArrayJson;
use Yiisoft\Db\Schema\Data\LazyArrayStructured;
use Yiisoft\Db\Tests\Support\TestTrait;

Expand All @@ -35,8 +36,10 @@ public static function buildProvider(): array
[[5, 'USD'], null, '[5,"USD"]'],
[new ArrayIterator(['5', 'USD']), $column, '["5","USD"]'],
[new LazyArrayStructured('["5","USD"]'), $column, '["5","USD"]'],
[new LazyArrayJson('["5","USD"]'), $column, '["5","USD"]'],
[['value' => '5', 'currency_code' => 'USD'], $column, '["5","USD"]'],
[(object) ['value' => '5', 'currency_code' => 'USD'], 'currency_money', '["5","USD"]'],
['["5","USD"]', null, '["5","USD"]'],
];
}

Expand All @@ -54,6 +57,19 @@ public function testBuild(array|object|string $value, AbstractStructuredColumn|s
$this->assertEquals([':qp0' => new Param($expected, DataType::STRING)], $params);
}

public function testBuildNull(): void
{
$db = $this->getConnection();
$qb = $db->getQueryBuilder();

$params = [];
$builder = new StructuredExpressionBuilder($qb);
$expression = new StructuredExpression(null);

$this->assertSame('NULL', $builder->build($expression, $params));
$this->assertSame([], $params);
}

public function testBuildQueryExpression(): void
{
$db = $this->getConnection();
Expand Down
8 changes: 8 additions & 0 deletions tests/Db/Helper/DbArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Yiisoft\Db\Tests\Db\Helper;

use Closure;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Helper\DbArrayHelper;
use Yiisoft\Db\Tests\Provider\DbArrayHelperProvider;

/**
* @group db
Expand Down Expand Up @@ -101,4 +103,10 @@ public function testIndexWithClosureIndexByAndArrangeBy(): void
],
], DbArrayHelper::index($rows, fn ($row) => $row['key'], ['key']));
}

#[DataProviderExternal(DbArrayHelperProvider::class, 'toArray')]
public function testToArray(array|object $value, array $expected): void
{
$this->assertSame($expected, DbArrayHelper::toArray($value));
}
}
14 changes: 11 additions & 3 deletions tests/Db/Schema/Data/LazyArrayJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Yiisoft\Db\Tests\Db\Schema\Data;

use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Schema\Data\LazyArrayJson;
use Yiisoft\Db\Tests\Support\TestTrait;

use function iterator_to_array;

Expand All @@ -16,8 +16,6 @@
*/
final class LazyArrayJsonTest extends TestCase
{
use TestTrait;

public static function valueProvider(): array
{
return [
Expand Down Expand Up @@ -84,4 +82,14 @@ public function testIterator(string $value, array $expected): void

$this->assertSame($expected, iterator_to_array($lazyArray));
}

public function testNullValue()
{
$lazyArray = new LazyArrayJson('null');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('JSON value must be a valid string array representation.');

$lazyArray->getValue();
}
}
25 changes: 25 additions & 0 deletions tests/Db/Schema/Data/LazyArrayStructuredTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Tests\Db\Schema\Data;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Schema\Data\LazyArrayStructured;

/**
* @group db
*/
final class LazyArrayStructuredTest extends TestCase
{
public function testNullValue()
{
$lazyArray = new LazyArrayStructured('null');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Structured value must be a valid string representation.');

$lazyArray->getValue();
}
}
25 changes: 25 additions & 0 deletions tests/Db/Schema/Data/LazyArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Tests\Db\Schema\Data;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Schema\Data\LazyArray;

/**
* @group db
*/
final class LazyArrayTest extends TestCase
{
public function testNullValue()
{
$lazyArray = new LazyArray('null');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Array value must be a valid string representation.');

$lazyArray->getValue();
}
}
18 changes: 18 additions & 0 deletions tests/Provider/DbArrayHelperProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Yiisoft\Db\Tests\Provider;

use ArrayIterator;
use Yiisoft\Db\Schema\Data\LazyArrayJson;

class DbArrayHelperProvider
{
public static function index(): array
Expand Down Expand Up @@ -123,4 +126,19 @@ public static function indexWithIndexBy(): array
],
];
}

public static function toArray(): array
{
return [
[[], []],
[['key' => 'value'], ['key' => 'value']],
[(object) [], []],
[(object) ['key' => 'value'], ['key' => 'value']],
[new ArrayIterator([]), []],
[new ArrayIterator(['key' => 'value']), ['key' => 'value']],
[new LazyArrayJson('[]'), []],
[new LazyArrayJson('[1,2,3]'), [1, 2, 3]],
[new LazyArrayJson('{"key":"value"}'), ['key' => 'value']],
];
}
}

0 comments on commit dd625f9

Please sign in to comment.