Skip to content

Commit

Permalink
Move methods providers to class providers in tests. (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Feb 14, 2023
1 parent 1a89c2f commit 5b4ef4d
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 275 deletions.
269 changes: 9 additions & 260 deletions tests/AbstractQueryTest.php
Expand Up @@ -755,7 +755,7 @@ public function testColumnWithIndexBy(): void
}

/**
* @dataProvider populateProvider
* @dataProvider \Yiisoft\Db\Tests\Provider\QueryProvider::populate()
*/
public function testPopulate(array $rows): void
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down

0 comments on commit 5b4ef4d

Please sign in to comment.