Skip to content

Commit 62cfb0e

Browse files
committed
refactor: migrate tests to PHPUnit 12 attributes and conventions
- Convert @dataProvider annotations to #[DataProvider] attributes - Convert @Depends annotations to #[Depends] attributes - Convert @group annotations to #[Group] attributes - Make data provider methods static (PHPUnit 12 requirement) - Replace @ExpectedException with expectException() calls - Update test method signatures with native union types - Remove phpunit-retry-annotations usage from tests - Adapt test assertions and setUp/tearDown for PHPUnit 12 compatibility
1 parent dc61ade commit 62cfb0e

26 files changed

Lines changed: 457 additions & 344 deletions

tests/Functional/Bigquery/BigqueryBaseCase.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private function getBigqueryClient(): BigQueryClient
102102
);
103103
}
104104

105-
public function getDatasetName(): string
105+
public static function getDatasetName(): string
106106
{
107107
return getenv('TEST_PREFIX') . self::TEST_SCHEMA;
108108
}
@@ -114,13 +114,17 @@ protected function insertRowToTable(
114114
string $firstName,
115115
string $lastName,
116116
): void {
117-
$this->bqClient->runQuery($this->bqClient->query(sprintf(
118-
'INSERT INTO %s.%s VALUES (%d, %s, %s)',
119-
BigqueryQuote::quoteSingleIdentifier($schemaName),
120-
BigqueryQuote::quoteSingleIdentifier($tableName),
121-
$id,
122-
BigqueryQuote::quote($firstName),
123-
BigqueryQuote::quote($lastName),
124-
)));
117+
$this->bqClient->runQuery(
118+
$this->bqClient->query(
119+
sprintf(
120+
'INSERT INTO %s.%s VALUES (%d, %s, %s)',
121+
BigqueryQuote::quoteSingleIdentifier($schemaName),
122+
BigqueryQuote::quoteSingleIdentifier($tableName),
123+
$id,
124+
BigqueryQuote::quote($firstName),
125+
BigqueryQuote::quote($lastName),
126+
),
127+
),
128+
);
125129
}
126130
}

tests/Functional/Bigquery/Connection/QueryTagsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ public function testRunIdAndBranchIdQueryTagExists(): void
3737

3838
// Get job info and verify the run_id and branch_id label
3939
$job = $queryResults->job();
40+
/** @var array<string, mixed> $info */
4041
$info = $job->info();
4142

4243
$this->assertArrayHasKey('configuration', $info);
4344
/** @var array<string, mixed> $configuration */
4445
$configuration = $info['configuration'];
4546
$this->assertArrayHasKey('labels', $configuration);
46-
/** @var array<string, mixed> $labels */
47+
/** @var array<string, string> $labels */
4748
$labels = $configuration['labels'];
4849
$this->assertArrayHasKey('run_id', $labels);
4950
$this->assertEquals('e2e-utils-lib', $labels['run_id']);

tests/Functional/Bigquery/Connection/SessionTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@ public function testCreateSession(): void
2525
$this->assertIsString($sessionIdDecoded); // @phpstan-ignore method.alreadyNarrowedType
2626

2727
// plain session use
28-
$job = $this->bqClient->runJob($this->bqClient->query('SELECT 1', [
29-
'configuration' => [
28+
$job = $this->bqClient->runJob(
29+
$this->bqClient->query(
30+
'SELECT 1',
31+
[
32+
'configuration' => [
3033
'query' => [
3134
'connectionProperties' => [
3235
'key' => 'session_id',
3336
'value' => $session->getSessionId(),
3437
],
3538
],
36-
],
37-
]));
39+
],
40+
],
41+
),
42+
);
3843

3944
$testSessionPlain = Session::createFromJob($job);
4045
$this->assertSame($session->getSessionId(), $testSessionPlain->getSessionId());

tests/Functional/Bigquery/Schema/BigquerySchemaReflectionTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableDefinition;
1313
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableQueryBuilder;
1414
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableReflection;
15+
use PHPUnit\Framework\Attributes\DataProvider;
1516
use Tests\Keboola\TableBackendUtils\Functional\Bigquery\BigqueryBaseCase;
1617

1718
class BigquerySchemaReflectionTest extends BigqueryBaseCase
@@ -77,9 +78,7 @@ public function testListTablesOnNonExistingDatasetThrowException(): void
7778
$schemaRef->getTablesNames();
7879
}
7980

