diff --git a/tests/AbstractQueryTest.php b/tests/AbstractQueryTest.php index 920d8e4e4..f14739992 100644 --- a/tests/AbstractQueryTest.php +++ b/tests/AbstractQueryTest.php @@ -755,7 +755,7 @@ public function testColumnWithIndexBy(): void } /** - * @dataProvider populateProvider + * @dataProvider \Yiisoft\Db\Tests\Provider\QueryProvider::populate() */ public function testPopulate(array $rows): void { @@ -765,28 +765,10 @@ public function testPopulate(array $rows): void $this->assertSame($rows, $query->populate($rows)); } - public function populateProvider(): array - { - return [ - [ - [], - ], - [ - [['value']], - ], - [ - [['key' => 'value']], - ], - [ - [['table.key' => 'value']], - ], - ]; - } - /** - * @dataProvider populateProviderWithIndexBy - * @dataProvider populateProviderWithIncorrectIndexBy - * @dataProvider populateProviderWithIndexByClosure + * @dataProvider \Yiisoft\Db\Tests\Provider\QueryProvider::populateWithIndexBy() + * @dataProvider \Yiisoft\Db\Tests\Provider\QueryProvider::populateWithIncorrectIndexBy() + * @dataProvider \Yiisoft\Db\Tests\Provider\QueryProvider::populateWithIndexByClosure() */ public function testPopulateWithIndexBy(Closure|string|null $indexBy, array $rows, array $populated): void { @@ -797,7 +779,7 @@ public function testPopulateWithIndexBy(Closure|string|null $indexBy, array $row } /** - * @dataProvider populateProviderWithIndexBy + * @dataProvider \Yiisoft\Db\Tests\Provider\QueryProvider::populateWithIndexBy() */ public function testPopulateWithIndexByWithObject(Closure|string|null $indexBy, array $rows, array $expectedPopulated): void { @@ -811,7 +793,7 @@ public function testPopulateWithIndexByWithObject(Closure|string|null $indexBy, } /** - * @dataProvider populateProviderWithIncorrectIndexBy + * @dataProvider \Yiisoft\Db\Tests\Provider\QueryProvider::populateWithIncorrectIndexBy() */ public function testPopulateWithIncorrectIndexByWithObject(Closure|string|null $indexBy, array $rows): void { @@ -831,217 +813,8 @@ public function testPopulateWithIncorrectIndexByWithObject(Closure|string|null $ restore_error_handler(); } - public function populateProviderWithIndexBy(): array - { - return [ - 'null key with empty rows' => [ - null, - 'rows' => [], - 'expected' => [], - ], - 'null key' => [ - null, - [ - ['key' => 'value1'], - ['key' => 'value2'], - ], - [ - ['key' => 'value1'], - ['key' => 'value2'], - ], - ], - 'correct key' => [ - 'key', - [ - ['key' => 'value1'], - ['key' => 'value2'], - ], - [ - 'value1' => ['key' => 'value1'], - 'value2' => ['key' => 'value2'], - ], - ], - 'null-key and composite.key' => [ - null, - [ - ['table.key' => 'value1'], - ['table.key' => 'value2'], - ], - [ - ['table.key' => 'value1'], - ['table.key' => 'value2'], - ], - ], - 'key with space' => [ - 'table key', - [ - ['table key' => 'value1'], - ['table key' => 'value2'], - ], - [ - 'value1' => ['table key' => 'value1'], - 'value2' => ['table key' => 'value2'], - ], - ], - 'composite-key and simple key' => [ - 't.key', - [ - [ - 'key' => 'value1', - 't' => [ - 'key' => 'value2', - ], - ], - ], - [ - 'value2' => [ - 'key' => 'value1', - 't' => [ - 'key' => 'value2', - ], - ], - ], - ], - 'composite-3-key and simple key' => [ - 't1.t2.key', - [ - [ - 'key' => 'value1', - 't1' => [ - 'key' => 'value2', - 't2' => [ - 'key' => 'value3', - ], - ], - ], - ], - [ - 'value3' => [ - 'key' => 'value1', - 't1' => [ - 'key' => 'value2', - 't2' => [ - 'key' => 'value3', - ], - ], - ], - ], - ], - 'composite-key and composite key' => [ - 'table.key', - [ - ['table.key' => 'value1'], - ['table.key' => 'value2'], - ], - [ - 'value1' => ['table.key' => 'value1'], - 'value2' => ['table.key' => 'value2'], - ], - ], - ]; - } - - public function populateProviderWithIncorrectIndexBy(): array - { - return [ - 'not existed key' => [ - 'incorrectKey', - [ - ['table.key' => 'value1'], - ['table.key' => 'value2'], - ], - [ - '' => ['table.key' => 'value2'], - ], - ], - 'empty key (not found key behavior)' => [ - '', - [ - ['table.key' => 'value1'], - ['table.key' => 'value2'], - ], - [ - '' => ['table.key' => 'value2'], - ], - ], - 'key and composite key (not found key behavior)' => [ - 'key', - [ - ['table.key' => 'value1'], - ['table.key' => 'value2'], - ], - [ - '' => ['table.key' => 'value2'], - ], - ], - ]; - } - - public function populateProviderWithIndexByClosure(): array - { - return [ - [ - static function ($row) { - return $row['key']; - }, - [ - ['key' => 'value1'], - ['key' => 'value2'], - ], - [ - 'value1' => ['key' => 'value1'], - 'value2' => ['key' => 'value2'], - ], - ], - ]; - } - - public function filterConditionDataProvider(): array - { - return [ - /* like */ - [['like', 'name', []], null], - [['not like', 'name', []], null], - [['or like', 'name', []], null], - [['or not like', 'name', []], null], - - /* not */ - [['not', ''], null], - - /* and */ - [['and', '', ''], null], - [['and', '', 'id=2'], ['and', 'id=2']], - [['and', 'id=1', ''], ['and', 'id=1']], - [['and', 'type=1', ['or', '', 'id=2']], ['and', 'type=1', ['or', 'id=2']]], - - /* or */ - [['or', 'id=1', ''], ['or', 'id=1']], - [['or', 'type=1', ['or', '', 'id=2']], ['or', 'type=1', ['or', 'id=2']]], - - /* between */ - [['between', 'id', 1, null], null], - [['between', 'id'], null], - [['between', 'id', 1], null], - [['not between', 'id', null, 10], null], - [['between', 'id', 1, 2], ['between', 'id', 1, 2]], - - /* in */ - [['in', 'id', []], null], - [['not in', 'id', []], null], - - /* simple conditions */ - [['=', 'a', ''], null], - [['>', 'a', ''], null], - [['>=', 'a', ''], null], - [['<', 'a', ''], null], - [['<=', 'a', ''], null], - [['<>', 'a', ''], null], - [['!=', 'a', ''], null], - ]; - } - /** - * @dataProvider filterConditionDataProvider + * @dataProvider \Yiisoft\Db\Tests\Provider\QueryProvider::filterConditionData() */ public function testFilterCondition(array|string $condition, array|string|null $expected): void { @@ -1052,18 +825,8 @@ public function testFilterCondition(array|string $condition, array|string|null $ $this->assertEquals($expected, $query->getWhere()); } - public function normalizeOrderByProvider(): array - { - return [ - ['id', ['id' => 4]], - [['id'], ['id']], - ['name ASC, date DESC', ['name' => 4, 'date' => 3]], - [new Expression('SUBSTR(name, 3, 4) DESC, x ASC'), [new Expression('SUBSTR(name, 3, 4) DESC, x ASC')]], - ]; - } - /** - * @dataProvider normalizeOrderByProvider + * @dataProvider \Yiisoft\Db\Tests\Provider\QueryProvider::normalizeOrderBy() */ public function testNormalizeOrderBy(array|string|Expression $columns, array|string $expected): void { @@ -1074,22 +837,8 @@ public function testNormalizeOrderBy(array|string|Expression $columns, array|str $this->assertEquals($expected, $query->getOrderBy()); } - public function normalizeSelectProvider(): array - { - return [ - ['exists', ['exists' => 'exists']], - ['count(*) > 1', ['count(*) > 1']], - ['name, name, name as X, name as X', ['name' => 'name', 'X' => 'name']], - [ - ['email', 'address', 'status' => new Expression('1')], - ['email' => 'email', 'address' => 'address', 'status' => new Expression('1')], - ], - [new Expression('1 as Ab'), [new Expression('1 as Ab')]], - ]; - } - /** - * @dataProvider normalizeSelectProvider + * @dataProvider \Yiisoft\Db\Tests\Provider\QueryProvider::normalizeSelect() */ public function testNormalizeSelect(array|string|Expression $columns, array|string $expected): void { diff --git a/tests/Provider/AbstractQueryProvider.php b/tests/Provider/AbstractQueryProvider.php new file mode 100644 index 000000000..de0edfcb0 --- /dev/null +++ b/tests/Provider/AbstractQueryProvider.php @@ -0,0 +1,261 @@ +', 'a', ''], null], + [['>=', 'a', ''], null], + [['<', 'a', ''], null], + [['<=', 'a', ''], null], + [['<>', 'a', ''], null], + [['!=', 'a', ''], null], + ]; + } + + public function normalizeOrderBy(): array + { + return [ + ['id', ['id' => 4]], + [['id'], ['id']], + ['name ASC, date DESC', ['name' => 4, 'date' => 3]], + [new Expression('SUBSTR(name, 3, 4) DESC, x ASC'), [new Expression('SUBSTR(name, 3, 4) DESC, x ASC')]], + ]; + } + + public function normalizeSelect(): array + { + return [ + ['exists', ['exists' => 'exists']], + ['count(*) > 1', ['count(*) > 1']], + ['name, name, name as X, name as X', ['name' => 'name', 'X' => 'name']], + [ + ['email', 'address', 'status' => new Expression('1')], + ['email' => 'email', 'address' => 'address', 'status' => new Expression('1')], + ], + [new Expression('1 as Ab'), [new Expression('1 as Ab')]], + ]; + } + + public function populate(): array + { + return [ + [ + [], + ], + [ + [['value']], + ], + [ + [['key' => 'value']], + ], + [ + [['table.key' => 'value']], + ], + ]; + } + + public function populateWithIndexByClosure(): array + { + return [ + [ + static function ($row) { + return $row['key']; + }, + [ + ['key' => 'value1'], + ['key' => 'value2'], + ], + [ + 'value1' => ['key' => 'value1'], + 'value2' => ['key' => 'value2'], + ], + ], + ]; + } + + public function populateWithIncorrectIndexBy(): array + { + return [ + 'not existed key' => [ + 'incorrectKey', + [ + ['table.key' => 'value1'], + ['table.key' => 'value2'], + ], + [ + '' => ['table.key' => 'value2'], + ], + ], + 'empty key (not found key behavior)' => [ + '', + [ + ['table.key' => 'value1'], + ['table.key' => 'value2'], + ], + [ + '' => ['table.key' => 'value2'], + ], + ], + 'key and composite key (not found key behavior)' => [ + 'key', + [ + ['table.key' => 'value1'], + ['table.key' => 'value2'], + ], + [ + '' => ['table.key' => 'value2'], + ], + ], + ]; + } + + public function populateWithIndexBy(): array + { + return [ + 'null key with empty rows' => [ + null, + 'rows' => [], + 'expected' => [], + ], + 'null key' => [ + null, + [ + ['key' => 'value1'], + ['key' => 'value2'], + ], + [ + ['key' => 'value1'], + ['key' => 'value2'], + ], + ], + 'correct key' => [ + 'key', + [ + ['key' => 'value1'], + ['key' => 'value2'], + ], + [ + 'value1' => ['key' => 'value1'], + 'value2' => ['key' => 'value2'], + ], + ], + 'null-key and composite.key' => [ + null, + [ + ['table.key' => 'value1'], + ['table.key' => 'value2'], + ], + [ + ['table.key' => 'value1'], + ['table.key' => 'value2'], + ], + ], + 'key with space' => [ + 'table key', + [ + ['table key' => 'value1'], + ['table key' => 'value2'], + ], + [ + 'value1' => ['table key' => 'value1'], + 'value2' => ['table key' => 'value2'], + ], + ], + 'composite-key and simple key' => [ + 't.key', + [ + [ + 'key' => 'value1', + 't' => [ + 'key' => 'value2', + ], + ], + ], + [ + 'value2' => [ + 'key' => 'value1', + 't' => [ + 'key' => 'value2', + ], + ], + ], + ], + 'composite-3-key and simple key' => [ + 't1.t2.key', + [ + [ + 'key' => 'value1', + 't1' => [ + 'key' => 'value2', + 't2' => [ + 'key' => 'value3', + ], + ], + ], + ], + [ + 'value3' => [ + 'key' => 'value1', + 't1' => [ + 'key' => 'value2', + 't2' => [ + 'key' => 'value3', + ], + ], + ], + ], + ], + 'composite-key and composite key' => [ + 'table.key', + [ + ['table.key' => 'value1'], + ['table.key' => 'value2'], + ], + [ + 'value1' => ['table.key' => 'value1'], + 'value2' => ['table.key' => 'value2'], + ], + ], + ]; + } +} diff --git a/tests/Provider/AbstractQuoterProvider.php b/tests/Provider/AbstractQuoterProvider.php index 026c2ef5e..499328502 100644 --- a/tests/Provider/AbstractQuoterProvider.php +++ b/tests/Provider/AbstractQuoterProvider.php @@ -4,6 +4,8 @@ namespace Yiisoft\Db\Tests\Provider; +use Yiisoft\Db\Expression\Expression; + abstract class AbstractQuoterProvider { /** @@ -87,4 +89,18 @@ public function tableNameParts(): array ['other.animal2', 'animal2', 'other'], ]; } + + public function tablesNameDataProvider(): array + { + return [ + [['customer'], '', ['{{customer}}' => '{{customer}}']], + [['profile AS "prf"'], '', ['{{prf}}' => '{{profile}}']], + [['mainframe as400'], '', ['{{as400}}' => '{{mainframe}}']], + [ + ['x' => new Expression('(SELECT id FROM user)')], + '', + ['{{x}}' => new Expression('(SELECT id FROM user)')], + ], + ]; + } } diff --git a/tests/Provider/QueryProvider.php b/tests/Provider/QueryProvider.php new file mode 100644 index 000000000..b87d50866 --- /dev/null +++ b/tests/Provider/QueryProvider.php @@ -0,0 +1,9 @@ + '{{customer}}']], - [['profile AS "prf"'], '', ['{{prf}}' => '{{profile}}']], - [['mainframe as400'], '', ['{{as400}}' => '{{mainframe}}']], - [ - ['x' => new Expression('(SELECT id FROM user)')], - '', - ['{{x}}' => new Expression('(SELECT id FROM user)')], - ], - ]; - } - public function testCleanUpTableNamesException(): void { $this->expectException(InvalidArgumentException::class);