Skip to content

Commit

Permalink
Run PHPCS on tests (#1192)
Browse files Browse the repository at this point in the history
This change updates the PHPCS configuration to enforce coding standards on all test files (unit, snippet and system tests). It continues to exclude generated code.

It also includes a sweep of test cleanup to conform to standards and modernizes some very old test code. This change shouldn't include changes to actual client code.
  • Loading branch information
jdpedrie committed Jul 26, 2018
1 parent fce404e commit df77513
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 59 deletions.
18 changes: 12 additions & 6 deletions tests/Snippet/BigQueryClientTest.php
Expand Up @@ -19,19 +19,20 @@

use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\BigQuery\Bytes;
use Google\Cloud\BigQuery\Date;
use Google\Cloud\BigQuery\Numeric;
use Google\Cloud\BigQuery\Time;
use Google\Cloud\BigQuery\Timestamp;
use Google\Cloud\BigQuery\Connection\ConnectionInterface;
use Google\Cloud\BigQuery\Dataset;
use Google\Cloud\BigQuery\Date;
use Google\Cloud\BigQuery\Job;
use Google\Cloud\BigQuery\Numeric;
use Google\Cloud\BigQuery\QueryJobConfiguration;
use Google\Cloud\BigQuery\QueryResults;
use Google\Cloud\BigQuery\Time;
use Google\Cloud\BigQuery\Timestamp;
use Google\Cloud\BigQuery\ValueMapper;
use Google\Cloud\Core\Int64;
use Google\Cloud\Core\Iterator\ItemIterator;
use Google\Cloud\Core\Testing\Snippet\SnippetTestCase;
use Google\Cloud\Core\Testing\TestHelpers;
use Prophecy\Argument;

/**
Expand Down Expand Up @@ -71,7 +72,7 @@ class BigQueryClientTest extends SnippetTestCase
public function setUp()
{
$this->connection = $this->prophesize(ConnectionInterface::class);
$this->client = \Google\Cloud\Core\Testing\TestHelpers::stub(BigQueryTestClient::class);
$this->client = TestHelpers::stub(BigQueryTestClient::class);
$this->client->___setProperty('connection', $this->connection->reveal());
}

Expand Down Expand Up @@ -132,7 +133,10 @@ public function testQueryWithLocation()
$array = $config->toArray();

$this->assertInstanceOf(QueryJobConfiguration::class, $config);
$this->assertEquals('SELECT name FROM `my_project.users_dataset.users` LIMIT 100', $array['configuration']['query']['query']);
$this->assertEquals(
'SELECT name FROM `my_project.users_dataset.users` LIMIT 100',
$array['configuration']['query']['query']
);
$this->assertEquals('asia-northeast1', $array['jobReference']['location']);
}

Expand Down Expand Up @@ -442,6 +446,7 @@ public function testGetServiceAccount()
}
}

//@codingStandardsIgnoreStart
class BigQueryTestClient extends BigQueryClient
{
public function query($query, array $options = [])
Expand All @@ -462,3 +467,4 @@ protected function generateJobId()
return BigQueryClientTest::JOB_ID;
}
}
//@codingStandardsIgnoreEnd
3 changes: 2 additions & 1 deletion tests/Snippet/CopyJobConfigurationTest.php
Expand Up @@ -20,6 +20,7 @@
use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\BigQuery\CopyJobConfiguration;
use Google\Cloud\Core\Testing\Snippet\SnippetTestCase;
use Google\Cloud\Core\Testing\TestHelpers;

/**
* @group bigquery
Expand Down Expand Up @@ -71,7 +72,7 @@ public function testSetters($method, $expected, $bq = null)

public function setterDataProvider()
{
$bq = \Google\Cloud\Core\Testing\TestHelpers::stub(BigQueryClient::class, [
$bq = TestHelpers::stub(BigQueryClient::class, [
['projectId' => self::PROJECT_ID]
]);

Expand Down
3 changes: 2 additions & 1 deletion tests/Snippet/ExtractJobConfigurationTest.php
Expand Up @@ -21,6 +21,7 @@
use Google\Cloud\BigQuery\ExtractJobConfiguration;
use Google\Cloud\BigQuery\Table;
use Google\Cloud\Core\Testing\Snippet\SnippetTestCase;
use Google\Cloud\Core\Testing\TestHelpers;

/**
* @group bigquery
Expand Down Expand Up @@ -72,7 +73,7 @@ public function testSetters($method, $expected, $bq = null)

public function setterDataProvider()
{
$bq = \Google\Cloud\Core\Testing\TestHelpers::stub(BigQueryClient::class, [
$bq = TestHelpers::stub(BigQueryClient::class, [
['projectId' => self::PROJECT_ID]
]);

Expand Down
3 changes: 2 additions & 1 deletion tests/Snippet/LoadJobConfigurationTest.php
Expand Up @@ -20,6 +20,7 @@
use Google\Cloud\BigQuery\BigQueryClient;
use Google\Cloud\BigQuery\LoadJobConfiguration;
use Google\Cloud\Core\Testing\Snippet\SnippetTestCase;
use Google\Cloud\Core\Testing\TestHelpers;

/**
* @group bigquery
Expand Down Expand Up @@ -82,7 +83,7 @@ public function testSetters($method, $expected, $bq = null)

public function setterDataProvider()
{
$bq = \Google\Cloud\Core\Testing\TestHelpers::stub(BigQueryClient::class, [
$bq = TestHelpers::stub(BigQueryClient::class, [
['projectId' => self::PROJECT_ID]
]);

Expand Down
3 changes: 2 additions & 1 deletion tests/Snippet/QueryJobConfigurationTest.php
Expand Up @@ -21,6 +21,7 @@
use Google\Cloud\BigQuery\QueryJobConfiguration;
use Google\Cloud\BigQuery\ValueMapper;
use Google\Cloud\Core\Testing\Snippet\SnippetTestCase;
use Google\Cloud\Core\Testing\TestHelpers;

/**
* @group bigquery
Expand Down Expand Up @@ -73,7 +74,7 @@ public function testSetters($method, $expected, $bq = null)

public function setterDataProvider()
{
$bq = \Google\Cloud\Core\Testing\TestHelpers::stub(BigQueryClient::class, [
$bq = TestHelpers::stub(BigQueryClient::class, [
['projectId' => self::PROJECT_ID]
]);

Expand Down
3 changes: 2 additions & 1 deletion tests/Snippet/QueryResultsTest.php
Expand Up @@ -22,6 +22,7 @@
use Google\Cloud\BigQuery\QueryResults;
use Google\Cloud\BigQuery\ValueMapper;
use Google\Cloud\Core\Testing\Snippet\SnippetTestCase;
use Google\Cloud\Core\Testing\TestHelpers;
use Prophecy\Argument;

/**
Expand Down Expand Up @@ -63,7 +64,7 @@ public function setUp()
];

$this->connection = $this->prophesize(ConnectionInterface::class);
$this->qr = \Google\Cloud\Core\Testing\TestHelpers::stub(QueryResults::class, [
$this->qr = TestHelpers::stub(QueryResults::class, [
$this->connection->reveal(),
self::JOB_ID,
self::PROJECT,
Expand Down
21 changes: 14 additions & 7 deletions tests/Snippet/TableTest.php
Expand Up @@ -28,8 +28,9 @@
use Google\Cloud\BigQuery\Table;
use Google\Cloud\BigQuery\ValueMapper;
use Google\Cloud\Core\Iterator\ItemIterator;
use Google\Cloud\Core\Upload\MultipartUploader;
use Google\Cloud\Core\Testing\Snippet\SnippetTestCase;
use Google\Cloud\Core\Testing\TestHelpers;
use Google\Cloud\Core\Upload\MultipartUploader;
use Google\Cloud\Storage\Connection\Rest as StorageConnection;
use Google\Cloud\Storage\StorageClient;
use Prophecy\Argument;
Expand Down Expand Up @@ -73,7 +74,7 @@ public function setUp()

$this->mapper = new ValueMapper(false);
$this->connection = $this->prophesize(ConnectionInterface::class);
$this->table = \Google\Cloud\Core\Testing\TestHelpers::stub(Table::class, [
$this->table = TestHelpers::stub(Table::class, [
$this->connection->reveal(),
self::ID,
self::DSID,
Expand Down Expand Up @@ -202,7 +203,7 @@ public function testStartJob()

public function testCopy()
{
$bq = \Google\Cloud\Core\Testing\TestHelpers::stub(BigQueryClient::class);
$bq = TestHelpers::stub(BigQueryClient::class);
$snippet = $this->snippetFromMethod(Table::class, 'copy');
$snippet->addLocal('bigQuery', $bq);
$bq->___setProperty('connection', $this->connection->reveal());
Expand All @@ -229,7 +230,7 @@ public function testCopy()

public function testExtract()
{
$storage = \Google\Cloud\Core\Testing\TestHelpers::stub(StorageClient::class);
$storage = TestHelpers::stub(StorageClient::class);
$storage->___setProperty('connection', $this->prophesize(StorageConnection::class)->reveal());
$snippet = $this->snippetFromMethod(Table::class, 'extract');
$snippet->addLocal('storage', $storage);
Expand Down Expand Up @@ -274,7 +275,7 @@ public function testLoad()

public function testLoadFromStorage()
{
$storage = \Google\Cloud\Core\Testing\TestHelpers::stub(StorageClient::class);
$storage = TestHelpers::stub(StorageClient::class);
$storage->___setProperty('connection', $this->prophesize(StorageConnection::class)->reveal());
$snippet = $this->snippetFromMethod(Table::class, 'loadFromStorage');
$snippet->addLocal('storage', $storage);
Expand Down Expand Up @@ -336,7 +337,10 @@ public function testInfo()
$snippet->addLocal('table', $this->table);

$res = $snippet->invoke();
$this->assertEquals('https://www.googleapis.com/bigquery/v2/projects/my-project/datasets/mynewdataset', $res->output());
$this->assertEquals(
'https://www.googleapis.com/bigquery/v2/projects/my-project/datasets/mynewdataset',
$res->output()
);
}

public function testReload()
Expand All @@ -353,7 +357,10 @@ public function testReload()
$this->table->___setProperty('connection', $this->connection->reveal());

$res = $snippet->invoke();
$this->assertEquals('https://www.googleapis.com/bigquery/v2/projects/my-project/datasets/myupdateddataset', $res->output());
$this->assertEquals(
'https://www.googleapis.com/bigquery/v2/projects/my-project/datasets/myupdateddataset',
$res->output()
);
}

public function testId()
Expand Down
6 changes: 1 addition & 5 deletions tests/System/BigQueryTestCase.php
Expand Up @@ -59,12 +59,8 @@ public static function setUpBeforeClass()
protected static function createTable(Dataset $dataset, $name, array $options = [])
{
if (!isset($options['schema'])) {
$options['schema']['fields'] = json_decode(
file_get_contents(__DIR__ . '/data/table-schema.json'), true
);
$options['schema']['fields'] = json_decode(file_get_contents(__DIR__ . '/data/table-schema.json'), true);
}
return $dataset->createTable(uniqid(self::TESTING_PREFIX), $options);
}
}


22 changes: 9 additions & 13 deletions tests/Unit/BigQueryClientTest.php
Expand Up @@ -323,20 +323,16 @@ public function testGetsDatasetsWithToken()
{
$client = $this->getClient();
$this->connection->listDatasets(Argument::any())
->willReturn(
[
'nextPageToken' => 'token',
'datasets' => [
['datasetReference' => ['datasetId' => 'someOtherdatasetId']]
]
],
[
'datasets' => [
['datasetReference' => ['datasetId' => self::DATASET_ID]]
]
->willReturn([
'nextPageToken' => 'token',
'datasets' => [
['datasetReference' => ['datasetId' => 'someOtherdatasetId']]
]
], [
'datasets' => [
['datasetReference' => ['datasetId' => self::DATASET_ID]]
]
)
->shouldBeCalledTimes(2);
])->shouldBeCalledTimes(2);

$client->___setProperty('connection', $this->connection->reveal());
$dataset = iterator_to_array($client->datasets());
Expand Down
22 changes: 9 additions & 13 deletions tests/Unit/DatasetTest.php
Expand Up @@ -146,20 +146,16 @@ public function testGetsTablesWithoutToken()
public function testGetsTablesWithToken()
{
$this->connection->listTables(Argument::any())
->willReturn(
[
'nextPageToken' => 'token',
'tables' => [
['tableReference' => ['tableId' => 'someOthertableId']]
]
],
[
'tables' => [
['tableReference' => ['tableId' => $this->tableId]]
]
->willReturn([
'nextPageToken' => 'token',
'tables' => [
['tableReference' => ['tableId' => 'someOthertableId']]
]
], [
'tables' => [
['tableReference' => ['tableId' => $this->tableId]]
]
)
->shouldBeCalledTimes(2);
])->shouldBeCalledTimes(2);

$dataset = $this->getDataset($this->connection);
$tables = iterator_to_array($dataset->tables());
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/JobConfigurationTraitTest.php
Expand Up @@ -18,8 +18,9 @@
namespace Google\Cloud\BigQuery\Tests\Unit;

use Google\Cloud\BigQuery\JobConfigurationTrait;
use Ramsey\Uuid\Uuid;
use Google\Cloud\Core\Testing\TestHelpers;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;

/**
* @group bigquery
Expand All @@ -34,7 +35,7 @@ class JobConfigurationTraitTest extends TestCase

public function setUp()
{
$this->trait = \Google\Cloud\Core\Testing\TestHelpers::impl(JobConfigurationTrait::class);
$this->trait = TestHelpers::impl(JobConfigurationTrait::class);
}

public function testJobConfigurationProperties()
Expand Down
19 changes: 12 additions & 7 deletions tests/Unit/JobWaitTraitTest.php
Expand Up @@ -19,6 +19,7 @@

use Google\Cloud\BigQuery\Job;
use Google\Cloud\BigQuery\JobWaitTrait;
use Google\Cloud\Core\Testing\TestHelpers;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -31,7 +32,7 @@ class JobWaitTraitTest extends TestCase

public function setUp()
{
$this->trait = \Google\Cloud\Core\Testing\TestHelpers::impl(JobWaitTrait::class);
$this->trait = TestHelpers::impl(JobWaitTrait::class);
$this->job = $this->prophesize(Job::class)->reveal();
}

Expand All @@ -41,11 +42,11 @@ public function testWaitSucceedsWhenAlreadyComplete()
$isReloadCalled = false;

$this->trait->call('wait', [
function() use (&$isCompleteCalled) {
function () use (&$isCompleteCalled) {
$isCompleteCalled = true;
return true;
},
function() use (&$isReloadCalled) {
function () use (&$isReloadCalled) {
$isReloadCalled = true;
return ['complete' => true];
},
Expand All @@ -63,11 +64,11 @@ public function testWaitCallsReloadThenSucceeds()
$isReloadCalled = false;

$this->trait->call('wait', [
function() use (&$isCompleteCallCount, &$isReloadCalled) {
function () use (&$isCompleteCallCount, &$isReloadCalled) {
$isCompleteCallCount++;
return $isReloadCalled ? true : false;
},
function() use (&$isReloadCalled) {
function () use (&$isReloadCalled) {
$isReloadCalled = true;
return ['complete' => true];
},
Expand All @@ -86,8 +87,12 @@ function() use (&$isReloadCalled) {
public function testWaitThrowsExceptionWhenMaxAttemptsMet()
{
$this->trait->call('wait', [
function() { return false; },
function () { return ['complete' => false]; },
function () {
return false;
},
function () {
return ['complete' => false];
},
$this->job,
1
]);
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/TableTest.php
Expand Up @@ -678,10 +678,12 @@ public function testGetsIdentity()
}
}

//@codingStandardsIgnoreStart
class TableStub extends Table
{
protected function usleep($ms)
{
return;
}
}
//@codingStandardsIgnoreEnd
2 changes: 1 addition & 1 deletion tests/Unit/ValueMapperTest.php
Expand Up @@ -45,7 +45,7 @@ public function testThrowsExceptionWithUnhandledClass()
*/
public function testToParameterThrowsExceptionWithUnhandledType()
{
$f = fopen('php://temp','r');
$f = fopen('php://temp', 'r');
fclose($f);
$mapper = new ValueMapper(false);
$mapper->toParameter($f);
Expand Down

0 comments on commit df77513

Please sign in to comment.