Skip to content

Commit

Permalink
support debug by set environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
aliguyong authored and JacksonTian committed Feb 20, 2019
1 parent 9653128 commit 9a14785
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README-CN.md
Expand Up @@ -479,6 +479,10 @@ AlibabaCloud::addHost('product_name', 'product_name.aliyuncs.com');
```


## 调试
如果存在环境变量 `DEBUG=sdk` ,所有请求将启用调试输出。


## 相关
* [阿里云服务 Regions & Endpoints][endpoints]
* [OpenAPI Explorer][open-api]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -479,6 +479,10 @@ AlibabaCloud::addHost('product_name', 'product_name.aliyuncs.com');
```


## Debugging
If there is an environment variable `DEBUG=sdk` , all requests will enable debug mode.


## References
* [Alibaba Cloud Regions & Endpoints][endpoints]
* [OpenAPI Explorer][open-api]
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -48,7 +48,7 @@
"friendsofphp/php-cs-fixer": "^2.2",
"league/climate": "^3.2.4",
"mikey179/vfsStream": "^1.6",
"phpunit/phpunit": "4.*",
"phpunit/phpunit": "^4.8.35|^5.4.3",
"psr/cache": "^1.0",
"squizlabs/php_codesniffer": "^3.4",
"symfony/dotenv": "^3.4",
Expand All @@ -67,6 +67,7 @@
"src/Constants/Business.php",
"src/Constants/ErrorCode.php",
"src/Functions.php",
"src/Helpers.php",
"src/Load.php"
]
},
Expand Down
54 changes: 54 additions & 0 deletions src/Helpers.php
@@ -0,0 +1,54 @@
<?php
if (!function_exists('env')) {
/**
* Gets the value of an environment variable.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
function env($key, $default = null)
{
$value = getenv($key);

if ($value === false) {
return value($default);
}

switch (strtolower($value)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'empty':
case '(empty)':
return '';
case 'null':
case '(null)':
return null;
}

if (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[$valueLength - 1] === '"') {
return substr($value, 1, -1);
}

return $value;
}
}

if (!function_exists('value')) {
/**
* Return the default value of the given value.
*
* @param mixed $value
*
* @return mixed
*/
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
}
4 changes: 4 additions & 0 deletions src/Request/Request.php
Expand Up @@ -95,6 +95,10 @@ public function __construct(array $options = [])
if ($options !== []) {
$this->options($options);
}

if (strtolower(env('DEBUG')) === 'sdk') {
$this->options['debug'] = true;
}
}

