Skip to content

Commit

Permalink
Fix #722: Remove legacy array syntax for typecast. Use Param instead
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Jun 20, 2023
1 parent 71762f3 commit e129d5e
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 118 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## 1.0.1 under development

- no changes in this release.
- Chg #722: Remove legacy array syntax for typecast. Use `Param` instead (@terabytesoftw)

## 1.0.0 April 12, 2023

- Initial release.
- Initial release.
157 changes: 83 additions & 74 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,76 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" executionOrder="default" failOnRisky="true" failOnWarning="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd">
<coverage>
<include>
<directory>./src</directory>
</include>
<exclude>
<directory>./src/TestSupport</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
<file>./src/Command/CommandInterface.php</file>
<file>./src/Command/ParamInterface.php</file>
<file>./src/Connection/ConnectionInterface.php</file>
<file>./src/Connection/ConnectionPoolInterface.php</file>
<file>./src/Constraint/ConstraintSchemaInterface.php</file>
<file>./src/Driver/DriverInterface.php</file>
<file>./src/Driver/PDO/CommandPDOInterface.php</file>
<file>./src/Driver/PDO/ConnectionPDOInterface.php</file>
<file>./src/Driver/PDO/ConnectionPDOPoolInterface.php</file>
<file>./src/Driver/PDO/PDODriverInterface.php</file>
<file>./src/Expression/ExpressionBuilderInterface.php</file>
<file>./src/Expression/ExpressionInterface.php</file>
<file>./src/Profiler/ProfilerInterface.php</file>
<file>./src/Query/BatchQueryResultInterface.php</file>
<file>./src/Query/Data/DataReaderInterface.php</file>
<file>./src/Query/QueryFunctionsInterface.php</file>
<file>./src/Query/QueryInterface.php</file>
<file>./src/Query/QueryPartsInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/BetweenColumnsConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/BetweenConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/ConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/ConjunctionConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/ExistConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/HashConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/InConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/LikeConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/NotConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/SimpleConditionInterface.php</file>
<file>./src/QueryBuilder/DDLQueryBuilderInterface.php</file>
<file>./src/QueryBuilder/DMLQueryBuilderInterface.php</file>
<file>./src/QueryBuilder/DQLQueryBuilderInterface.php</file>
<file>./src/QueryBuilder/QueryBuilderInterface.php</file>
<file>./src/Schema/ColumnSchemaInterface.php</file>
<file>./src/Schema/QuoterInterface.php</file>
<file>./src/Schema/SchemaInterface.php</file>
<file>./src/Schema/TableSchemaInterface.php</file>
<file>./src/Transaction/TransactionInterface.php</file>
</exclude>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="Db">
<directory>./tests/Db</directory>
</testsuite>
<testsuite name="ActiveRecord">
<directory>./vendor/yiisoft/active-record/tests/Driver</directory>
</testsuite>
<testsuite name="Mssql">
<directory>./vendor/yiisoft/db-mssql/tests</directory>
</testsuite>
<testsuite name="Mysql">
<directory>./vendor/yiisoft/db-mysql/tests</directory>
</testsuite>
<testsuite name="Oracle">
<directory>./vendor/yiisoft/db-oracle/tests</directory>
</testsuite>
<testsuite name="Pgsql">
<directory>./vendor/yiisoft/db-pgsql/tests</directory>
</testsuite>
<testsuite name="Sqlite">
<directory>./vendor/yiisoft/db-sqlite/tests</directory>
</testsuite>
</testsuites>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
executionOrder="default"
failOnRisky="true"
failOnWarning="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
>
<source>
<include>
<directory>./src</directory>
</include>
<exclude>
<directory>./src/TestSupport</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
<file>./src/Command/CommandInterface.php</file>
<file>./src/Command/ParamInterface.php</file>
<file>./src/Connection/ConnectionInterface.php</file>
<file>./src/Connection/ConnectionPoolInterface.php</file>
<file>./src/Constraint/ConstraintSchemaInterface.php</file>
<file>./src/Driver/DriverInterface.php</file>
<file>./src/Driver/PDO/CommandPDOInterface.php</file>
<file>./src/Driver/PDO/ConnectionPDOInterface.php</file>
<file>./src/Driver/PDO/ConnectionPDOPoolInterface.php</file>
<file>./src/Driver/PDO/PDODriverInterface.php</file>
<file>./src/Expression/ExpressionBuilderInterface.php</file>
<file>./src/Expression/ExpressionInterface.php</file>
<file>./src/Profiler/ProfilerInterface.php</file>
<file>./src/Query/BatchQueryResultInterface.php</file>
<file>./src/Query/Data/DataReaderInterface.php</file>
<file>./src/Query/QueryFunctionsInterface.php</file>
<file>./src/Query/QueryInterface.php</file>
<file>./src/Query/QueryPartsInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/BetweenColumnsConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/BetweenConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/ConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/ConjunctionConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/ExistConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/HashConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/InConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/LikeConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/NotConditionInterface.php</file>
<file>./src/QueryBuilder/Condition/Interface/SimpleConditionInterface.php</file>
<file>./src/QueryBuilder/DDLQueryBuilderInterface.php</file>
<file>./src/QueryBuilder/DMLQueryBuilderInterface.php</file>
<file>./src/QueryBuilder/DQLQueryBuilderInterface.php</file>
<file>./src/QueryBuilder/QueryBuilderInterface.php</file>
<file>./src/Schema/ColumnSchemaInterface.php</file>
<file>./src/Schema/QuoterInterface.php</file>
<file>./src/Schema/SchemaInterface.php</file>
<file>./src/Schema/TableSchemaInterface.php</file>
<file>./src/Transaction/TransactionInterface.php</file>
</exclude>
</source>
<php>
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="Db">
<directory>./tests/Db</directory>
</testsuite>
<testsuite name="ActiveRecord">
<directory>./vendor/yiisoft/active-record/tests/Driver</directory>
</testsuite>
<testsuite name="Mssql">
<directory>./vendor/yiisoft/db-mssql/tests</directory>
</testsuite>
<testsuite name="Mysql">
<directory>./vendor/yiisoft/db-mysql/tests</directory>
</testsuite>
<testsuite name="Oracle">
<directory>./vendor/yiisoft/db-oracle/tests</directory>
</testsuite>
<testsuite name="Pgsql">
<directory>./vendor/yiisoft/db-pgsql/tests</directory>
</testsuite>
<testsuite name="Sqlite">
<directory>./vendor/yiisoft/db-sqlite/tests</directory>
</testsuite>
</testsuites>
</phpunit>
1 change: 0 additions & 1 deletion src/Connection/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Db\Connection;

