Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Sep 1, 2023
1 parent 79c1264 commit f14c094
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
37 changes: 16 additions & 21 deletions tests/ColumnSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
namespace Yiisoft\Db\Mssql\Tests;

use PDO;
use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Mssql\BinaryColumnSchema;
use Yiisoft\Db\Mssql\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;

use Yiisoft\Db\Schema\Column\DoubleColumnSchema;
use Yiisoft\Db\Schema\Column\IntegerColumnSchema;
use Yiisoft\Db\Schema\Column\StringColumnSchema;
use Yiisoft\Db\Schema\SchemaInterface;
use function fopen;
use Yiisoft\Db\Tests\Common\CommonColumnSchemaTest;

use function str_repeat;

/**
* @group mssql
*/
final class ColumnSchemaTest extends TestCase
final class ColumnSchemaTest extends CommonColumnSchemaTest
{
use TestTrait;

Expand Down Expand Up @@ -82,27 +80,24 @@ public function testColumnSchemaInstance()
$this->assertInstanceOf(BinaryColumnSchema::class, $tableSchema->getColumn('blob_col'));
}

/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnSchemaProvider::predefinedTypes */
public function testPredefinedType(string $className, string $type, string $phpType)
{
parent::testPredefinedType($className, $type, $phpType);
}

/** @dataProvider \Yiisoft\Db\Mssql\Tests\Provider\ColumnSchemaProvider::dbTypecastColumns */
public function testDbTypecastColumns(string $className, array $values)
{
parent::testDbTypecastColumns($className, $values);
}

public function testBinaryColumnSchema()
{
$binaryCol = new BinaryColumnSchema('binary_col');
$binaryCol->dbType('varbinary');

$this->assertSame('binary_col', $binaryCol->getName());
$this->assertSame(SchemaInterface::TYPE_BINARY, $binaryCol->getType());
$this->assertSame(SchemaInterface::PHP_TYPE_RESOURCE, $binaryCol->getPhpType());

$this->assertNull($binaryCol->dbTypecast(null));
$this->assertSame('1', $binaryCol->dbTypecast(1));
$this->assertSame('1', $binaryCol->dbTypecast(true));
$this->assertSame('0', $binaryCol->dbTypecast(false));
$this->assertSame($resource = fopen('php://memory', 'rb'), $binaryCol->dbTypecast($resource));
$this->assertEquals(new Param("\x10\x11\x12", PDO::PARAM_LOB), $binaryCol->dbTypecast("\x10\x11\x12"));
$this->assertSame($expression = new Expression('expression'), $binaryCol->dbTypecast($expression));

$this->assertNull($binaryCol->phpTypecast(null));
$this->assertSame("\x10\x11\x12", $binaryCol->phpTypecast("\x10\x11\x12"));
$this->assertSame($resource = fopen('php://memory', 'rb'), $binaryCol->phpTypecast($resource));

$binaryCol->dbType('varbinary');
$this->assertEquals(
new Expression('CONVERT(VARBINARY(MAX), 0x101112)'),
$binaryCol->dbTypecast("\x10\x11\x12"),
Expand Down
26 changes: 26 additions & 0 deletions tests/Provider/ColumnSchemaProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Mssql\Tests\Provider;

use Yiisoft\Db\Mssql\BinaryColumnSchema;

class ColumnSchemaProvider extends \Yiisoft\Db\Tests\Provider\ColumnSchemaProvider
{
public static function predefinedTypes(): array
{
$values = parent::predefinedTypes();
$values['binary'][0] = BinaryColumnSchema::class;

return $values;
}

public static function dbTypecastColumns(): array
{
$values = parent::dbTypecastColumns();
$values['binary'][0] = BinaryColumnSchema::class;

return $values;
}
}

0 comments on commit f14c094

Please sign in to comment.