From dc79df1cb14a9d252dc422e2dc24d84e48a6cccf Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Tue, 27 Oct 2026 14:16:33 +0000 Subject: [PATCH 1/3] Allow packaged/helpers 2 and PHP 8.4+ nullable types The library calls nothing from packaged/helpers, but its ~1.0 constraint stopped any project on helpers 2 from installing it. writeRows() and BigQueryTimeoutException declare their nullable parameters explicitly, which PHP 8.4 deprecates leaving implicit. Co-Authored-By: Claude Opus 5.5 --- composer.json | 2 +- src/BigQueryHelper.php | 4 ++-- src/BigQueryTimeoutException.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 54eb185..71dcc26 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "library", "require": { "google/cloud-bigquery": "~1.2", - "packaged/helpers": "~1.0" + "packaged/helpers": "~1.0 || ^2.0" }, "license": "MIT", "authors": [ diff --git a/src/BigQueryHelper.php b/src/BigQueryHelper.php index fca17ad..dec93a5 100644 --- a/src/BigQueryHelper.php +++ b/src/BigQueryHelper.php @@ -489,8 +489,8 @@ protected function _groupRowsByTable(array $rows) * @throws ConflictException */ public function writeRows( - array $items, $useTemplatedTables = false, $autoCreateTables = false, callable $onError = null, - callable $onSuccess = null + array $items, $useTemplatedTables = false, $autoCreateTables = false, ?callable $onError = null, + ?callable $onSuccess = null ) { $groupedItems = $this->_groupRowsByTable($items); diff --git a/src/BigQueryTimeoutException.php b/src/BigQueryTimeoutException.php index 14d5f1b..9d8ecfb 100644 --- a/src/BigQueryTimeoutException.php +++ b/src/BigQueryTimeoutException.php @@ -11,7 +11,7 @@ class BigQueryTimeoutException extends BigQueryException protected $_query; public function __construct( - $message = "", $code = 0, Throwable $previous = null, $query = "", $projectId = "", $jobId = "", $location = "" + $message = "", $code = 0, ?Throwable $previous = null, $query = "", $projectId = "", $jobId = "", $location = "" ) { parent::__construct( From 0b3761d6ae33f65c26c19329056e244c895793cf Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Tue, 27 Oct 2026 14:19:32 +0000 Subject: [PATCH 2/3] Drop the unused packaged/helpers requirement Neither the library nor any of its dependencies references packaged/helpers. Co-Authored-By: Claude Opus 5.5 --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 71dcc26..25a2c29 100644 --- a/composer.json +++ b/composer.json @@ -3,8 +3,7 @@ "description": "Helper code for Google BigQuery", "type": "library", "require": { - "google/cloud-bigquery": "~1.2", - "packaged/helpers": "~1.0 || ^2.0" + "google/cloud-bigquery": "~1.2" }, "license": "MIT", "authors": [ From 97800c502ab1cca98b0add0bec9b65cbf5387010 Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Tue, 27 Oct 2026 14:22:28 +0000 Subject: [PATCH 3/3] Add PHPUnit tests and GitHub Actions CI CI runs the tests on PHP 7.4 and 8.2 to 8.5. The tests cover credential loading, table name handling, row serialisation for every field type, batching by request size, row error messages and BigQueryTimeoutException, without network access. On 7.4, install skips advisory blocking because every google/auth release for 7.4 needs an affected firebase/php-jwt. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 40 ++++ composer.json | 8 + phpunit.xml | 19 ++ tests/BigQueryHelperTest.php | 282 +++++++++++++++++++++++++ tests/BigQueryTimeoutExceptionTest.php | 32 +++ 5 files changed, 381 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 phpunit.xml create mode 100644 tests/BigQueryHelperTest.php create mode 100644 tests/BigQueryTimeoutExceptionTest.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..8137149 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,40 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + php-version: + - "7.4" + - "8.2" + - "8.3" + - "8.4" + - "8.5" + + steps: + - name: Checkout repository + uses: actions/checkout@v7 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + coverage: none + + # Every google/auth release that supports PHP 7.4 needs a firebase/php-jwt version with a security advisory. + - name: Composer install + run: composer install ${{ matrix.php-version == '7.4' && '--no-security-blocking' || '' }} + + - name: PHPUnit + run: vendor/bin/phpunit diff --git a/composer.json b/composer.json index 25a2c29..7b897bd 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,9 @@ "require": { "google/cloud-bigquery": "~1.2" }, + "require-dev": { + "phpunit/phpunit": "^9.6" + }, "license": "MIT", "authors": [ { @@ -16,5 +19,10 @@ "psr-4": { "Packaged\\BigQuery\\": "src/" } + }, + "autoload-dev": { + "psr-4": { + "Packaged\\BigQuery\\Tests\\": "tests/" + } } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..488f1ea --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,19 @@ + + + + + tests + + + + + src + + + diff --git a/tests/BigQueryHelperTest.php b/tests/BigQueryHelperTest.php new file mode 100644 index 0000000..d63aaf2 --- /dev/null +++ b/tests/BigQueryHelperTest.php @@ -0,0 +1,282 @@ + 'service_account', 'project_id' => 'test-project', 'client_email' => 'a@b.c']; + + private function _helper($dataSet = 'test_ds'): BigQueryHelper + { + return new BigQueryHelper(self::CREDENTIALS, $dataSet); + } + + /** + * Calls a private or protected method on the helper + */ + private function _call(BigQueryHelper $helper, string $method, ...$args) + { + $reflection = new \ReflectionMethod($helper, $method); + if(PHP_VERSION_ID < 80100) + { + $reflection->setAccessible(true); + } + return $reflection->invoke($helper, ...$args); + } + + private function _writeable(string $table, string $insertId, array $data, array $schema, $rowTimestamp = null) + { + return new TestWriteable($table, $insertId, $data, $schema, $rowTimestamp); + } + + public function testCredentialsFromArrayJsonAndFile() + { + $this->assertSame('test-project', (new BigQueryHelper(self::CREDENTIALS))->gcpProjectName()); + $this->assertSame('test-project', (new BigQueryHelper(json_encode(self::CREDENTIALS)))->gcpProjectName()); + + $file = tempnam(sys_get_temp_dir(), 'bq'); + file_put_contents($file, json_encode(self::CREDENTIALS)); + try + { + $this->assertSame('test-project', (new BigQueryHelper($file))->gcpProjectName()); + } + finally + { + unlink($file); + } + } + + public function invalidCredentialsProvider() + { + return [ + 'null' => [null, 'Invalid credentials provided'], + 'invalid json' => ['{not json', 'The provided credentials are not in valid JSON format'], + 'missing project id' => [['type' => 'service_account'], 'Invalid credentials provided'], + 'directory' => [__DIR__, 'The specified credentials file is not a file'], + ]; + } + + /** + * @dataProvider invalidCredentialsProvider + */ + public function testInvalidCredentialsThrow($credentials, string $message) + { + $this->expectException(BigQueryException::class); + $this->expectExceptionMessage($message); + new BigQueryHelper($credentials); + } + + public function testClientIsCreatedOnce() + { + $helper = $this->_helper(); + $client = $helper->getClient(); + $this->assertInstanceOf(BigQueryClient::class, $client); + $this->assertSame($client, $helper->getClient()); + } + + public function testDataSetName() + { + $helper = $this->_helper(); + $this->assertSame('test_ds', $helper->getDataSetName()); + $this->assertSame($helper, $helper->setDataSetName('other')); + $this->assertSame('other', $helper->getDataSetName()); + } + + public function testTrimDatasetFromTableName() + { + $helper = $this->_helper(); + $this->assertSame('events', $this->_call($helper, '_trimDatasetFromTableName', 'events')); + $this->assertSame('events', $this->_call($helper, '_trimDatasetFromTableName', 'test_ds.events')); + + $this->expectException(BigQueryException::class); + $this->expectExceptionMessage('Incorrect dataset in table name: other.events. Expected dataset test_ds'); + $this->_call($helper, '_trimDatasetFromTableName', 'other.events'); + } + + public function testSplitTableNameForTemplate() + { + $helper = $this->_helper(); + $this->assertSame( + ['events_20260101', 'events', '_20260101'], + $this->_call($helper, '_splitTableNameForTemplate', 'events_20260101') + ); + + $this->expectException(BigQueryException::class); + $this->expectExceptionMessage('Table name not suitable for using template: events'); + $this->_call($helper, '_splitTableNameForTemplate', 'events'); + } + + public function testWriteablesToRowsSerialisesEveryType() + { + $schema = [ + ['name' => 'int', 'type' => BigQueryType::INTEGER], + ['name' => 'ts', 'type' => BigQueryType::TIMESTAMP], + ['name' => 'float', 'type' => BigQueryType::FLOAT], + ['name' => 'bool', 'type' => BigQueryType::BOOLEAN], + ['name' => 'str', 'type' => BigQueryType::STRING], + ['name' => 'list', 'type' => BigQueryType::STRING], + ['name' => 'bytes', 'type' => BigQueryType::BYTES], + ['name' => 'missing', 'type' => BigQueryType::STRING], + ['name' => 'rowTimestamp', 'type' => BigQueryType::TIMESTAMP], + ]; + $data = ['int' => '5', 'ts' => 1700000000.9, 'float' => '1.5', 'bool' => 1, 'str' => 42, 'list' => ['a', 'b'], + 'bytes' => 'raw', 'rowTimestamp' => 'ignored']; + + $rows = $this->_call( + $this->_helper(), + '_writeablesToRows', + ['tag' => $this->_writeable('events', 'id1', $data, $schema, 1700000001)] + ); + + $this->assertSame( + [ + 'tag' => [ + 'insertId' => 'id1', + 'data' => [ + 'int' => 5, + 'ts' => 1700000000, + 'float' => 1.5, + 'bool' => true, + 'str' => '42', + 'list' => 'a, b', + 'bytes' => 'cmF3', + 'missing' => '', + 'rowTimestamp' => 1700000001, + ], + ], + ], + $rows + ); + } + + public function testRowTimestampOmittedWhenEmpty() + { + $schema = [['name' => 'a', 'type' => BigQueryType::STRING], ['name' => 'rowTimestamp', 'type' => BigQueryType::TIMESTAMP]]; + $rows = $this->_call( + $this->_helper(), + '_writeablesToRows', + [$this->_writeable('events', 'id1', ['a' => 'x'], $schema)] + ); + $this->assertSame(['a' => 'x'], $rows[0]['data']); + } + + public function invalidFieldProvider() + { + return [ + 'non numeric integer' => [BigQueryType::INTEGER, 'abc', "Invalid data 'abc' for integer field `f`"], + 'array timestamp' => [BigQueryType::TIMESTAMP, ['x'], "Invalid data 'array' for timestamp field `f`"], + 'non numeric float' => [BigQueryType::FLOAT, '', "Invalid data '' for float field `f`"], + 'unknown type' => [BigQueryType::RECORD, 'x', "Unable to serialize data for BigQuery: Unknown type 'record' for field `f`"], + ]; + } + + /** + * @dataProvider invalidFieldProvider + */ + public function testInvalidFieldThrows(string $type, $value, string $message) + { + $this->expectException(BigQueryException::class); + $this->expectExceptionMessage($message); + $this->_call($this->_helper(), '_serializeField', $value, $type, 'f'); + } + + public function testGroupRowsByTableKeepsTags() + { + $schema = [['name' => 'a', 'type' => BigQueryType::STRING]]; + $first = $this->_writeable('one', 'i1', [], $schema); + $second = $this->_writeable('two', 'i2', [], $schema); + $third = $this->_writeable('one', 'i3', [], $schema); + + $this->assertSame( + ['one' => ['x' => $first, 'z' => $third], 'two' => ['y' => $second]], + $this->_call($this->_helper(), '_groupRowsByTable', ['x' => $first, 'y' => $second, 'z' => $third]) + ); + } + + public function testBatchRowsBySizeSplitsLargeRequests() + { + $helper = $this->_helper(); + $small = [['data' => 'a'], ['data' => 'b']]; + $this->assertSame([$small], $this->_call($helper, '_batchRowsBySize', $small)); + + $row = ['data' => str_repeat('x', (int)(BigQueryHelper::MAX_REQUEST_DATA_SIZE * 0.4))]; + $batches = $this->_call($helper, '_batchRowsBySize', [$row, $row, $row, $row, $row]); + $this->assertSame([2, 2, 1], array_map('count', $batches)); + foreach($batches as $batch) + { + $this->assertLessThanOrEqual(BigQueryHelper::MAX_REQUEST_DATA_SIZE, strlen(json_encode($batch))); + } + } + + public function testRowErrorsMessage() + { + $this->assertSame( + 'Index 3: [reason: invalid, location: a, message: bad a], [reason: stopped, location: b, message: bad b]', + $this->_call( + $this->_helper(), + '_makeRowErrorsMsg', + [ + 'index' => 3, + 'errors' => [ + ['reason' => 'invalid', 'location' => 'a', 'message' => 'bad a'], + ['reason' => 'stopped', 'location' => 'b', 'message' => 'bad b'], + ], + ] + ) + ); + } + + public function testWriteRowsWithNoItemsMakesNoRequests() + { + $this->assertSame([], $this->_helper()->writeRows([])); + } +} + +class TestWriteable implements IBigQueryWriteable +{ + private $_table; + private $_insertId; + private $_data; + private $_schema; + private $_rowTimestamp; + + public function __construct(string $table, string $insertId, array $data, array $schema, $rowTimestamp) + { + $this->_table = $table; + $this->_insertId = $insertId; + $this->_data = $data; + $this->_schema = $schema; + $this->_rowTimestamp = $rowTimestamp; + } + + public function getBigQueryTableName() + { + return $this->_table; + } + + public function getBigQuerySchema() + { + return $this->_schema; + } + + public function getBigQueryData() + { + return $this->_data; + } + + public function getBigQueryInsertId() + { + return $this->_insertId; + } + + public function getBigQueryRowTimestamp() + { + return $this->_rowTimestamp; + } +} diff --git a/tests/BigQueryTimeoutExceptionTest.php b/tests/BigQueryTimeoutExceptionTest.php new file mode 100644 index 0000000..61cc018 --- /dev/null +++ b/tests/BigQueryTimeoutExceptionTest.php @@ -0,0 +1,32 @@ +assertInstanceOf(BigQueryException::class, $e); + $this->assertSame('timed out', $e->getMessage()); + $this->assertSame(7, $e->getCode()); + $this->assertSame($previous, $e->getPrevious()); + $this->assertSame('SELECT 1', $e->getQuery()); + $this->assertSame('project', $e->getProjectId()); + $this->assertSame('job', $e->getJobId()); + $this->assertSame('EU', $e->getLocation()); + } + + public function testDefaults() + { + $e = new BigQueryTimeoutException(); + $this->assertNull($e->getPrevious()); + $this->assertSame('', $e->getQuery()); + $this->assertSame('', $e->getJobId()); + } +}