Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support debug by set environment variables
- Loading branch information
1 parent
9653128
commit 9a14785
Showing
11 changed files
with
213 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
} | ||
} |