Skip to content

Commit

Permalink
Move shared testing code into core (#822)
Browse files Browse the repository at this point in the history
* manual changes

* Grpc trait rename

* Rename key storage trait

* Rename stub

* Rename impl

* Rename snippet tests

* Rename stub

* Rename SystemTestCase

* Rename DeletionQueue

* Move fixtures

* Rename stub and impl

* Fix locking and fixtures

* Fixes

* Restructure MockGlobals, update fixtures

* Fix phpcs errors

* Fix snippet tests

* Add comments to support doc building

* Exclude MockValues from side effects phpcs rule

* Update StubTrait

* Remove Storage dependency from StreamableUploaderTest

* Move code out of dev

* Add snippet testing of parser

* Update Iam

* Handle servicebuilder tests

* Add internal and experimental annotations

* Add README to testing directory

* Fix phpcs errors

* Fix import for doc gen

* Add comments to new code in Core/Testing

* Update after merge

* Address PR comments

* Fix reference to impl

* Address PR feedback

* Exclude Core/Testing from unit tests

* Remove extra space

* Rename Functions to TestHelpers
  • Loading branch information
michaelbausor authored and dwsupplee committed Jan 31, 2018
1 parent d534eaa commit b84cfd6
Show file tree
Hide file tree
Showing 240 changed files with 1,116 additions and 478 deletions.
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@
"Google\\Cloud\\Tests\\System\\": "tests/system",
"Google\\Cloud\\Tests\\Unit\\": "tests/unit"
},
"files": [
"dev/src/Functions.php"
],
"classmap": [
"tests/conformance-fixtures"
]
Expand Down
2 changes: 1 addition & 1 deletion dev/src/DocGenerator/Parser/CodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Google\Cloud\Dev\DocGenerator\Parser;

use Google\Cloud\Dev\DocBlockStripSpaces;
use Google\Cloud\Core\Testing\DocBlockStripSpaces;
use Google\Cloud\Dev\DocGenerator\ReflectorRegister;
use Google\Cloud\Dev\GetComponentsTrait;
use phpDocumentor\Reflection\ClassReflector;
Expand Down
59 changes: 0 additions & 59 deletions dev/src/Functions.php

This file was deleted.

