Skip to content

Commit

Permalink
chore: improve test suite (googleapis#2115)
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Sep 20, 2021
1 parent 3a98175 commit 0285bc1
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 61 deletions.
17 changes: 0 additions & 17 deletions .github/apply-phpunit-patches.sh

This file was deleted.

16 changes: 12 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ jobs:
composer-flags: "--prefer-lowest "
- php: "8.0"
composer-flags: "--prefer-lowest "
- php: "8.1"
composer-flags: "--ignore-platform-reqs "
- php: "8.1"
composer-flags: "--ignore-platform-reqs --prefer-lowest "
name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }}Unit Test
steps:
- uses: actions/checkout@v2
Expand All @@ -31,13 +35,17 @@ jobs:
timeout_minutes: 10
max_attempts: 3
command: composer update ${{ matrix.composer-flags }}
- if: ${{ matrix.php == '8.0' || matrix.composer-flags == '--prefer-lowest' }}
- if: ${{ matrix.php == '8.0' || ( matrix.composer-flags == '--prefer-lowest' && matrix.php != '8.1' ) }}
name: Update guzzlehttp/ringphp dependency
run: composer update guzzlehttp/ringphp
- if: ${{ matrix.php == '5.6' || matrix.php == '7.0' || matrix.php == '7.1' }}
name: Run PHPUnit Patches
run: sh .github/apply-phpunit-patches.sh
- if: ${{ matrix.php == '8.1' }}
name: Update guzzlehttp/ringphp dependency
run: composer update guzzlehttp/ringphp --ignore-platform-reqs
- if: ${{ matrix.php == '8.1' }}
name: Update phpunit/phpunit dependency
run: composer update phpunit/phpunit phpspec/prophecy-phpunit --with-dependencies --ignore-platform-reqs
- name: Run Script
continue-on-error: ${{ matrix.php == '8.1' }}
run: vendor/bin/phpunit

style:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ vendor
examples/testfile-small.txt
examples/testfile.txt
tests/.apiKey
.phpunit.result.cache
.idea/
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
"guzzlehttp/psr7": "^1.2"
},
"require-dev": {
"phpunit/phpunit": "^5.7||^8.5.13",
"squizlabs/php_codesniffer": "~2.3",
"symfony/dom-crawler": "~2.1",
"symfony/css-selector": "~2.1",
"cache/filesystem-adapter": "^0.3.2|^1.1",
"phpcompatibility/php-compatibility": "^9.2",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"composer/composer": "^1.10.22"
"composer/composer": "^1.10.22",
"yoast/phpunit-polyfills": "^1.0",
"phpspec/prophecy-phpunit": "^1.1||^2.0",
"phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0"
},
"suggest": {
"cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.2/phpunit.xsd"
colors="true"
bootstrap="tests/bootstrap.php">
<testsuites>
Expand Down
14 changes: 12 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Collection extends Model implements \Iterator, \Countable
{
protected $collection_key = 'items';

#[ReturnTypeWillChange]
public function rewind()
{
if (isset($this->{$this->collection_key})
Expand All @@ -19,6 +20,7 @@ public function rewind()
}
}

#[ReturnTypeWillChange]
public function current()
{
$this->coerceType($this->key());
Expand All @@ -27,6 +29,7 @@ public function current()
}
}

#[ReturnTypeWillChange]
public function key()
{
if (isset($this->{$this->collection_key})
Expand All @@ -35,17 +38,20 @@ public function key()
}
}

#[ReturnTypeWillChange]
public function next()
{
return next($this->{$this->collection_key});
}

#[ReturnTypeWillChange]
public function valid()
{
$key = $this->key();
return $key !== null && $key !== false;
}

#[ReturnTypeWillChange]
public function count()
{
if (!isset($this->{$this->collection_key})) {
Expand All @@ -54,6 +60,7 @@ public function count()
return count($this->{$this->collection_key});
}

#[ReturnTypeWillChange]
public function offsetExists($offset)
{
if (!is_numeric($offset)) {
Expand All @@ -62,6 +69,7 @@ public function offsetExists($offset)
return isset($this->{$this->collection_key}[$offset]);
}

#[ReturnTypeWillChange]
public function offsetGet($offset)
{
if (!is_numeric($offset)) {
Expand All @@ -71,18 +79,20 @@ public function offsetGet($offset)
return $this->{$this->collection_key}[$offset];
}

#[ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (!is_numeric($offset)) {
return parent::offsetSet($offset, $value);
parent::offsetSet($offset, $value);
}
$this->{$this->collection_key}[$offset] = $value;
}

#[ReturnTypeWillChange]
public function offsetUnset($offset)
{
if (!is_numeric($offset)) {
return parent::offsetUnset($offset);
parent::offsetUnset($offset);
}
unset($this->{$this->collection_key}[$offset]);
}
Expand Down
23 changes: 19 additions & 4 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,26 @@
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Cache\Adapter\Filesystem\FilesystemCachePool;
use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

if (trait_exists('\Prophecy\PhpUnit\ProphecyTrait')) {
trait BaseTestTrait
{
use \Prophecy\PhpUnit\ProphecyTrait;
}
} else {
trait BaseTestTrait
{
}
}

class BaseTest extends TestCase
{
private $key;
private $client;

use BaseTestTrait;

public function getClient()
{
if (!$this->client) {
Expand Down Expand Up @@ -71,12 +84,14 @@ private function createClient()
$client = new Client();
$client->setApplicationName('google-api-php-client-tests');
$client->setHttpClient($httpClient);
$client->setScopes([
$client->setScopes(
[
"https://www.googleapis.com/auth/tasks",
"https://www.googleapis.com/auth/adsense",
"https://www.googleapis.com/auth/youtube",
"https://www.googleapis.com/auth/drive",
]);
]
);

if ($this->key) {
$client->setDeveloperKey($this->key);
Expand Down Expand Up @@ -178,7 +193,7 @@ protected function checkKey()
$apiKey = file_get_contents($apiKeyFile);
} elseif (!$apiKey = getenv('GOOGLE_API_KEY')) {
$this->markTestSkipped(
"Test requires api key\nYou can create one in your developer console"
"Test requires api key\nYou can create one in your developer console"
);
file_put_contents($apiKeyFile, $apiKey);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Google/Http/BatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public function testMediaFileBatch()
/** @var \GuzzleHttp\Psr7\Request $request */
$request = $storage->objects->insert($bucket, $obj, $params);

$this->assertContains('multipart/related', $request->getHeaderLine('content-type'));
$this->assertContains('/upload/', $request->getUri()->getPath());
$this->assertContains('uploadType=multipart', $request->getUri()->getQuery());
$this->assertStringContainsString('multipart/related', $request->getHeaderLine('content-type'));
$this->assertStringContainsString('/upload/', $request->getUri()->getPath());
$this->assertStringContainsString('uploadType=multipart', $request->getUri()->getQuery());
}
}
4 changes: 2 additions & 2 deletions tests/Google/Http/MediaFileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public function testProcess()
$request = $request->withBody(Psr7\stream_for($reqData));
$media = new MediaFileUpload($client, $request, 'image/png', $data, false);
$request = $media->getRequest();
$this->assertContains($reqData, (string) $request->getBody());
$this->assertContains(base64_encode($data), (string) $request->getBody());
$this->assertStringContainsString($reqData, (string) $request->getBody());
$this->assertStringContainsString(base64_encode($data), (string) $request->getBody());
}

public function testGetResumeUri()
Expand Down
2 changes: 1 addition & 1 deletion tests/Google/Http/RESTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class RESTTest extends BaseTest
*/
private $rest;

public function setUp(): void
public function set_up()
{
$this->rest = new REST();
$this->request = new Request('GET', '/');
Expand Down
2 changes: 1 addition & 1 deletion tests/Google/Service/AdSenseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
class AdSenseTest extends BaseTest
{
public $adsense;
public function setUp(): void
public function set_up()
{
$this->markTestSkipped('Thesse tests need to be fixed');
$this->checkToken();
Expand Down
2 changes: 1 addition & 1 deletion tests/Google/Service/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ResourceTest extends BaseTest
private $client;
private $service;

public function setUp(): void
public function set_up()
{
$this->client = $this->prophesize(Client::class);

Expand Down
2 changes: 1 addition & 1 deletion tests/Google/Service/TasksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TasksTest extends BaseTest
/** @var Tasks */
public $taskService;

public function setUp(): void
public function set_up()
{
$this->checkToken();
$this->taskService = new Tasks($this->getClient());
Expand Down
2 changes: 1 addition & 1 deletion tests/Google/Service/YouTubeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class YouTubeTest extends BaseTest
{
/** @var YouTube */
public $youtube;
public function setUp(): void
public function set_up()
{
$this->checkToken();
$this->youtube = new YouTube($this->getClient());
Expand Down
46 changes: 33 additions & 13 deletions tests/Google/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Google\Model;
use Google\Service;
use Google\Http\Batch;
use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -47,22 +47,40 @@ class TestService extends Service
public $batchPath = 'batch/test';
}

if (trait_exists('\Prophecy\PhpUnit\ProphecyTrait')) {
trait ServiceTestTrait
{
use \Prophecy\PhpUnit\ProphecyTrait;
}
} else {
trait ServiceTestTrait
{
}
}

class ServiceTest extends TestCase
{
private static $errorMessage;

use ServiceTestTrait;

public function testCreateBatch()
{
$response = $this->prophesize(ResponseInterface::class);
$client = $this->prophesize(Client::class);

$client->execute(Argument::allOf(
Argument::type(RequestInterface::class),
Argument::that(function ($request) {
$this->assertEquals('/batch/test', $request->getRequestTarget());
return $request;
})
), Argument::any())->willReturn($response->reveal());
$client->execute(
Argument::allOf(
Argument::type(RequestInterface::class),
Argument::that(
function ($request) {
$this->assertEquals('/batch/test', $request->getRequestTarget());
return $request;
}
)
),
Argument::any()
)->willReturn($response->reveal());

$client->getConfig('base_path')->willReturn('');

Expand Down Expand Up @@ -131,12 +149,14 @@ public function testInvalidConstructorPhp7Plus()

try {
$service = new TestService('foo');
} catch (\TypeError $e) {}
} catch (\TypeError $e) {

}

$this->assertInstanceOf('TypeError', $e);
$this->assertEquals(
'constructor must be array or instance of Google\Client',
$e->getMessage()
'constructor must be array or instance of Google\Client',
$e->getMessage()
);
}

Expand All @@ -152,8 +172,8 @@ public function testInvalidConstructorPhp5()
$service = new TestService('foo');

$this->assertEquals(
'constructor must be array or instance of Google\Client',
self::$errorMessage
'constructor must be array or instance of Google\Client',
self::$errorMessage
);
}

Expand Down
Loading

0 comments on commit 0285bc1

Please sign in to comment.