diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml index e4f3f8f8..3c670a95 100644 --- a/.github/workflows/rector.yml +++ b/.github/workflows/rector.yml @@ -1,13 +1,17 @@ +name: Rector + PHP CS Fixer + on: pull_request_target: paths: - 'src/**' - 'tests/**' - - '.github/workflows/rector.yml' + - '.github/workflows/rector-cs.yml' - 'composer.json' - 'rector.php' + - '.php-cs-fixer.dist.php' -name: Rector +permissions: + contents: read concurrency: group: ${{ github.workflow }}-${{ github.ref }} @@ -15,12 +19,11 @@ concurrency: jobs: rector: - uses: yiisoft/actions/.github/workflows/rector.yml@master + uses: yiisoft/actions/.github/workflows/rector-cs.yml@master secrets: token: ${{ secrets.YIISOFT_GITHUB_TOKEN }} with: repository: ${{ github.event.pull_request.head.repo.full_name }} - php: >- - ['8.4'] + php: '8.4' required-packages: >- ['db'] diff --git a/.gitignore b/.gitignore index d684f7dd..8e1f8a00 100644 --- a/.gitignore +++ b/.gitignore @@ -23,12 +23,10 @@ composer.phar # Mac DS_Store Files .DS_Store -# phpunit itself is not needed -phpunit.phar - -# local phpunit config and cache +# PHPUnit +/phpunit.phar /phpunit.xml /.phpunit.result.cache -# local tests configuration -/tests/data/config.local.php +# PHP CS Fixer +/.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 00000000..c8091220 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,22 @@ +in([ + __DIR__ . '/src', + __DIR__ . '/tests', +]); + +return (new Config()) + ->setParallelConfig(ParallelConfigFactory::detect()) + ->setRules([ + '@PER-CS3.0' => true, + 'no_unused_imports' => true, + 'ordered_class_elements' => true, + 'class_attributes_separation' => ['elements' => ['method' => 'one']], + ]) + ->setFinder($finder); diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 1ab379b4..00000000 --- a/.styleci.yml +++ /dev/null @@ -1,85 +0,0 @@ -preset: psr12 -risky: true - -version: 8.1 - -finder: - exclude: - - docs - - vendor - -enabled: - - alpha_ordered_traits - - array_indentation - - array_push - - combine_consecutive_issets - - combine_consecutive_unsets - - combine_nested_dirname - - declare_strict_types - - dir_constant - - fully_qualified_strict_types - - function_to_constant - - hash_to_slash_comment - - is_null - - logical_operators - - magic_constant_casing - - magic_method_casing - - method_separation - - modernize_types_casting - - native_function_casing - - native_function_type_declaration_casing - - no_alias_functions - - no_empty_comment - - no_empty_phpdoc - - no_empty_statement - - no_extra_block_blank_lines - - no_short_bool_cast - - no_superfluous_elseif - - no_unneeded_control_parentheses - - no_unneeded_curly_braces - - no_unneeded_final_method - - no_unset_cast - - no_unused_imports - - no_unused_lambda_imports - - no_useless_else - - no_useless_return - - normalize_index_brace - - php_unit_dedicate_assert - - php_unit_dedicate_assert_internal_type - - php_unit_expectation - - php_unit_mock - - php_unit_mock_short_will_return - - php_unit_namespaced - - php_unit_no_expectation_annotation - - phpdoc_no_empty_return - - phpdoc_no_useless_inheritdoc - - phpdoc_order - - phpdoc_property - - phpdoc_scalar - - phpdoc_singular_inheritdoc - - phpdoc_trim - - phpdoc_trim_consecutive_blank_line_separation - - phpdoc_type_to_var - - phpdoc_types - - phpdoc_types_order - - print_to_echo - - regular_callable_call - - return_assignment - - self_accessor - - self_static_accessor - - set_type_to_cast - - short_array_syntax - - short_list_syntax - - simplified_if_return - - single_quote - - standardize_not_equals - - ternary_to_null_coalescing - - trailing_comma_in_multiline_array - - unalign_double_arrow - - unalign_equals - - empty_loop_body_braces - - integer_literal_case - - union_type_without_spaces - -disabled: - - function_declaration diff --git a/composer.json b/composer.json index 5f614924..c647b45b 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "yiisoft/db": "dev-master" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.89.1", "maglnet/composer-require-checker": "^4.7.1", "phpunit/phpunit": "^10.5.45", "rector/rector": "^2.0.10", diff --git a/src/Builder/InBuilder.php b/src/Builder/InBuilder.php index 36617a40..91fa7c12 100644 --- a/src/Builder/InBuilder.php +++ b/src/Builder/InBuilder.php @@ -56,7 +56,7 @@ public function build(ExpressionInterface $expression, array &$params = []): str * * @return string|null `null` when split isn't required. Otherwise - built SQL condition. */ - protected function splitCondition(In|NotIn $condition, array &$params): string|null + protected function splitCondition(In|NotIn $condition, array &$params): ?string { $operator = match ($condition::class) { In::class => 'IN', @@ -80,7 +80,7 @@ protected function splitCondition(In|NotIn $condition, array &$params): string|n for ($i = 0; $i < $count; $i += $maxParameters) { $slices[] = $this->queryBuilder->createConditionFromArray( - [$operator, $column, array_slice($values, $i, $maxParameters)] + [$operator, $column, array_slice($values, $i, $maxParameters)], ); } diff --git a/src/Column/BooleanColumn.php b/src/Column/BooleanColumn.php index fd111398..a4d780b0 100644 --- a/src/Column/BooleanColumn.php +++ b/src/Column/BooleanColumn.php @@ -22,7 +22,7 @@ public function dbTypecast(mixed $value): string|ExpressionInterface|null }; } - public function phpTypecast(mixed $value): bool|null + public function phpTypecast(mixed $value): ?bool { if ($value === null) { return null; diff --git a/src/Column/ColumnBuilder.php b/src/Column/ColumnBuilder.php index e0b19753..76b1b48e 100644 --- a/src/Column/ColumnBuilder.php +++ b/src/Column/ColumnBuilder.php @@ -8,7 +8,7 @@ final class ColumnBuilder extends \Yiisoft\Db\Schema\Column\ColumnBuilder { - public static function binary(int|null $size = null): BinaryColumn + public static function binary(?int $size = null): BinaryColumn { return new BinaryColumn(ColumnType::BINARY, size: $size); } @@ -18,27 +18,27 @@ public static function boolean(): BooleanColumn return new BooleanColumn(ColumnType::BOOLEAN); } - public static function timestamp(int|null $size = 0): DateTimeColumn + public static function timestamp(?int $size = 0): DateTimeColumn { return new DateTimeColumn(ColumnType::TIMESTAMP, size: $size); } - public static function datetime(int|null $size = 0): DateTimeColumn + public static function datetime(?int $size = 0): DateTimeColumn { return new DateTimeColumn(ColumnType::DATETIME, size: $size); } - public static function datetimeWithTimezone(int|null $size = 0): DateTimeColumn + public static function datetimeWithTimezone(?int $size = 0): DateTimeColumn { return new DateTimeColumn(ColumnType::DATETIMETZ, size: $size); } - public static function time(int|null $size = 0): DateTimeColumn + public static function time(?int $size = 0): DateTimeColumn { return new DateTimeColumn(ColumnType::TIME, size: $size); } - public static function timeWithTimezone(int|null $size = 0): DateTimeColumn + public static function timeWithTimezone(?int $size = 0): DateTimeColumn { return new DateTimeColumn(ColumnType::TIMETZ, size: $size); } diff --git a/src/Column/ColumnDefinitionBuilder.php b/src/Column/ColumnDefinitionBuilder.php index 96a6a2d5..cfa673f4 100644 --- a/src/Column/ColumnDefinitionBuilder.php +++ b/src/Column/ColumnDefinitionBuilder.php @@ -62,12 +62,12 @@ protected function buildCheck(ColumnInterface $column): string } return match ($column->getType()) { - ColumnType::ARRAY, ColumnType::STRUCTURED, ColumnType::JSON => - version_compare($this->queryBuilder->getServerInfo()->getVersion(), '21', '<') + ColumnType::ARRAY, ColumnType::STRUCTURED, ColumnType::JSON + => version_compare($this->queryBuilder->getServerInfo()->getVersion(), '21', '<') ? ' CHECK (' . $this->queryBuilder->getQuoter()->quoteSimpleColumnName($name) . ' IS JSON)' : '', - ColumnType::BOOLEAN => - ' CHECK (' . $this->queryBuilder->getQuoter()->quoteSimpleColumnName($name) . ' IN (0,1))', + ColumnType::BOOLEAN + => ' CHECK (' . $this->queryBuilder->getQuoter()->quoteSimpleColumnName($name) . ' IN (0,1))', default => '', }; } @@ -124,8 +124,8 @@ protected function getDbType(ColumnInterface $column): string ColumnType::TIME => 'interval day(0) to second', ColumnType::TIMETZ => 'interval day(0) to second', ColumnType::DATE => 'date', - ColumnType::ARRAY, ColumnType::STRUCTURED, ColumnType::JSON => - version_compare($this->queryBuilder->getServerInfo()->getVersion(), '21', '>=') + ColumnType::ARRAY, ColumnType::STRUCTURED, ColumnType::JSON + => version_compare($this->queryBuilder->getServerInfo()->getVersion(), '21', '>=') ? 'json' : 'clob', default => 'varchar2', diff --git a/src/Column/ColumnFactory.php b/src/Column/ColumnFactory.php index fdca1868..a3b4639b 100644 --- a/src/Column/ColumnFactory.php +++ b/src/Column/ColumnFactory.php @@ -16,8 +16,6 @@ final class ColumnFactory extends AbstractColumnFactory { - private const DATETIME_REGEX = "/^(?:TIMESTAMP|DATE|INTERVAL|to_timestamp(?:_tz)?\(|to_date\(|to_dsinterval\()\s*'(?:\d )?([^']+)/"; - /** * The mapping from physical column types (keys) to abstract column types (values). * @@ -54,6 +52,7 @@ final class ColumnFactory extends AbstractColumnFactory /** Deprecated */ 'long' => ColumnType::TEXT, ]; + private const DATETIME_REGEX = "/^(?:TIMESTAMP|DATE|INTERVAL|to_timestamp(?:_tz)?\(|to_date\(|to_dsinterval\()\s*'(?:\d )?([^']+)/"; protected function columnDefinitionParser(): ColumnDefinitionParser { diff --git a/src/Column/DateTimeColumn.php b/src/Column/DateTimeColumn.php index 26bf497a..28d1d587 100644 --- a/src/Column/DateTimeColumn.php +++ b/src/Column/DateTimeColumn.php @@ -44,14 +44,14 @@ public function dbTypecast(mixed $value): float|int|string|ExpressionInterface|n return match ($this->getType()) { ColumnType::TIMESTAMP, ColumnType::DATETIME, ColumnType::DATETIMETZ => new Expression("TIMESTAMP '$value'"), ColumnType::TIME, ColumnType::TIMETZ => new Expression( - "INTERVAL '$value' DAY(0) TO SECOND" . (($size = $this->getSize()) !== null ? "($size)" : '') + "INTERVAL '$value' DAY(0) TO SECOND" . (($size = $this->getSize()) !== null ? "($size)" : ''), ), ColumnType::DATE => new Expression("DATE '$value'"), default => $value, }; } - public function phpTypecast(mixed $value): DateTimeImmutable|null + public function phpTypecast(mixed $value): ?DateTimeImmutable { if (is_string($value) && match ($this->getType()) { ColumnType::TIME, ColumnType::TIMETZ => true, diff --git a/src/Command.php b/src/Command.php index a1921cc0..cb33a870 100644 --- a/src/Command.php +++ b/src/Command.php @@ -46,7 +46,7 @@ public function insertReturningPks(string $table, array|QueryInterface $columns) if ($columns instanceof QueryInterface) { throw new NotSupportedException( - __METHOD__ . '() is not supported by Oracle when inserting sub-query.' + __METHOD__ . '() is not supported by Oracle when inserting sub-query.', ); } @@ -130,7 +130,7 @@ protected function bindPendingParams(): void $name, $paramsPassedByReference[$name], $param->type, - strlen((string) $param->value) + strlen((string) $param->value), ); } else { $this->pdoStatement?->bindValue($name, $param->value, $param->type); diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index 68ac100c..e9f40ab4 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -26,7 +26,7 @@ public function addForeignKey( string $referenceTable, array|string $referenceColumns, ?string $delete = null, - ?string $update = null + ?string $update = null, ): string { $sql = 'ALTER TABLE ' . $this->quoter->quoteTableName($table) . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name) @@ -98,7 +98,7 @@ public function dropTable(string $table, bool $ifExists = false, bool $cascade = public function renameTable(string $oldName, string $newName): string { - return 'ALTER TABLE ' . $this->quoter->quoteTableName($oldName) . ' RENAME TO ' . - $this->quoter->quoteTableName($newName); + return 'ALTER TABLE ' . $this->quoter->quoteTableName($oldName) . ' RENAME TO ' + . $this->quoter->quoteTableName($newName); } } diff --git a/src/DMLQueryBuilder.php b/src/DMLQueryBuilder.php index 93279011..e397ad17 100644 --- a/src/DMLQueryBuilder.php +++ b/src/DMLQueryBuilder.php @@ -60,7 +60,7 @@ public function update( array $columns, array|string|ExpressionInterface $condition, array|string|ExpressionInterface|null $from = null, - array &$params = [] + array &$params = [], ): string { if ($from !== null) { throw new NotSupportedException('Oracle does not support FROM clause in UPDATE statement.'); @@ -83,7 +83,7 @@ public function upsert( $table, $insertColumns, $updateColumns, - $constraints + $constraints, ); if (empty($uniqueNames)) { @@ -139,39 +139,12 @@ public function upsertReturning( string $table, array|QueryInterface $insertColumns, array|bool $updateColumns = true, - array|null $returnColumns = null, + ?array $returnColumns = null, array &$params = [], ): string { throw new NotSupportedException(__METHOD__ . '() is not supported by Oracle.'); } - protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array - { - if (empty($columns)) { - $names = []; - $placeholders = []; - $tableSchema = $this->schema->getTableSchema($table); - - if ($tableSchema !== null) { - if (!empty($tableSchema->getPrimaryKey())) { - $names = $tableSchema->getPrimaryKey(); - } else { - /** - * @psalm-suppress PossiblyNullArgument - * @var string[] $names - */ - $names = [array_key_first($tableSchema->getColumns())]; - } - - $placeholders = array_fill(0, count($names), 'DEFAULT'); - } - - return [$names, $placeholders, '', $params]; - } - - return parent::prepareInsertValues($table, $columns, $params); - } - public function resetSequence(string $table, int|string|null $value = null): string { $tableSchema = $this->schema->getTableSchema($table); @@ -203,4 +176,31 @@ public function resetSequence(string $table, int|string|null $value = null): str execute immediate \'CREATE SEQUENCE "' . $sequenceName . '" START WITH \' || lastSeq || \' INCREMENT BY 1 NOMAXVALUE NOCACHE\'; end;'; } + + protected function prepareInsertValues(string $table, array|QueryInterface $columns, array $params = []): array + { + if (empty($columns)) { + $names = []; + $placeholders = []; + $tableSchema = $this->schema->getTableSchema($table); + + if ($tableSchema !== null) { + if (!empty($tableSchema->getPrimaryKey())) { + $names = $tableSchema->getPrimaryKey(); + } else { + /** + * @psalm-suppress PossiblyNullArgument + * @var string[] $names + */ + $names = [array_key_first($tableSchema->getColumns())]; + } + + $placeholders = array_fill(0, count($names), 'DEFAULT'); + } + + return [$names, $placeholders, '', $params]; + } + + return parent::prepareInsertValues($table, $columns, $params); + } } diff --git a/src/DQLQueryBuilder.php b/src/DQLQueryBuilder.php index 6c44f9fb..b73a18ca 100644 --- a/src/DQLQueryBuilder.php +++ b/src/DQLQueryBuilder.php @@ -33,7 +33,7 @@ public function buildOrderByAndLimit( array $orderBy, ExpressionInterface|int|null $limit, ExpressionInterface|int|null $offset, - array &$params = [] + array &$params = [], ): string { $orderByString = $this->buildOrderBy($orderBy, $params); @@ -44,13 +44,13 @@ public function buildOrderByAndLimit( $filters = []; if (!empty($offset)) { - $filters[] = 'rowNumId > ' . - ($offset instanceof ExpressionInterface ? $this->buildExpression($offset) : (string) $offset); + $filters[] = 'rowNumId > ' + . ($offset instanceof ExpressionInterface ? $this->buildExpression($offset) : (string) $offset); } if ($limit !== null) { - $filters[] = 'rownum <= ' . - ($limit instanceof ExpressionInterface ? $this->buildExpression($limit) : (string) $limit); + $filters[] = 'rownum <= ' + . ($limit instanceof ExpressionInterface ? $this->buildExpression($limit) : (string) $limit); } if (empty($filters)) { @@ -69,7 +69,7 @@ public function selectExists(string $rawSql): string return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END AS "0" FROM DUAL'; } - public function buildFrom(array|null $tables, array &$params): string + public function buildFrom(?array $tables, array &$params): string { if (empty($tables)) { return 'FROM DUAL'; @@ -84,7 +84,7 @@ public function buildWithQueries(array $withQueries, array &$params): string static fn(WithQuery $withQuery) => new WithQuery( $withQuery->query, $withQuery->alias, - false + false, ), $withQueries, ); diff --git a/src/Driver.php b/src/Driver.php index 61829b88..3ec22f17 100644 --- a/src/Driver.php +++ b/src/Driver.php @@ -28,7 +28,7 @@ public function createConnection(): PDO NLS_TIME_FORMAT = 'HH24:MI:SSXFF' NLS_TIME_TZ_FORMAT = 'HH24:MI:SSXFFTZH:TZM' NLS_DATE_FORMAT = 'YYYY-MM-DD' - SQL + SQL, ); return $pdo; diff --git a/src/Dsn.php b/src/Dsn.php index e939552e..0a200f02 100644 --- a/src/Dsn.php +++ b/src/Dsn.php @@ -24,8 +24,7 @@ public function __construct( public readonly string $databaseName = '', public readonly string $port = '1521', public readonly array $options = [], - ) { - } + ) {} /** * @return string The Data Source Name, or DSN, contains the information required to connect to the database. diff --git a/src/Schema.php b/src/Schema.php index b8d2469c..5cf3ea60 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -61,6 +61,12 @@ */ final class Schema extends AbstractPdoSchema { + public function __construct(protected ConnectionInterface $db, SchemaCache $schemaCache, string $defaultSchema) + { + $this->defaultSchema = $defaultSchema; + parent::__construct($db, $schemaCache); + } + protected function findConstraints(TableSchemaInterface $table): void { $tableName = $this->resolveFullName($table->getName(), $table->getSchemaName()); @@ -70,12 +76,6 @@ protected function findConstraints(TableSchemaInterface $table): void $table->indexes(...$this->getTableMetadata($tableName, SchemaInterface::INDEXES)); } - public function __construct(protected ConnectionInterface $db, SchemaCache $schemaCache, string $defaultSchema) - { - $this->defaultSchema = $defaultSchema; - parent::__construct($db, $schemaCache); - } - /** * @link https://docs.oracle.com/cd/B28359_01/server.111/b28337/tdpsg_user_accounts.htm */ @@ -155,7 +155,7 @@ protected function findTableNames(string $schema = ''): array * * @psalm-suppress MoreSpecificImplementedParamType */ - protected function loadResultColumn(array $metadata): ColumnInterface|null + protected function loadResultColumn(array $metadata): ?ColumnInterface { if (empty($metadata['oci:decl_type'])) { return null; @@ -191,8 +191,8 @@ protected function loadResultColumn(array $metadata): ColumnInterface|null 'timestamp with time zone', 'timestamp with local time zone' => $columnInfo['size'] = $metadata['scale'], 'interval day to second', - 'interval year to month' => - [$columnInfo['size'], $columnInfo['scale']] = [$metadata['scale'], $metadata['precision']], + 'interval year to month' + => [$columnInfo['size'], $columnInfo['scale']] = [$metadata['scale'], $metadata['precision']], 'number' => $metadata['scale'] !== -127 ? $columnInfo['scale'] = $metadata['scale'] : null, 'float' => null, default => $columnInfo['size'] = $metadata['len'], @@ -208,7 +208,7 @@ protected function loadResultColumn(array $metadata): ColumnInterface|null return $this->db->getColumnFactory()->fromDbType($dbType, $columnInfo); } - protected function loadTableSchema(string $name): TableSchemaInterface|null + protected function loadTableSchema(string $name): ?TableSchemaInterface { $table = new TableSchema(...$this->db->getQuoter()->getTableNameParts($name)); @@ -390,7 +390,7 @@ protected function findColumns(TableSchemaInterface $table): bool * * @internal TableSchemaInterface `$table->getName()` The table schema. */ - protected function getTableSequenceName(string $tableName): string|null + protected function getTableSequenceName(string $tableName): ?string { $sequenceNameSql = << 'SELECT VIEW_NAME FROM USER_VIEWS', + default => "SELECT VIEW_NAME FROM ALL_VIEWS WHERE OWNER = '$schema'", + }; + + /** @var string[] */ + return $this->db->createCommand($sql)->queryColumn(); + } + /** * Loads the column information into a {@see ColumnInterface} object. * @@ -533,15 +544,4 @@ private function loadTableConstraints(string $tableName, string $returnType): ar return $result[$returnType]; } - - protected function findViewNames(string $schema = ''): array - { - $sql = match ($schema) { - '' => 'SELECT VIEW_NAME FROM USER_VIEWS', - default => "SELECT VIEW_NAME FROM ALL_VIEWS WHERE OWNER = '$schema'", - }; - - /** @var string[] */ - return $this->db->createCommand($sql)->queryColumn(); - } } diff --git a/src/SqlParser.php b/src/SqlParser.php index 7d03db6a..e1c20358 100644 --- a/src/SqlParser.php +++ b/src/SqlParser.php @@ -8,7 +8,7 @@ final class SqlParser extends AbstractSqlParser { - public function getNextPlaceholder(int|null &$position = null): string|null + public function getNextPlaceholder(?int &$position = null): ?string { $result = null; $length = $this->length - 1; diff --git a/tests/ColumnDefinitionParserTest.php b/tests/ColumnDefinitionParserTest.php index de744770..df02a28c 100644 --- a/tests/ColumnDefinitionParserTest.php +++ b/tests/ColumnDefinitionParserTest.php @@ -12,11 +12,6 @@ */ final class ColumnDefinitionParserTest extends AbstractColumnDefinitionParserTest { - protected function createColumnDefinitionParser(): ColumnDefinitionParser - { - return new ColumnDefinitionParser(); - } - /** * @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\ColumnDefinitionParserProvider::parse */ @@ -24,4 +19,9 @@ public function testParse(string $definition, array $expected): void { parent::testParse($definition, $expected); } + + protected function createColumnDefinitionParser(): ColumnDefinitionParser + { + return new ColumnDefinitionParser(); + } } diff --git a/tests/ColumnFactoryTest.php b/tests/ColumnFactoryTest.php index a71a0afc..d7b7a94d 100644 --- a/tests/ColumnFactoryTest.php +++ b/tests/ColumnFactoryTest.php @@ -18,11 +18,6 @@ final class ColumnFactoryTest extends AbstractColumnFactoryTest { use TestTrait; - protected function getColumnFactoryClass(): string - { - return ColumnFactory::class; - } - #[DataProviderExternal(ColumnFactoryProvider::class, 'dbTypes')] public function testFromDbType(string $dbType, string $expectedType, string $expectedInstanceOf): void { @@ -48,8 +43,13 @@ public function testFromType(string $type, string $expectedType, string $expecte } #[DataProviderExternal(ColumnFactoryProvider::class, 'defaultValueRaw')] - public function testFromTypeDefaultValueRaw(string $type, string|null $defaultValueRaw, mixed $expected): void + public function testFromTypeDefaultValueRaw(string $type, ?string $defaultValueRaw, mixed $expected): void { parent::testFromTypeDefaultValueRaw($type, $defaultValueRaw, $expected); } + + protected function getColumnFactoryClass(): string + { + return ColumnFactory::class; + } } diff --git a/tests/ColumnTest.php b/tests/ColumnTest.php index 8cd0dcc8..12e04abb 100644 --- a/tests/ColumnTest.php +++ b/tests/ColumnTest.php @@ -35,48 +35,6 @@ final class ColumnTest extends CommonColumnTest { use TestTrait; - protected function insertTypeValues(ConnectionInterface $db): void - { - $db->createCommand()->insert( - 'type', - [ - 'int_col' => 1, - 'char_col' => str_repeat('x', 100), - 'char_col3' => null, - 'float_col' => 1.234, - 'blob_col' => "\x10\x11\x12", - 'timestamp_col' => new Expression("TIMESTAMP '2023-07-11 14:50:23'"), - 'timestamp_local' => '2023-07-11 14:50:23', - 'time_col' => new DateTimeImmutable('14:50:23'), - 'bool_col' => false, - 'bit_col' => 0b0110_0110, // 102 - 'json_col' => [['a' => 1, 'b' => null, 'c' => [1, 3, 5]]], - ] - )->execute(); - } - - protected function assertTypecastedValues(array $result, bool $allTypecasted = false): void - { - $utcTimezone = new DateTimeZone('UTC'); - - $this->assertSame(1, $result['int_col']); - $this->assertSame(str_repeat('x', 100), $result['char_col']); - $this->assertNull($result['char_col3']); - $this->assertSame(1.234, $result['float_col']); - $this->assertSame("\x10\x11\x12", (string) $result['blob_col']); - $this->assertEquals(new DateTimeImmutable('2023-07-11 14:50:23', $utcTimezone), $result['timestamp_col']); - $this->assertEquals(new DateTimeImmutable('2023-07-11 14:50:23', $utcTimezone), $result['timestamp_local']); - $this->assertEquals(new DateTimeImmutable('14:50:23'), $result['time_col']); - $this->assertEquals(false, $result['bool_col']); - $this->assertSame(0b0110_0110, $result['bit_col']); - - if ($allTypecasted) { - $this->assertSame([['a' => 1, 'b' => null, 'c' => [1, 3, 5]]], $result['json_col']); - } else { - $this->assertSame('[{"a":1,"b":null,"c":[1,3,5]}]', (string) $result['json_col']); - } - } - public function testQueryWithTypecasting(): void { $db = $this->getConnection(); @@ -296,7 +254,7 @@ public function testTimestampColumnOnDifferentTimezones(): void [ 'timestamp_col' => ColumnBuilder::timestamp(), 'datetime_col' => ColumnBuilder::datetime(), - ] + ], )->execute(); $command->insert($tableName, [ @@ -325,4 +283,46 @@ public function testTimestampColumnOnDifferentTimezones(): void $db->close(); } + + protected function insertTypeValues(ConnectionInterface $db): void + { + $db->createCommand()->insert( + 'type', + [ + 'int_col' => 1, + 'char_col' => str_repeat('x', 100), + 'char_col3' => null, + 'float_col' => 1.234, + 'blob_col' => "\x10\x11\x12", + 'timestamp_col' => new Expression("TIMESTAMP '2023-07-11 14:50:23'"), + 'timestamp_local' => '2023-07-11 14:50:23', + 'time_col' => new DateTimeImmutable('14:50:23'), + 'bool_col' => false, + 'bit_col' => 0b0110_0110, // 102 + 'json_col' => [['a' => 1, 'b' => null, 'c' => [1, 3, 5]]], + ], + )->execute(); + } + + protected function assertTypecastedValues(array $result, bool $allTypecasted = false): void + { + $utcTimezone = new DateTimeZone('UTC'); + + $this->assertSame(1, $result['int_col']); + $this->assertSame(str_repeat('x', 100), $result['char_col']); + $this->assertNull($result['char_col3']); + $this->assertSame(1.234, $result['float_col']); + $this->assertSame("\x10\x11\x12", (string) $result['blob_col']); + $this->assertEquals(new DateTimeImmutable('2023-07-11 14:50:23', $utcTimezone), $result['timestamp_col']); + $this->assertEquals(new DateTimeImmutable('2023-07-11 14:50:23', $utcTimezone), $result['timestamp_local']); + $this->assertEquals(new DateTimeImmutable('14:50:23'), $result['time_col']); + $this->assertEquals(false, $result['bool_col']); + $this->assertSame(0b0110_0110, $result['bit_col']); + + if ($allTypecasted) { + $this->assertSame([['a' => 1, 'b' => null, 'c' => [1, 3, 5]]], $result['json_col']); + } else { + $this->assertSame('[{"a":1,"b":null,"c":[1,3,5]}]', (string) $result['json_col']); + } + } } diff --git a/tests/CommandTest.php b/tests/CommandTest.php index 482a619f..fa20c70c 100644 --- a/tests/CommandTest.php +++ b/tests/CommandTest.php @@ -42,7 +42,7 @@ public function testAddDefaultValue(): void $this->expectException(NotSupportedException::class); $this->expectExceptionMessage( - 'Yiisoft\Db\Oracle\DDLQueryBuilder::addDefaultValue is not supported by Oracle.' + 'Yiisoft\Db\Oracle\DDLQueryBuilder::addDefaultValue is not supported by Oracle.', ); $command->addDefaultValue('{{table}}', '{{name}}', 'column', 'value'); @@ -57,7 +57,7 @@ public function testBatchInsert( array $columns, string $expected, array $expectedParams = [], - int $insertedRow = 1 + int $insertedRow = 1, ): void { $db = $this->getConnection(); @@ -93,7 +93,7 @@ public function testBatchInsertWithAutoincrement(): void ['id' => '1', 'name' => 'John'], ['id' => '2', 'name' => 'Emma'], ], - (new Query($db))->from('test_batch_autoincrement')->all() + (new Query($db))->from('test_batch_autoincrement')->all(), ); $db->close(); @@ -146,7 +146,7 @@ public function testCreateTable(): void $command->createTable( '{{testCreateTable}}', - ['id' => PseudoType::PK, 'bar' => ColumnType::INTEGER] + ['id' => PseudoType::PK, 'bar' => ColumnType::INTEGER], )->execute(); $command->setSql( <<expectException(NotSupportedException::class); $this->expectExceptionMessage( - 'Yiisoft\Db\Oracle\DDLQueryBuilder::dropDefaultValue is not supported by Oracle.' + 'Yiisoft\Db\Oracle\DDLQueryBuilder::dropDefaultValue is not supported by Oracle.', ); $command->dropDefaultValue('{{table}}', '{{name}}'); @@ -368,7 +368,7 @@ public function testInsertSelectAlias(): void 'email' => 't1@example.com', 'name' => 'test', 'address' => 'test address', - ] + ], )->execute(); $query = $command->setSql( @@ -464,13 +464,13 @@ public function testNoTablenameReplacement(): void 'name' => 'Some {{updated}} name', 'address' => 'Some {{%updated}} address', ], - ['id' => $customerId] + ['id' => $customerId], )->execute(); $customer = $command->setSql( << new DateTimeColumn( dbType: 'timestamp with local time zone', - size:6, + size: 6, dbTimezone: $dbTimezone, ), 'time_col' => new DateTimeColumn( diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 609c5659..ab4bc91e 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -57,9 +57,9 @@ public function testAddForeignKey( array|string $columns, string $refTable, array|string $refColumns, - string|null $delete, - string|null $update, - string $expected + ?string $delete, + ?string $update, + string $expected, ): void { // Oracle does not support ON UPDATE CASCADE parent::testAddForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, null, $expected); @@ -109,8 +109,8 @@ public function testBatchInsert( #[DataProviderExternal(QueryBuilderProvider::class, 'buildCondition')] public function testBuildCondition( array|ExpressionInterface|string $condition, - string|null $expected, - array $expectedParams + ?string $expected, + array $expectedParams, ): void { parent::testBuildCondition($condition, $expected, $expectedParams); } @@ -119,7 +119,7 @@ public function testBuildCondition( public function testBuildLikeCondition( array|ExpressionInterface $condition, string $expected, - array $expectedParams + array $expectedParams, ): void { parent::testBuildLikeCondition($condition, $expected, $expectedParams); } @@ -296,7 +296,7 @@ public function testDropDefaultValue(): void $this->expectException(NotSupportedException::class); $this->expectExceptionMessage( - 'Yiisoft\Db\Oracle\DDLQueryBuilder::dropDefaultValue is not supported by Oracle.' + 'Yiisoft\Db\Oracle\DDLQueryBuilder::dropDefaultValue is not supported by Oracle.', ); $qb->dropDefaultValue('T_constraints_1', 'CN_pk'); @@ -324,7 +324,7 @@ public function testInsert( array|QueryInterface $columns, array $params, string $expectedSQL, - array $expectedParams + array $expectedParams, ): void { parent::testInsert($table, $columns, $params, $expectedSQL, $expectedParams); } @@ -335,7 +335,7 @@ public function testInsertReturningPks( array|QueryInterface $columns, array $params, string $expectedSQL, - array $expectedParams + array $expectedParams, ): void { $this->expectException(NotSupportedException::class); $this->expectExceptionMessage( @@ -495,7 +495,7 @@ public function testUpsert( array|QueryInterface $insertColumns, array|bool $updateColumns, string $expectedSql, - array $expectedParams + array $expectedParams, ): void { parent::testUpsert($table, $insertColumns, $updateColumns, $expectedSql, $expectedParams); } @@ -505,9 +505,9 @@ public function testUpsertReturning( string $table, array|QueryInterface $insertColumns, array|bool $updateColumns, - array|null $returnColumns, + ?array $returnColumns, string $expectedSql, - array $expectedParams + array $expectedParams, ): void { $db = $this->getConnection(); $qb = $db->getQueryBuilder(); @@ -648,7 +648,7 @@ public function testArrayMergeWithTypeWithOrdering( . " UNION SELECT value FROM JSON_TABLE(:qp2, '$[*]' COLUMNS(value $operandType PATH '$'))" . " UNION SELECT value FROM JSON_TABLE((SELECT :qp3 FROM DUAL), '$[*]' COLUMNS(value $operandType PATH '$'))" . '))', - $qb->buildExpression($arrayMerge, $params) + $qb->buildExpression($arrayMerge, $params), ); Assert::arraysEquals( [ diff --git a/tests/QuoterTest.php b/tests/QuoterTest.php index 4125aee2..e2df60ba 100644 --- a/tests/QuoterTest.php +++ b/tests/QuoterTest.php @@ -32,7 +32,7 @@ public function testQuoteColumnName(string $columnName, string $expected): void public function testQuoteSimpleColumnName( string $columnName, string $expectedQuotedColumnName, - string|null $expectedUnQuotedColumnName = null + ?string $expectedUnQuotedColumnName = null, ): void { parent::testQuoteSimpleColumnName($columnName, $expectedQuotedColumnName, $expectedUnQuotedColumnName); } diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 478822b7..a1d494f9 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -193,7 +193,7 @@ public function testTableSchemaConstraintsWithPdoUppercase(string $tableName, st public function testTableSchemaWithDbSchemes( string $tableName, string $expectedTableName, - string $expectedSchemaName = '' + string $expectedSchemaName = '', ): void { $db = $this->getConnection(); @@ -204,15 +204,15 @@ public function testTableSchemaWithDbSchemes( $mockDb ->method('createCommand') ->with( - self::callback(static fn ($sql) => true), + self::callback(static fn($sql) => true), self::callback( function ($params) use ($expectedTableName, $expectedSchemaName) { $this->assertEquals($expectedTableName, $params[':tableName']); $this->assertEquals($expectedSchemaName, $params[':schemaName']); return true; - } - ) + }, + ), ) ->willReturn($commandMock); $schema = new Schema($mockDb, DbHelper::getSchemaCache(), 'dbo'); @@ -225,7 +225,7 @@ public function testWorkWithDefaultValueConstraint(): void { $this->expectException(NotSupportedException::class); $this->expectExceptionMessage( - 'Yiisoft\Db\Oracle\DDLQueryBuilder::addDefaultValue is not supported by Oracle.' + 'Yiisoft\Db\Oracle\DDLQueryBuilder::addDefaultValue is not supported by Oracle.', ); parent::testWorkWithDefaultValueConstraint(); @@ -243,7 +243,7 @@ public function testNotConnectionPDO(): void } #[DataProviderExternal(SchemaProvider::class, 'resultColumns')] - public function testGetResultColumn(ColumnInterface|null $expected, array $info): void + public function testGetResultColumn(?ColumnInterface $expected, array $info): void { parent::testGetResultColumn($expected, $info); } diff --git a/tests/SqlParserTest.php b/tests/SqlParserTest.php index 67b2d1c8..bac0da11 100644 --- a/tests/SqlParserTest.php +++ b/tests/SqlParserTest.php @@ -12,14 +12,14 @@ */ final class SqlParserTest extends AbstractSqlParserTest { - protected function createSqlParser(string $sql): SqlParser + /** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\SqlParserProvider::getNextPlaceholder */ + public function testGetNextPlaceholder(string $sql, ?string $expectedPlaceholder, ?int $expectedPosition): void { - return new SqlParser($sql); + parent::testGetNextPlaceholder($sql, $expectedPlaceholder, $expectedPosition); } - /** @dataProvider \Yiisoft\Db\Oracle\Tests\Provider\SqlParserProvider::getNextPlaceholder */ - public function testGetNextPlaceholder(string $sql, string|null $expectedPlaceholder, int|null $expectedPosition): void + protected function createSqlParser(string $sql): SqlParser { - parent::testGetNextPlaceholder($sql, $expectedPlaceholder, $expectedPosition); + return new SqlParser($sql); } }