3 changes: 3 additions & 0 deletions phpcs-ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
<rule ref="PSR1.Methods.CamelCapsMethodName">
<exclude-pattern>src/Storage/StreamWrapper.php</exclude-pattern>
</rule>
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>src/Core/Testing/Lock/MockValues.php</exclude-pattern>
</rule>
<file>src</file>
</ruleset>
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<directory suffix=".php">src/*/V[!a-zA-Z]*</directory>
<directory suffix=".php">src/*/*/V[!a-zA-Z]*</directory>
<directory suffix=".php">src/*/*/*/V[!a-zA-Z]*</directory>
<directory suffix=".php">src/Core/Testing</directory>
</exclude>
</whitelist>
</filter>
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Iam/Iam.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* ```
* // IAM policies are obtained via resources which implement IAM.
* // In this example, we'll use PubSub topics to demonstrate
* // how IAM politices are managed.
* // how IAM policies are managed.
*
* use Google\Cloud\PubSub\PubSubClient;
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
<?php

namespace Google\Cloud\Tests;
namespace Google\Cloud\Core\Testing;

use Prophecy\Argument\Token\TokenInterface;
use Prophecy\Util\StringUtil;

/**
* ArrayHasSameValuesToken implements TokenInterface and is used for testing
*
* @experimental
* @internal
*/
class ArrayHasSameValuesToken implements TokenInterface
{
private $value;
private $string;
private $util;

/**
* ArrayHasSameValuesToken constructor.
* @param $value
* @param StringUtil|null $util
*
* @experimental
* @internal
*/
public function __construct($value, StringUtil $util = null)
{
$this->value = $value;
$this->util = $util ?: new StringUtil();
}

/**
* @param $argument
* @return bool|int
*
* @experimental
* @internal
*/
public function scoreArgument($argument)
{
return $this->compare($this->value, $argument) ? 11 : false;
Expand All @@ -30,11 +51,23 @@ private function compare(array $value, array $argument)
return $value == $argument;
}

/**
* @return bool
*
* @experimental
* @internal
*/
public function isLast()
{
return false;
}

/**
* @return string
*
* @experimental
* @internal
*/
public function __toString()
{
if ($this->string) {
Expand Down
45 changes: 45 additions & 0 deletions src/Core/Testing/CheckForClassTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright 2018 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\Core\Testing;

/**
* Trait CheckForClassTrait
*
* @experimental
* @internal
*/
trait CheckForClassTrait
{
/**
* Check whether all required classes are available, otherwise skip the tests.
*
* @param array $requiredClasses List of classes that must be available.
*
* @experimental
* @internal
*/
protected function checkAndSkipTest(array $requiredClasses)
{
foreach ($requiredClasses as $class) {
if (!class_exists($class)) {
$this->markTestSkipped("Missing required class: $class");
return;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
* limitations under the License.
*/

namespace Google\Cloud\Dev;
namespace Google\Cloud\Core\Testing;

use phpDocumentor\Reflection\DocBlock;

/**
* Class DocBlockStripSpaces
*
* @experimental
* @internal
*/
class DocBlockStripSpaces extends DocBlock
{
/**
Expand All @@ -28,6 +34,9 @@ class DocBlockStripSpaces extends DocBlock
* @param int $spaces The number of spaces to strip.
*
* @return string
*
* @experimental
* @internal
*/
public function cleanInput($comment, $spaces = 4)
{
Expand Down
17 changes: 16 additions & 1 deletion tests/GrpcTestTrait.php → src/Core/Testing/GrpcTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@
* limitations under the License.
*/

namespace Google\Cloud\Tests;
namespace Google\Cloud\Core\Testing;

/**
* Provides checks for whether to run gRPC tests
*
* @experimental
* @internal
*/
trait GrpcTestTrait
{
/**
* Checks for the grpc extension, and if it is missing, marked the test as skipped
*
* @experimental
* @internal
*/
public function checkAndSkipGrpcTests()
{
if (!extension_loaded('grpc')) {
Expand All @@ -32,6 +41,12 @@ public function checkAndSkipGrpcTests()
}
}

/**
* @return bool True if grpc tests should be skipped, otherwise false
*
* @experimental
* @internal
*/
public function shouldSkipGrpcTests()
{
return !extension_loaded('grpc') || defined('HHVM_VERSION');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@
* limitations under the License.
*/

namespace Google\Cloud\Tests;
namespace Google\Cloud\Core\Testing;

use Google\Cloud\Storage\EncryptionTrait;
use phpseclib\Crypt\RSA;

/**
* Trait KeyPairGenerateTrait implements key pair generation functions used for testing
*
* @experimental
* @internal
*/
trait KeyPairGenerateTrait
{
use EncryptionTrait;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
<?php

/**
* Copyright 2017 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.
*/

/**
* @experimental
* @internal
*/

namespace Google\Cloud\Core\Lock {
class MockValues
{
public static $flockReturnValue;
public static $fopenReturnValue;
public static $sem_acquireReturnValue;
public static $sem_releaseReturnValue;
public static $sem_getReturnValue;

public static function initialize()
{
self::$flockReturnValue = true;
self::$fopenReturnValue = function($file, $mode) {
return \fopen($file, $mode);
};
self::$sem_acquireReturnValue = true;
self::$sem_releaseReturnValue = true;
self::$sem_getReturnValue = function($key) {
return \sem_get($key);
};
}
}

use Google\Cloud\Core\Testing\Lock\MockValues;

function flock($handle, $type)
{
Expand Down
Loading

0 comments on commit b84cfd6

Please sign in to comment.