use Closure;
use Exception;
use Throwable;
use Yiisoft\Db\Query\BatchQueryResult;
use Yiisoft\Db\Query\BatchQueryResultInterface;
Expand Down
28 changes: 0 additions & 28 deletions src/Schema/AbstractColumnSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@

namespace Yiisoft\Db\Schema;

use Yiisoft\Db\Command\DataType;
use Yiisoft\Db\Command\Param;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Helper\DbStringHelper;

use function count;
use function gettype;
use function in_array;
use function is_array;
use function is_bool;
use function is_float;
use function is_resource;
Expand Down Expand Up @@ -271,15 +267,6 @@ protected function typecast(mixed $value): mixed
return $value;
}

if (
is_array($value)
&& count($value) === 2
&& isset($value[1])
&& in_array($value[1], $this->getDataTypes(), true)
) {
return new Param((string) $value[0], $value[1]);
}

switch ($this->phpType) {
case SchemaInterface::PHP_TYPE_RESOURCE:
case SchemaInterface::PHP_TYPE_STRING:
Expand Down Expand Up @@ -312,19 +299,4 @@ protected function typecast(mixed $value): mixed

return $value;
}

/**
* @return int[] Array of numbers that represent possible parameter types.
*/
private function getDataTypes(): array
{
return [
DataType::BOOLEAN,
DataType::INTEGER,
DataType::STRING,
DataType::LOB,
DataType::NULL,
DataType::STMT,
];
}
}
24 changes: 24 additions & 0 deletions tests/Common/CommonSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use function ksort;
use function mb_chr;
use function sort;
use function str_replace;
use function strtolower;

abstract class CommonSchemaTest extends AbstractSchemaTest
Expand Down Expand Up @@ -1148,6 +1149,29 @@ public function testWorkWithIndex(
$db->close();
}

/**
* @link https://github.com/yiisoft/db/issues/718
*/
public function testIssue718(): void
{
$db = $this->getConnection();

if ($db->getDriverName() === 'oci' || $db->getDriverName() === 'sqlite' || $db->getDriverName() === 'sqlsrv') {
$this->markTestSkipped('Test is not supported by sqlite, oci and sqlsrv drivers.');
}

if ($db->getTableSchema('{{%table}}', true) !== null) {
$db->createCommand()->dropTable('{{%table}}')->execute();
}

$db->createCommand()->createTable('{{%table}}', ['array' => 'json'])->execute();
$db->createCommand()->insert('{{%table}}', ['array' => [1, 2]])->execute();

$result = str_replace(' ', '', $db->createCommand('SELECT * FROM {{%table}}')->queryScalar());

$this->assertSame('[1,2]', trim($result));
}

protected function createTableForIndexAndConstraintTests(
ConnectionInterface $db,
string $tableName,
Expand Down
25 changes: 12 additions & 13 deletions tests/Db/Schema/ColumnSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Yiisoft\Db\Tests\Db\Schema;

use PHPUnit\Framework\TestCase;
use Yiisoft\Db\Command\DataType;
use Yiisoft\Db\Command\ParamInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Tests\Support\Stub\ColumnSchema;

Expand Down Expand Up @@ -144,6 +142,18 @@ public function testExtra(): void
$this->assertSame('', $column->getExtra());
}

/**
* @link https://github.com/yiisoft/db/issues/718
*/
public function testTypecastIssue718(): void
{
$column = new ColumnSchema('new');

$param = [1, 2];
$result = $column->dbTypecast($param);
$this->assertSame([1, 2], $result);
}

public function testName(): void
{
$column = new ColumnSchema('test');
Expand Down Expand Up @@ -238,17 +248,6 @@ public function testPhpTypecastWithStringNullValue(): void
$this->assertNull($column->phpTypecast(null));
}

public function testPhpTypecastWithStringParamValue(): void
{
$column = new ColumnSchema('new');

$column->phpType(SchemaInterface::PHP_TYPE_STRING);

$this->assertInstanceOf(ParamInterface::class, $column->phpTypecast(['test', DataType::STRING]));
$this->assertSame('test', $column->phpTypecast(['test', DataType::STRING])->getValue());
$this->assertSame(DataType::STRING, $column->phpTypecast(['test', DataType::STRING])->getType());
}

public function testPhpTypecastWithStringResourceValue(): void
{
$column = new ColumnSchema('new');
Expand Down

0 comments on commit e129d5e

Please sign in to comment.