80-
/**
81-
* @dataProvider createTableTestFromDefinitionSqlProvider
82-
*/
81+
#[DataProvider('createTableTestFromDefinitionSqlProvider')]
8382
public function testGetCreateTableCommandFromDefinition(
8483
BigqueryTableDefinition $definition,
8584
string $expectedSql,
@@ -108,13 +107,13 @@ public function testGetCreateTableCommandFromDefinition(
108107
/**
109108
* @return \Generator<string, array{
110109
* definition: BigqueryTableDefinition,
111-
* query: string,
110+
* expectedSql: string,
112111
* createPrimaryKeys: bool
113112
* }>
114113
*/
115-
public function createTableTestFromDefinitionSqlProvider(): Generator
114+
public static function createTableTestFromDefinitionSqlProvider(): Generator
116115
{
117-
$testDb = $this->getDatasetName();
116+
$testDb = self::getDatasetName();
118117
$tableName = self::TABLE_GENERIC;
119118

120119
yield 'no keys' => [
@@ -130,7 +129,7 @@ public function createTableTestFromDefinitionSqlProvider(): Generator
130129
),
131130
[],
132131
),
133-
'query' => <<<EOT
132+
'expectedSql' => <<<EOT
134133
CREATE TABLE `$testDb`.`$tableName`
135134
(
136135
`col1` STRING DEFAULT '' NOT NULL,
@@ -154,7 +153,7 @@ public function createTableTestFromDefinitionSqlProvider(): Generator
154153
),
155154
['id'],
156155
),
157-
'query' => <<<EOT
156+
'expectedSql' => <<<EOT
158157
CREATE TABLE `$testDb`.`$tableName`
159158
(
160159
`id` STRING DEFAULT '' NOT NULL,
@@ -180,7 +179,7 @@ public function createTableTestFromDefinitionSqlProvider(): Generator
180179
),
181180
['id', 'type'],
182181
),
183-
'query' => <<<EOT
182+
'expectedSql' => <<<EOT
184183
CREATE TABLE `$testDb`.`$tableName`
185184
(
186185
`id` STRING DEFAULT '' NOT NULL,
@@ -206,7 +205,7 @@ public function createTableTestFromDefinitionSqlProvider(): Generator
206205
),
207206
['id'],
208207
),
209-
'query' => <<<EOT
208+
'expectedSql' => <<<EOT
210209
CREATE TABLE `$testDb`.`$tableName`
211210
(
212211
`id` STRING DEFAULT '' NOT NULL,

tests/Functional/Bigquery/Table/BigqueryTableQueryBuilderTest.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableQueryBuilder;
1515
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableReflection;
1616
use Keboola\TableBackendUtils\TableNotExistsReflectionException;
17+
use PHPUnit\Framework\Attributes\DataProvider;
1718
use Tests\Keboola\TableBackendUtils\Functional\Bigquery\BigqueryBaseCase;
1819

1920
class BigqueryTableQueryBuilderTest extends BigqueryBaseCase
@@ -30,11 +31,11 @@ public function setUp(): void
3031

3132
/**
3233
* @param BigqueryColumn[] $columns
33-
* @param string[] $primaryKeys
34-
* @param string[] $expectedColumnNames
35-
* @param string[] $expectedPKs
36-
* @dataProvider createTableTestSqlProvider
34+
* @param string[] $primaryKeys
35+
* @param string[] $expectedColumnNames
36+
* @param string[] $expectedPKs
3737
*/
38+
#[DataProvider('createTableTestSqlProvider')]
3839
public function testGetCreateCommand(
3940
array $columns,
4041
array $primaryKeys,
@@ -64,22 +65,22 @@ public function testGetCreateCommand(
6465
}
6566

6667
/**
67-
* @return \Generator<string, mixed, mixed, mixed>
68+
* @return \Generator<string, array<string, mixed>>
6869
*/
69-
public function createTableTestSqlProvider(): Generator
70+
public static function createTableTestSqlProvider(): Generator
7071
{
7172
$testDb = self::TEST_SCHEMA;
7273
$tableName = self::TABLE_GENERIC;
7374

7475
yield 'no keys' => [
75-
'cols' => [
76+
'columns' => [
7677
BigqueryColumn::createGenericColumn('col1'),
7778
BigqueryColumn::createGenericColumn('col2'),
7879
],
7980
'primaryKeys' => [],
8081
'expectedColumnNames' => ['col1', 'col2'],
81-
'expectedPrimaryKeys' => [],
82-
'query' => <<<EOT
82+
'expectedPKs' => [],
83+
'expectedSql' => <<<EOT
8384
CREATE TABLE `$testDb`.`$tableName`
8485
(
8586
`col1` STRING DEFAULT '' NOT NULL,
@@ -90,14 +91,14 @@ public function createTableTestSqlProvider(): Generator
9091
];
9192

9293
yield 'single primary key' => [
93-
'cols' => [
94+
'columns' => [
9495
BigqueryColumn::createGenericColumn('id'),
9596
BigqueryColumn::createGenericColumn('name'),
9697
],
9798
'primaryKeys' => ['id'],
9899
'expectedColumnNames' => ['id', 'name'],
99-
'expectedPrimaryKeys' => ['id'],
100-
'query' => <<<EOT
100+
'expectedPKs' => ['id'],
101+
'expectedSql' => <<<EOT
101102
CREATE TABLE `$testDb`.`$tableName`
102103
(
103104
`id` STRING DEFAULT '' NOT NULL,
@@ -109,15 +110,15 @@ public function createTableTestSqlProvider(): Generator
109110
];
110111

111112
yield 'composite primary key' => [
112-
'cols' => [
113+
'columns' => [
113114
BigqueryColumn::createGenericColumn('id'),
114115
BigqueryColumn::createGenericColumn('type'),
115116
BigqueryColumn::createGenericColumn('name'),
116117
],
117118
'primaryKeys' => ['id', 'type'],
118119
'expectedColumnNames' => ['id', 'type', 'name'],
119-
'expectedPrimaryKeys' => ['id', 'type'],
120-
'query' => <<<EOT
120+
'expectedPKs' => ['id', 'type'],
121+
'expectedSql' => <<<EOT
121122
CREATE TABLE `$testDb`.`$tableName`
122123
(
123124
`id` STRING DEFAULT '' NOT NULL,
@@ -191,9 +192,12 @@ public function testAddAndDropColumn(): void
191192
$sql = $this->qb->getAddColumnCommand(
192193
self::TEST_SCHEMA,
193194
self::TABLE_GENERIC,
194-
new BigqueryColumn('col3', new Bigquery(
195-
Bigquery::TYPE_STRING,
196-
)),
195+
new BigqueryColumn(
196+
'col3',
197+
new Bigquery(
198+
Bigquery::TYPE_STRING,
199+
),
200+
),
197201
);
198202
$this->assertEquals(
199203
sprintf(
@@ -214,11 +218,14 @@ public function testAddAndDropColumn(): void
214218

215219
// drop column
216220
$sql = $this->qb->getDropColumnCommand(self::TEST_SCHEMA, self::TABLE_GENERIC, 'col2');
217-
$this->assertEquals(sprintf(
218-
'ALTER TABLE `%s`.`%s` DROP COLUMN `col2`',
219-
self::TEST_SCHEMA,
220-
self::TABLE_GENERIC,
221-
), $sql);
221+
$this->assertEquals(
222+
sprintf(
223+
'ALTER TABLE `%s`.`%s` DROP COLUMN `col2`',
224+
self::TEST_SCHEMA,
225+
self::TABLE_GENERIC,
226+
),
227+
$sql,
228+
);
222229
$this->bqClient->runQuery($this->bqClient->query($sql));
223230

224231
$tableReflection = new BigqueryTableReflection(

tests/Functional/Bigquery/Table/BigqueryTableReflectionTest.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Keboola\TableBackendUtils\Table\Bigquery\TimePartitioningConfig;
1818
use Keboola\TableBackendUtils\Table\TableStats;
1919
use Keboola\TableBackendUtils\TableNotExistsReflectionException;
20+
use PHPUnit\Framework\Attributes\DataProvider;
2021
use Tests\Keboola\TableBackendUtils\Functional\Bigquery\BigqueryBaseCase;
2122

2223
class BigqueryTableReflectionTest extends BigqueryBaseCase
@@ -32,16 +33,17 @@ public function testGetTableColumnsNames(): void
3233
$this->initTable();
3334
$ref = new BigqueryTableReflection($this->bqClient, self::TEST_SCHEMA, self::TABLE_GENERIC);
3435

35-
self::assertSame([
36+
self::assertSame(
37+
[
3638
'id',
3739
'first_name',
3840
'last_name',
39-
], $ref->getColumnsNames());
41+
],
42+
$ref->getColumnsNames(),
43+
);
4044
}
4145

42-
/**
43-
* @dataProvider tableColsDataProvider
44-
*/
46+
#[DataProvider('tableColsDataProvider')]
4547
public function testColumnDefinition(
4648
string $sqlDef,
4749
string $expectedSqlDefinition,
@@ -79,14 +81,15 @@ public function testColumnDefinition(
7981
self::assertEquals($expectedSqlDefinition, $definition->getSQLDefinition(), 'SQL definition doesnt match');
8082

8183
// check that SQLtoRestDatatypeConverter returns same definition as BQ REST API
84+
/** @var array<string, mixed> $tableInfo */
8285
$tableInfo = $this->bqClient
8386
->dataset(self::TEST_SCHEMA)
8487
->table(self::TABLE_GENERIC)
8588
->info();
86-
// @phpstan-ignore offsetAccess.nonOffsetAccessible, offsetAccess.nonOffsetAccessible
87-
$fieldInfo = $tableInfo['schema']['fields'][1];
89+
/** @var array{fields: array<int, array{name: string, type: string, mode?: 'NULLABLE'|'REPEATED'|'REQUIRED', fields?: array<mixed>, description?: string, maxLength?: string, precision?: string, scale?: string}>} $schema */
90+
$schema = $tableInfo['schema'];
8891
self::assertEquals(
89-
RESTtoSQLDatatypeConverter::convertColumnToSQLFormat($fieldInfo), // @phpstan-ignore argument.type
92+
RESTtoSQLDatatypeConverter::convertColumnToSQLFormat($schema['fields'][1]),
9093
RESTtoSQLDatatypeConverter::convertColumnToSQLFormat(
9194
SQLtoRestDatatypeConverter::convertColumnToRestFormat($column),
9295
),
@@ -96,7 +99,7 @@ public function testColumnDefinition(
9699
/**
97100
* @return Generator<string,array<mixed>>
98101
*/
99-
public function tableColsDataProvider(): Generator
102+
public static function tableColsDataProvider(): Generator
100103
{
101104
yield 'ARRAY simple' => [
102105
'ARRAY<INT64>',
@@ -453,7 +456,7 @@ public function testTableWithPartitioning(): void
453456
BigqueryQuote::quoteSingleIdentifier(self::TEST_SCHEMA),
454457
BigqueryQuote::quoteSingleIdentifier('test-partitions'),
455458
implode(',', $rows),
456-
)));
459+
),),);
457460
$ref = new BigqueryTableReflection($this->bqClient, self::TEST_SCHEMA, 'test-partitions');
458461
$this->assertCount(1, $ref->getPartitionsList());
459462
}
@@ -488,14 +491,14 @@ public function testTableWithoutPartitioning(): void
488491
SQL,
489492
BigqueryQuote::quoteSingleIdentifier(self::TEST_SCHEMA),
490493
BigqueryQuote::quoteSingleIdentifier('test-partitions'),
491-
)));
494+
),),);
492495
$ref = new BigqueryTableReflection($this->bqClient, self::TEST_SCHEMA, 'test-partitions');
493496
// expect none partition returned by reflection class
494497
// there is one unnamed partition even when no partitioning is set and we ignore it
495498
$this->assertCount(0, $ref->getPartitionsList());
496499
}
497500

498-
public function partitioningProvider(): Generator
501+
public static function partitioningProvider(): Generator
499502
{
500503
yield 'Time simple' => [
501504
'config' => [
@@ -597,8 +600,8 @@ public function partitioningProvider(): Generator
597600

598601
/**
599602
* @param array<mixed> $config
600-
* @dataProvider partitioningProvider
601603
*/
604+
#[DataProvider('partitioningProvider')]
602605
public function testTableWithPartitioningConfig(
603606
array $config,
604607
?PartitioningConfig $expectedPartitioning,

0 commit comments

Comments
 (0)