Skip to content

Update tests according changes in db #417

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

Merged
merged 3 commits into from
Jun 24, 2025
Merged
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
87 changes: 10 additions & 77 deletions tests/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeImmutable;
use DateTimeZone;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Constant\ColumnType;
use Yiisoft\Db\Expression\ArrayExpression;
use Yiisoft\Db\Expression\Expression;
Expand All @@ -19,7 +20,6 @@
use Yiisoft\Db\Pgsql\Column\ColumnBuilder;
use Yiisoft\Db\Pgsql\Column\IntegerColumn;
use Yiisoft\Db\Pgsql\Column\StructuredColumn;
use Yiisoft\Db\Pgsql\Connection;
use Yiisoft\Db\Pgsql\Tests\Provider\ColumnProvider;
use Yiisoft\Db\Pgsql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
Expand All @@ -30,6 +30,7 @@
use Yiisoft\Db\Tests\Common\CommonColumnTest;
use Yiisoft\Db\Tests\Support\Assert;

use function iterator_to_array;
use function str_repeat;
use function stream_get_contents;

Expand All @@ -42,7 +43,7 @@ final class ColumnTest extends CommonColumnTest

protected const COLUMN_BUILDER = ColumnBuilder::class;

private function insertTypeValues(Connection $db): void
protected function insertTypeValues(ConnectionInterface $db): void
{
$db->createCommand()->insert(
'type',
Expand All @@ -69,7 +70,7 @@ private function insertTypeValues(Connection $db): void
)->execute();
}

private function assertTypecastedValues(array $result): void
protected function assertTypecastedValues(array $result, bool $allTypecasted = false): void
{
$this->assertSame(1, $result['int_col']);
$this->assertSame(str_repeat('x', 100), $result['char_col']);
Expand All @@ -90,44 +91,6 @@ private function assertTypecastedValues(array $result): void
$this->assertSame([[[',', 'null', true, 'false', 'f']]], $result['jsonarray_col']);
}

public function testQueryWithTypecasting(): void
{
$db = $this->getConnection(true);

$this->insertTypeValues($db);

$query = (new Query($db))->from('type')->withTypecasting();

$result = $query->one();

$this->assertTypecastedValues($result);

$result = $query->all();

$this->assertTypecastedValues($result[0]);

$db->close();
}

public function testCommandWithPhpTypecasting(): void
{
$db = $this->getConnection(true);

$this->insertTypeValues($db);

$command = $db->createCommand('SELECT * FROM type')->withPhpTypecasting();

$result = $command->queryOne();

$this->assertTypecastedValues($result);

$result = $command->queryAll();

$this->assertTypecastedValues($result[0]);

$db->close();
}

public function testSelectWithPhpTypecasting(): void
{
$db = $this->getConnection(true);
Expand Down Expand Up @@ -173,6 +136,12 @@ public function testSelectWithPhpTypecasting(): void

$this->assertSame([$expected], $result);

$result = $db->createCommand($sql)
->withPhpTypecasting()
->query();

$this->assertSame([$expected], iterator_to_array($result));

$result = $db->createCommand('SELECT 2.5')
->withPhpTypecasting()
->queryScalar();
Expand All @@ -188,27 +157,6 @@ public function testSelectWithPhpTypecasting(): void
$db->close();
}

public function testPhpTypeCast(): void
{
$db = $this->getConnection(true);
$schema = $db->getSchema();
$columns = $schema->getTableSchema('type')->getColumns();

$this->insertTypeValues($db);

$query = (new Query($db))->from('type')->one();

$result = [];

foreach ($columns as $columnName => $column) {
$result[$columnName] = $column->phpTypecast($query[$columnName]);
}

$this->assertTypecastedValues($result);

$db->close();
}

public function testDbTypeCastJson(): void
{
$db = $this->getConnection(true);
Expand Down Expand Up @@ -271,21 +219,6 @@ public function testBoolDefault(): void
$db->close();
}

public function testNegativeDefaultValues()
{
$db = $this->getConnection(true);

$schema = $db->getSchema();
$tableSchema = $schema->getTableSchema('negative_default_values');

$this->assertSame(-123, $tableSchema->getColumn('tinyint_col')->getDefaultValue());
$this->assertSame(-123, $tableSchema->getColumn('smallint_col')->getDefaultValue());
$this->assertSame(-123, $tableSchema->getColumn('int_col')->getDefaultValue());
$this->assertSame(-123, $tableSchema->getColumn('bigint_col')->getDefaultValue());
$this->assertSame(-12345.6789, $tableSchema->getColumn('float_col')->getDefaultValue());
$this->assertSame(-33.22, $tableSchema->getColumn('numeric_col')->getDefaultValue());
}

public function testPrimaryKeyOfView()
{
$db = $this->getConnection(true);
Expand Down