Skip to content

Commit

Permalink
fix: use exception::class instead of strings (#6051)
Browse files Browse the repository at this point in the history
fix: rename exceptions in test and phpDoc
  • Loading branch information
vishwarajanand authored Apr 10, 2023
1 parent 7836392 commit 106abbb
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 66 deletions.
8 changes: 4 additions & 4 deletions src/GeoPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class GeoPoint implements \JsonSerializable
* in the constructor only. This switch exists to handle a rare case
* wherein a geopoint may be empty and is not intended for use from
* outside the client. **Defaults to** `false`.
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function __construct($latitude, $longitude, $allowNull = false)
{
Expand Down Expand Up @@ -97,7 +97,7 @@ public function latitude()
*
* @param int|float $latitude The new latitude
* @return GeoPoint
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function setLatitude($latitude)
{
Expand Down Expand Up @@ -134,7 +134,7 @@ public function longitude()
*
* @param float|int $longitude The new longitude value
* @return GeoPoint
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function setLongitude($longitude)
{
Expand Down Expand Up @@ -166,7 +166,7 @@ public function point()
*
* @param string $method the method name
* @param array $args The method arguments
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
* @return void
*/
private function checkContext($method, array $args)
Expand Down
8 changes: 4 additions & 4 deletions src/Iam/PolicyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class PolicyBuilder
* ```
*
* @param array $policy A policy array
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function __construct(array $policy = [])
{
Expand Down Expand Up @@ -125,7 +125,7 @@ public function __construct(array $policy = [])
*
* @param array $bindings [optional] An array of bindings
* @return PolicyBuilder
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
*/
public function setBindings(array $bindings = [])
{
Expand All @@ -151,7 +151,7 @@ public function setBindings(array $bindings = [])
* @param string $role A valid role for the service
* @param array $members An array of members to assign to the binding
* @return PolicyBuilder
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
* @throws BadMethodCallException if the policy's version is greater than 1.
* @deprecated
*/
Expand Down Expand Up @@ -193,7 +193,7 @@ public function addBinding($role, array $members)
* @param string $role A valid role for the service
* @param array $members An array of members to remove from the role
* @return PolicyBuilder
* @throws InvalidArgumentException
* @throws \InvalidArgumentException
* @throws BadMethodCallException if the policy's version is greater than 1.
* @deprecated
*/
Expand Down
3 changes: 1 addition & 2 deletions src/Testing/Snippet/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

namespace Google\Cloud\Core\Testing\Snippet\Parser;

use Google\Cloud\Core\Testing\Snippet\Coverage\Scanner;
use Google\Cloud\Core\Testing\Reflection\ReflectionHandlerFactory;
use DOMDocument;
use Parsedown;
Expand Down Expand Up @@ -59,7 +58,7 @@ public function __construct()
* @param string $class the name of the class
* @param int|string $index The index of the example to return.
* @return Snippet
* @throws Exception
* @throws \Exception
*/
public function classExample($class, $index = 0)
{
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/ArrayTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\Testing\TestHelpers;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -46,7 +47,7 @@ public function testPluck()

public function testPluckThrowsExceptionWithInvalidKey()
{
$this->expectException('\InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$array = [];
$this->impl->call('pluck', ['not_here', &$array]);
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/Batch/BatchRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Google\Cloud\Core\Batch\BatchRunner;
use Google\Cloud\Core\Batch\ConfigStorageInterface;
use Google\Cloud\Core\Batch\ProcessItemInterface;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
Expand All @@ -34,6 +35,7 @@ class BatchRunnerTest extends TestCase
{
use ProphecyTrait;

private $batchConfig;
private $configStorage;
private $processor;

Expand All @@ -46,7 +48,7 @@ public function setUp(): void

public function testRegisterJobClosure()
{
$this->expectException('\InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$runner = new BatchRunner(
$this->configStorage->reveal(),
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Batch/BatchTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

use Google\Cloud\Core\Batch\BatchJob;
use Google\Cloud\Core\Batch\BatchRunner;
use Google\Cloud\Core\Batch\BatchTrait;
use Google\Cloud\Core\Batch\ProcessItemInterface;
use Google\Cloud\Core\Tests\Unit\Batch\Fixtures\BatchClass;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
Expand All @@ -33,6 +33,7 @@
class BatchTraitTest extends TestCase
{
use ProphecyTrait;

const ID = 'some-id';
const BATCH_METHOD = 'doBatch';

Expand Down Expand Up @@ -86,15 +87,15 @@ public function testSend()

public function testSetCommonBatchPropertiesThrowsExceptionWithoutIdentifier()
{
$this->expectException('\InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$impl = new BatchClass();
$impl->setCommonBatchProperties(['batchMethod' => self::BATCH_METHOD]);
}

public function testSetCommonBatchPropertiesThrowsExceptionWithoutBatchMethod()
{
$this->expectException('\InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$impl = new BatchClass();
$impl->setCommonBatchProperties(['identifier' => self::ID]);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Batch/HandleFailureTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testInitFailureFileThrowsException()
if (0 === posix_getuid()) {
$this->markTestSkipped('Cannot test init failure as root');
}
$this->expectException('\RuntimeException');
$this->expectException(\RuntimeException::class);

putenv('GOOGLE_CLOUD_BATCH_DAEMON_FAILURE_DIR=/bad/write/dir');
$this->impl->call('initFailureFile');
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Batch/InMemoryConfigStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function ($id) {

public function testSerializeThrowsException()
{
$this->expectException('BadMethodCallException');
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Serialization not supported');

$configStorage = InMemoryConfigStorage::getInstance();
Expand All @@ -111,7 +111,7 @@ public function testSerializeThrowsException()

public function testUnserializeThrowsException()
{
$this->expectException('BadMethodCallException');
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Serialization not supported');

$configStorage = InMemoryConfigStorage::getInstance();
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Batch/SimpleJobTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Google\Cloud\Core\Batch\JobConfig;
use Google\Cloud\Core\Batch\SimpleJob;
use Google\Cloud\Core\Batch\SimpleJobTrait;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -33,7 +34,7 @@ class SimpleJobTraitTest extends TestCase

public function testSetSimpleJobPropertiesThrowsExceptionWithoutIdentifier()
{
$this->expectException('\InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$job = new SimpleClass();
$job->setSimpleJobProperties([]);
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Batch/SysvProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Google\Cloud\Core\Tests\Unit\Batch;

use Google\Cloud\Core\Batch\BatchDaemonTrait;
use Google\Cloud\Core\Batch\QueueOverflowException;
use Google\Cloud\Core\Batch\SysvProcessor;
use Google\Cloud\Core\SysvTrait;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -138,7 +139,7 @@ public function testQueueOverflowDirect()
*/
public function testQueueOverflowFile()
{
$this->expectException('\Google\Cloud\Core\Batch\QueueOverflowException');
$this->expectException(QueueOverflowException::class);

$queueSize = $this->queueSize();
$item = str_repeat('a', 8160);
Expand Down
13 changes: 7 additions & 6 deletions tests/Unit/ClientTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Google\Cloud\Core\ClientTrait;
use Google\Cloud\Core\Compute\Metadata;
use Google\Cloud\Core\Exception\GoogleException;
use Google\Cloud\Core\Testing\TestHelpers;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function setUp(): void
*/
public function testGetConnectionTypeInvalidStatus($dependencyStatus, $config)
{
$this->expectException('Google\Cloud\Core\Exception\GoogleException');
$this->expectException(GoogleException::class);

$this->dependency->___setProperty('dependencyStatus', $dependencyStatus);
$this->dependency->call('getConnectionType', [$config]);
Expand Down Expand Up @@ -119,7 +120,7 @@ public function testRequireGrpcPassesWithGrpc()

public function testRequireGrpcThrowsExceptionWithoutGrpc()
{
$this->expectException('Google\Cloud\Core\Exception\GoogleException');
$this->expectException(GoogleException::class);

$this->dependency->___setProperty('dependencyStatus', false);
$this->dependency->call('requireGrpc');
Expand Down Expand Up @@ -165,7 +166,7 @@ public function testConfigureAuthenticationWithKeyFilePath()

public function testConfigureAuthenticationWithInvalidKeyFilePath()
{
$this->expectException('Google\Cloud\Core\Exception\GoogleException');
$this->expectException(GoogleException::class);

$keyFilePath = __DIR__ . '/i/sure/hope/this/doesnt/exist';

Expand All @@ -176,7 +177,7 @@ public function testConfigureAuthenticationWithInvalidKeyFilePath()

public function testConfigureAuthenticationWithKeyFileThatCantBeDecoded()
{
$this->expectException('Google\Cloud\Core\Exception\GoogleException');
$this->expectException(GoogleException::class);

$keyFilePath = __DIR__ . '/ClientTraitTest.php';

Expand All @@ -187,7 +188,7 @@ public function testConfigureAuthenticationWithKeyFileThatCantBeDecoded()

public function testDetectProjectIdWithNoProjectIdAvailable()
{
$this->expectException('Google\Cloud\Core\Exception\GoogleException');
$this->expectException(GoogleException::class);

$conf = $this->impl->call('detectProjectId', [[
'projectIdRequired' => true,
Expand Down Expand Up @@ -287,7 +288,7 @@ public function testDetectNumericProjectIdOnGce()

public function testDetectProjectIdOnGceButOhNoThereStillIsntAProjectId()
{
$this->expectException('Google\Cloud\Core\Exception\GoogleException');
$this->expectException(GoogleException::class);

$projectId = null;

Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/GeoPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Google\Cloud\Core\Tests\Unit;

use Google\Cloud\Core\GeoPoint;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -57,7 +58,7 @@ public function testPoint()
*/
public function testCheckContext($method)
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$point = new GeoPoint(1.1, 2.2);
$point->$method(222.33);
Expand All @@ -68,7 +69,7 @@ public function testCheckContext($method)
*/
public function testInvalidType($method)
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$method = 'set' . ucfirst($method);
$point = new GeoPoint(1.1, 2.2);
Expand All @@ -80,7 +81,7 @@ public function testInvalidType($method)
*/
public function testSetNullValue($method)
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$method = 'set' . ucfirst($method);
$point = new GeoPoint(1.1, 2.2, true);
Expand Down
9 changes: 6 additions & 3 deletions tests/Unit/GrpcRequestWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
use Google\ApiCore\ServerStream;
use Google\Auth\FetchAuthTokenInterface;
use Google\Cloud\Core\Exception;
use Google\Cloud\Core\Exception\GoogleException;
use Google\Cloud\Core\Exception\ServiceException;
use Google\Cloud\Core\GrpcRequestWrapper;
use Google\Cloud\Core\Testing\GrpcTestTrait;
use Google\Rpc\BadRequest;
use Google\Rpc\BadRequest\FieldViolation;
use Google\Rpc\Code;
use Google\Rpc\PreconditionFailure;
use Google\Rpc\Status;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

Expand Down Expand Up @@ -139,7 +142,7 @@ function () use (&$timesCalled) {

public function testThrowsExceptionWhenRequestFails()
{
$this->expectException('Google\Cloud\Core\Exception\GoogleException');
$this->expectException(GoogleException::class);

$requestWrapper = new GrpcRequestWrapper();

Expand Down Expand Up @@ -194,7 +197,7 @@ public function testReturnsStreamedResponse()

public function testThrowsExceptionWithInvalidCredentialsFetcher()
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$credentialsFetcher = new \stdClass();

Expand Down Expand Up @@ -320,7 +323,7 @@ public function testExceptionMetadata()
}, [[]], ['retries' => 0]);

$this->assertFalse(true, 'Exception not thrown!');
} catch (\Exception $ex) {
} catch (ServiceException $ex) {
$this->assertEquals(
json_decode($metadata->serializeToJsonString(), true),
$ex->getMetadata()[0]
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Iam/IamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Google\Cloud\Core\Iam\Iam;
use Google\Cloud\Core\Iam\IamConnectionInterface;
use Google\Cloud\Core\Iam\PolicyBuilder;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
Expand Down Expand Up @@ -106,7 +107,7 @@ public function testSetPolicyWithPolicyBuilder()

public function testSetPolicyWithInvalidPolicy()
{
$this->expectException('InvalidArgumentException');
$this->expectException(InvalidArgumentException::class);

$iam = new Iam($this->connection->reveal(), self::RESOURCE);
$res = $iam->setPolicy('foo');
Expand Down
Loading

0 comments on commit 106abbb

Please sign in to comment.