| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- <?php
- namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Request;
- use PHPUnit\Framework\TestCase;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Request\RpcRequest;
- use AlibabaCloud\Client\Request\RoaRequest;
- use AlibabaCloud\Client\Exception\ServerException;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Tests\Mock\Services\Rds\DeleteDatabaseRequest;
- /**
- * Class RequestTest
- *
- * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Request
- *
- * @coversDefaultClass \AlibabaCloud\Client\Request\Request
- */
- class RequestTest extends TestCase
- {
- /**
- * @throws ClientException
- */
- public static function testIsset()
- {
- // Setup
- $request = new DeleteDatabaseRequest();
- // Get
- self::assertFalse(isset($request->object));
- // Set
- $request->object = 'object';
- // Isset
- self::assertTrue(isset($request->object));
- self::assertEquals('object', $request->object);
- // Unset
- unset($request->object);
- self::assertEquals(null, $request->object);
- }
- /**
- * @throws ClientException
- */
- public static function testRequest()
- {
- // Setup
- $request = new RpcRequest();
- // Assert
- self::assertArrayNotHasKey('verify', $request->options);
- // Test
- $request->verify(true);
- // Assert
- self::assertTrue($request->options['verify']);
- }
- /**
- * @throws ClientException
- */
- public function testConstruct()
- {
- // Setup
- $options = ['testConstruct' => __METHOD__];
- putenv('DEBUG=sdk');
- // Test
- $rpcRequest = new RpcRequest($options);
- $roaRequest = new RoaRequest($options);
- // Assert
- self::assertEquals(__METHOD__, $rpcRequest->options['testConstruct']);
- self::assertEquals(__METHOD__, $roaRequest->options['testConstruct']);
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Name cannot be empty
- * @throws ClientException
- */
- public function testAppendUserAgentWithNameEmpty()
- {
- $request = new RpcRequest();
- $request->appendUserAgent('', 'value');
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Name must be a string
- * @throws ClientException
- */
- public function testAppendUserAgentWithNameFormat()
- {
- $request = new RpcRequest();
- $request->appendUserAgent(null, 'value');
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Value cannot be empty
- * @throws ClientException
- */
- public function testAppendUserAgentWithValueEmpty()
- {
- $request = new RpcRequest();
- $request->appendUserAgent('name', '');
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Value must be a string
- * @throws ClientException
- */
- public function testAppendUserAgentWithValueFormat()
- {
- $request = new RpcRequest();
- $request->appendUserAgent('name', null);
- }
- /**
- * @throws ClientException
- */
- public function testFormat()
- {
- // Setup
- $format = 'rss';
- $rpcRequest = new RpcRequest();
- $roaRequest = new RoaRequest();
- // Test
- $rpcRequest->format($format);
- $roaRequest->format($format);
- // Assert
- self::assertEquals(\strtoupper($format), $rpcRequest->format);
- self::assertEquals(\strtoupper($format), $roaRequest->format);
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Format cannot be empty
- * @throws ClientException
- */
- public function testFormatWithEmpty()
- {
- $request = new RpcRequest();
- $request->format('');
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Format must be a string
- * @throws ClientException
- */
- public function testFormatWithNull()
- {
- $request = new RpcRequest();
- $request->format(null);
- }
- /**
- * @throws ClientException
- */
- public function testBody()
- {
- // Setup
- $body = 'rss';
- $rpcRequest = new RpcRequest();
- $roaRequest = new RoaRequest();
- // Test
- $rpcRequest->body($body);
- $roaRequest->body($body);
- // Assert
- self::assertEquals($body, $rpcRequest->options['body']);
- self::assertEquals($body, $roaRequest->options['body']);
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Body cannot be empty
- * @throws ClientException
- */
- public function testBodyEmpty()
- {
- $request = new RpcRequest();
- $request->body('');
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Body must be a string
- * @throws ClientException
- */
- public function testBodyNotString()
- {
- $request = new RpcRequest();
- $request->body(null);
- }
- /**
- * @throws ClientException
- */
- public function testJsonBody()
- {
- // Setup
- $body = ['test' => 'test'];
- $rpcRequest = new RpcRequest();
- $roaRequest = new RoaRequest();
- // Test
- $rpcRequest->jsonBody($body);
- $roaRequest->jsonBody($body);
- // Assert
- self::assertEquals('{"test":"test"}', $rpcRequest->options['body']);
- self::assertEquals('{"test":"test"}', $roaRequest->options['body']);
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage jsonBody only accepts an array or object
- * @throws ClientException
- */
- public function testJsonBodyFormat()
- {
- $request = new RpcRequest();
- $request->jsonBody(null);
- }
- /**
- * @throws ClientException
- */
- public function testScheme()
- {
- // Setup
- $scheme = 'no';
- $rpcRequest = new RpcRequest();
- $roaRequest = new RoaRequest();
- // Test
- $rpcRequest->scheme($scheme);
- $roaRequest->scheme($scheme);
- // Assert
- self::assertEquals($scheme, $rpcRequest->uri->getScheme());
- self::assertEquals($scheme, $roaRequest->uri->getScheme());
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Scheme must be a string
- * @throws ClientException
- */
- public function testSchemeFormat()
- {
- $request = new RpcRequest();
- $request->scheme(null);
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Scheme cannot be empty
- * @throws ClientException
- */
- public function testSchemeEmpty()
- {
- $request = new RpcRequest();
- $request->scheme('');
- }
- /**
- * @throws ClientException
- */
- public function testHost()
- {
- // Setup
- $host = 'domain';
- $rpcRequest = new RpcRequest();
- $roaRequest = new RoaRequest();
- // Test
- $rpcRequest->host($host);
- $roaRequest->host($host);
- // Assert
- self::assertEquals($host, $rpcRequest->uri->getHost());
- self::assertEquals($host, $roaRequest->uri->getHost());
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Host must be a string
- * @throws ClientException
- */
- public function testHostFormat()
- {
- $request = new RpcRequest();
- $request->host(null);
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Host cannot be empty
- * @throws ClientException
- */
- public function testHostEmpty()
- {
- $request = new RpcRequest();
- $request->host('');
- }
- /**
- * @throws ClientException
- */
- public function testMethod()
- {
- // Setup
- $method = 'method';
- $rpcRequest = new RpcRequest();
- $roaRequest = new RoaRequest();
- // Test
- $rpcRequest->method($method);
- $roaRequest->method($method);
- // Assert
- self::assertEquals(\strtoupper($method), $rpcRequest->method);
- self::assertEquals(\strtoupper($method), $roaRequest->method);
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Method cannot be empty
- * @throws ClientException
- */
- public function testMethodEmpty()
- {
- $request = new RpcRequest();
- $request->method('');
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Method must be a string
- * @throws ClientException
- */
- public function testMethodFormat()
- {
- $request = new RpcRequest();
- $request->method(null);
- }
- /**
- * @throws ClientException
- */
- public function testClient()
- {
- // Setup
- $clientName = 'clientName';
- $rpcRequest = new RpcRequest();
- $roaRequest = new RoaRequest();
- // Test
- $rpcRequest->client($clientName);
- $roaRequest->client($clientName);
- // Assert
- self::assertEquals(strtolower($clientName), $rpcRequest->client);
- self::assertEquals(strtolower($clientName), $roaRequest->client);
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Client Name cannot be empty
- * @throws ClientException
- */
- public function testClientEmpty()
- {
- $request = new RpcRequest();
- $request->client('');
- }
- /**
- * @expectedException \AlibabaCloud\Client\Exception\ClientException
- * @expectedExceptionMessage Client Name must be a string
- * @throws ClientException
- */
- public function testClientFormat()
- {
- $request = new RpcRequest();
- $request->client(null);
- }
- /**
- * @throws ClientException
- */
- public function testIsDebug()
- {
- AlibabaCloud::accessKeyClient('key', 'secret')
- ->name('temp');
- $request = (new DeleteDatabaseRequest())->client('temp')
- ->debug(false);
- self::assertFalse($request->isDebug());
- unset($request->options['debug']);
- AlibabaCloud::get('temp')->debug(false);
- self::assertFalse($request->isDebug());
- unset($request->options['debug'], AlibabaCloud::get('temp')->options['debug']);
- self::assertFalse($request->isDebug());
- }
- /**
- * @throws ClientException
- */
- public function testRequestWithServiceException()
- {
- // Setup
- $request = new DeleteDatabaseRequest();
- AlibabaCloud::accessKeyClient('key', 'secret')
- ->regionId('cn-hangzhou')
- ->connectTimeout(15)
- ->timeout(10)
- ->name('temp');
- try {
- $request->client('temp')
- ->request();
- } catch (ServerException $e) {
- self::assertEquals('Specified access key is not found.', $e->getErrorMessage());
- } catch (ClientException $e) {
- self::assertStringStartsWith('cURL error', $e->getErrorMessage());
- }
- }
- /**
- * @throws ServerException
- * @throws ClientException
- */
- public function testRequestWithClientException()
- {
- // Setup
- $request = new DeleteDatabaseRequest();
- AlibabaCloud::accessKeyClient('key', 'secret')
- ->regionId('cn-hangzhou')
- ->name('temp');
- try {
- $request->client('temp')
- ->timeout(0.01)
- ->request();
- } catch (ClientException $e) {
- self::assertStringStartsWith('cURL error', $e->getErrorMessage());
- }
- }
- }
|