/**
Expand Down
74 changes: 74 additions & 0 deletions tests/Feature/Request/RequestDebugTest.php
@@ -0,0 +1,74 @@
<?php

namespace AlibabaCloud\Client\Tests\Feature\Request;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Client\Tests\Mock\Services\Ecs\DescribeRegionsRequest;
use PHPUnit\Framework\TestCase;

/**
* Class RequestDebugTest
*
* @package AlibabaCloud\Client\Tests\Feature\Request
*/
class RequestDebugTest extends TestCase
{
/**
* @throws ClientException
* @throws ServerException
*/
public function testEnv()
{
// Setup
$nameClient = 'name';
$regionId = \getenv('REGION_ID');
$accessKeyId = \getenv('ACCESS_KEY_ID');
$accessKeySecret = \getenv('ACCESS_KEY_SECRET');
putenv('DEBUG=sdk');

// Test
AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
->regionId($regionId)
->name($nameClient);

// Assert
$request = (new DescribeRegionsRequest())->client($nameClient)
->connectTimeout(15)
->timeout(20);
$request->request();

self::assertArrayHasKey('debug', $request->options);
self::assertTrue($request->options['debug']);
}

/**
* @throws ClientException
* @throws ServerException
*/
public function testOption()
{
// Setup
$nameClient = 'name';
$regionId = \getenv('REGION_ID');
$accessKeyId = \getenv('ACCESS_KEY_ID');
$accessKeySecret = \getenv('ACCESS_KEY_SECRET');
putenv('DEBUG=false');

// Test
AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
->regionId($regionId)
->name($nameClient);

// Assert
$request = (new DescribeRegionsRequest())->client($nameClient)
->debug(true)
->connectTimeout(15)
->timeout(20);
$request->request();

self::assertArrayHasKey('debug', $request->options);
self::assertTrue($request->options['debug']);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/AlibabaCloudTest.php
Expand Up @@ -8,7 +8,7 @@
/**
* Class AlibabaCloudTest
*
* @package AlibabaCloud\Client\Tests\Unit\Client
* @package AlibabaCloud\Client\Tests\Unit
*
* @coversDefaultClass \AlibabaCloud\Client\AlibabaCloud
*/
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Clients/BearerTokenClientTest.php
Expand Up @@ -10,7 +10,7 @@
/**
* Class BearerTokenClientTest
*
* @package AlibabaCloud\Client\Tests\Unit\Clients
* @package AlibabaCloud\Client\Tests\Unit\Clients
*/
class BearerTokenClientTest extends TestCase
{
Expand All @@ -29,6 +29,7 @@ public function testConstruct()
// Assert
self::assertEquals($bearerToken, $client->getCredential()->getBearerToken());
self::assertInstanceOf(BearerTokenSignature::class, $client->getSignature());

return $client;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Clients/RamRoleArnClientTest.php
Expand Up @@ -10,9 +10,9 @@
use PHPUnit\Framework\TestCase;

/**
* Class RamRoleClientTest
* Class RamRoleArnClientTest
*
* @package AlibabaCloud\Client\Tests\Unit\Clients
* @package AlibabaCloud\Client\Tests\Unit\Clients
*/
class RamRoleArnClientTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/DefaultAcsClientTest.php
Expand Up @@ -14,7 +14,7 @@
/**
* Class DefaultAcsClientTest
*
* @package AlibabaCloud\Client\Tests\Unit\Client
* @package AlibabaCloud\Client\Tests\Unit
*
* @coversDefaultClass \AlibabaCloud\Client\DefaultAcsClient
*/
Expand Down
65 changes: 65 additions & 0 deletions tests/Unit/HelpersTest.php
@@ -0,0 +1,65 @@
<?php

namespace AlibabaCloud\Client\Tests\Unit;

use PHPUnit\Framework\TestCase;

/**
* Class HelpersTest
*
* @package AlibabaCloud\Client\Tests\Unit
*/
class HelpersTest extends TestCase
{
public static function testDefault()
{
self::assertEquals('default', env('default', 'default'));
}

public static function testEnv()
{
self::assertEquals(null, env('null'));
}

public static function testSwitch()
{
putenv('TRUE=true');
self::assertEquals('true', getenv('TRUE'));
self::assertEquals(true, env('TRUE'));
putenv('TRUE=(true)');
self::assertEquals('(true)', getenv('TRUE'));
self::assertEquals(true, env('TRUE'));

putenv('FALSE=false');
self::assertEquals('false', getenv('FALSE'));
self::assertEquals(false, env('FALSE'));
putenv('FALSE=(false)');
self::assertEquals('(false)', getenv('FALSE'));
self::assertEquals(false, env('FALSE'));

putenv('EMPTY=empty');
self::assertEquals('empty', getenv('EMPTY'));
self::assertEquals(false, env('EMPTY'));
putenv('EMPTY=(empty)');
self::assertEquals('(empty)', getenv('EMPTY'));
self::assertEquals('', env('EMPTY'));

putenv('NULL=null');
self::assertEquals('null', getenv('NULL'));
self::assertEquals(null, env('NULL'));
putenv('NULL=(null)');
self::assertEquals('(null)', getenv('NULL'));
self::assertEquals(null, env('NULL'));
}

public static function testString()
{
putenv('STRING="Alibaba Cloud"');
self::assertEquals('"Alibaba Cloud"', getenv('STRING'));
self::assertEquals('Alibaba Cloud', env('STRING'));

putenv('STRING="Alibaba Cloud');
self::assertEquals('"Alibaba Cloud', getenv('STRING'));
self::assertEquals('"Alibaba Cloud', env('STRING'));
}
}

0 comments on commit 9a14785

Please sign in to comment.