Skip to content

Commit

Permalink
initial pubsub system tests (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwsupplee authored and jdpedrie committed Nov 7, 2016
1 parent 3c58ba8 commit 53f69d2
Show file tree
Hide file tree
Showing 107 changed files with 404 additions and 9 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
},
"autoload-dev": {
"psr-4": {
"Google\\Cloud\\Dev\\": "dev/src"
"Google\\Cloud\\Dev\\": "dev/src",
"Google\\Cloud\\Tests\\System\\": "tests/system"
}
},
"scripts": {
Expand Down
16 changes: 16 additions & 0 deletions phpunit.system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/system/bootstrap.php" colors="true">
<testsuites>
<testsuite>
<directory>tests/system</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory suffix=".php">src/*/V[!a-zA-Z]*</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
<phpunit bootstrap="./tests/unit/bootstrap.php" colors="true">
<testsuites>
<testsuite>
<directory>tests</directory>
<directory>tests/unit</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
3 changes: 0 additions & 3 deletions tests/bootstrap.php

This file was deleted.

62 changes: 62 additions & 0 deletions tests/system/PubSub/ManageIAMPoliciesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\PubSub;

class ManageIAMPoliciesTest extends PubSubTestCase
{
/**
* @dataProvider clientProvider
*/
public function testManageTopicIAM($client)
{
$topic = $client->createTopic(uniqid(self::TESTING_PREFIX));
self::$deletionQueue[] = $topic;
$this->assertIam($topic->iam());
}

/**
* @dataProvider clientProvider
*/
public function testManageSubscriptionIAM($client)
{
$topic = $client->createTopic(uniqid(self::TESTING_PREFIX));
self::$deletionQueue[] = $topic;
$sub = $client->subscribe(uniqid(self::TESTING_PREFIX), $topic->name());
self::$deletionQueue[] = $sub;

$this->assertIam($sub->iam());
}

private function assertIam($iam)
{
$policy = [
'bindings' => [
[
'role' => 'roles/pubsub.admin',
'members' => [
'user:[email protected]'
]
]
]
];
$iam->setPolicy($policy);
$actualPolicy = $iam->reload();

$this->assertEquals($policy['bindings'][0], $actualPolicy['bindings'][0]);
}
}
76 changes: 76 additions & 0 deletions tests/system/PubSub/ManageSubscriptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\PubSub;

class ManageSubscriptionsTest extends PubSubTestCase
{
/**
* @dataProvider clientProvider
*/
public function testCreateAndListSubscriptions($client)
{
$topicName = uniqid(self::TESTING_PREFIX);
$topic = $client->createTopic($topicName);
$subsToCreate = [
uniqid(self::TESTING_PREFIX),
uniqid(self::TESTING_PREFIX)
];

foreach ($subsToCreate as $subToCreate) {
self::$deletionQueue[] = $client->subscribe($subToCreate, $topicName);
}

$subs = $client->subscriptions();
$subsByTopic = $topic->subscriptions();

$this->assertSubsFound($subs, $subsToCreate);
$this->assertSubsFound($subsByTopic, $subsToCreate);
}

/**
* @dataProvider clientProvider
*/
public function testReloadSub($client)
{
$topicName = uniqid(self::TESTING_PREFIX);
$topic = $client->createTopic($topicName);
self::$deletionQueue[] = $topic;
$shortName = uniqid(self::TESTING_PREFIX);
$this->assertFalse($topic->subscription($shortName)->exists());
$sub = $client->subscribe($shortName, $topic->name());
self::$deletionQueue[] = $sub;

$this->assertTrue($topic->subscription($shortName)->exists());
$this->assertEquals($sub->name(), $sub->reload()['name']);
}

private function assertSubsFound($subs, $expectedSubs)
{
$foundSubs = [];
foreach ($subs as $sub) {
$sName = end(explode('/', $sub->name()));
foreach ($expectedSubs as $key => $expectedSub) {
if ($sName === $expectedSub) {
$foundSubs[$key] = $sName;
}
}
}

$this->assertEquals($expectedSubs, $foundSubs);
}
}
64 changes: 64 additions & 0 deletions tests/system/PubSub/ManageTopicsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\PubSub;

class ManageTopicsTest extends PubSubTestCase
{
/**
* @dataProvider clientProvider
*/
public function testCreateAndListTopics($client)
{
$foundTopics = [];
$topicsToCreate = [
uniqid(self::TESTING_PREFIX),
uniqid(self::TESTING_PREFIX)
];

foreach ($topicsToCreate as $topicToCreate) {
self::$deletionQueue[] = $client->createTopic($topicToCreate);
}

$topics = $client->topics();

foreach ($topics as $topic) {
$tName = end(explode('/', $topic->name()));
foreach ($topicsToCreate as $key => $topicToCreate) {
if ($tName === $topicToCreate) {
$foundTopics[$key] = $tName;
}
}
}

$this->assertEquals($topicsToCreate, $foundTopics);
}

/**
* @dataProvider clientProvider
*/
public function testReloadTopic($client)
{
$shortName = uniqid(self::TESTING_PREFIX);
$this->assertFalse($client->topic($shortName)->exists());
$topic = $client->createTopic($shortName);
self::$deletionQueue[] = $topic;

$this->assertTrue($client->topic($shortName)->exists());
$this->assertEquals($topic->name(), $topic->reload()['name']);
}
}
73 changes: 73 additions & 0 deletions tests/system/PubSub/PubSubTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Tests\System\PubSub;

use Google\Cloud\ExponentialBackoff;
use Google\Cloud\PubSub\PubSubClient;

class PubSubTestCase extends \PHPUnit_Framework_TestCase
{
const TESTING_PREFIX = 'gcloud_testing_';

protected static $deletionQueue = [];
protected static $grpcClient;
protected static $restClient;
protected static $topic;
private static $hasSetUp = false;

public function clientProvider()
{
self::setUpBeforeClass();

return [
[self::$restClient],
[self::$grpcClient]
];
}

public static function setUpBeforeClass()
{
if (self::$hasSetUp) {
return;
}

$keyFilePath = getenv('GOOGLE_CLOUD_PHP_TESTS_KEY_PATH');
self::$restClient = new PubSubClient([
'keyFilePath' => $keyFilePath,
'transport' => 'rest'
]);
self::$grpcClient = new PubSubClient([
'keyFilePath' => $keyFilePath,
'transport' => 'grpc'
]);
self::$hasSetUp = true;
}

public static function tearDownFixtures()
{
$backoff = new ExponentialBackoff(8);

foreach (self::$deletionQueue as $item) {
$backoff->execute(function () use ($item) {
$item->delete();
});
}
}
}


Loading

0 comments on commit 53f69d2

Please sign in to comment.