mirror of
https://github.com/awslabs/aws-crt-php.git
synced 2026-08-17 17:47:12 +00:00
Cleanup pass based on prior PRs (#21)
* Updates to stub generation * More idiomatic fixes from PHP team * Moved tests from src/tests -> tests, got rid of extension-only tests * fixed up test paths, regenerated arginfo
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
use AWS\CRT\Auth\AwsCredentials as AwsCredentials;
|
||||
use AWS\CRT\Auth\StaticCredentialsProvider as StaticCredentialsProvider;
|
||||
|
||||
require_once('common.inc');
|
||||
|
||||
final class CredentialsTest extends CrtTestCase {
|
||||
|
||||
public function testEmptyCredentials() {
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$creds = new AwsCredentials(AwsCredentials::defaults());
|
||||
$this->assertNotNull($creds, "Failed to create default/empty credentials");
|
||||
$creds = null;
|
||||
}
|
||||
|
||||
private function getCredentialsConfig() {
|
||||
$options = AwsCredentials::defaults();
|
||||
$options['access_key_id'] = 'TESTAWSACCESSKEYID';
|
||||
$options['secret_access_key'] = 'TESTSECRETaccesskeyThatDefinitelyDoesntWork';
|
||||
$options['session_token'] = 'ThisIsMyTestSessionTokenIMadeItUpMyself';
|
||||
$options['expiration_timepoint_seconds'] = 42;
|
||||
return $options;
|
||||
}
|
||||
|
||||
public function testCredentialsLifetime() {
|
||||
$options = $this->getCredentialsConfig();
|
||||
$creds = new AwsCredentials($options);
|
||||
$this->assertNotNull($creds, "Failed to create Credentials with options");
|
||||
$this->assertEquals($creds->access_key_id, $options['access_key_id']);
|
||||
$this->assertEquals($creds->secret_access_key, $options['secret_access_key']);
|
||||
$this->assertEquals($creds->session_token, $options['session_token']);
|
||||
$this->assertEquals($creds->expiration_timepoint_seconds, $options['expiration_timepoint_seconds']);
|
||||
$creds = null;
|
||||
}
|
||||
|
||||
public function testStaticCredentialsProviderLifetime() {
|
||||
$options = $this->getCredentialsConfig();
|
||||
$provider = new StaticCredentialsProvider($options);
|
||||
$this->assertNotNull($provider, "Failed to create StaticCredentialsProvider");
|
||||
$provider = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
use AWS\CRT\CRT;
|
||||
|
||||
final class ErrorTest extends PHPUnit_Framework_TestCase {
|
||||
private static $crt = null;
|
||||
public static function setUpBeforeClass() {
|
||||
self::$crt = new CRT();
|
||||
}
|
||||
|
||||
public static function tearDownAfterClass() {
|
||||
self::$crt = null;
|
||||
}
|
||||
|
||||
public function testNoInitialError() {
|
||||
$this->assertEquals(0, CRT::last_error());
|
||||
}
|
||||
|
||||
public function testCanResolveErrorName() {
|
||||
$this->assertEquals("AWS_ERROR_SUCCESS", CRT::error_name(0));
|
||||
}
|
||||
|
||||
public function testCanResolveErrorStr() {
|
||||
$this->assertEquals("Success.", CRT::error_str(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
use AWS\CRT\IO\EventLoopGroup as EventLoopGroup;
|
||||
|
||||
require_once('common.inc');
|
||||
|
||||
final class EventLoopGroupTest extends CrtTestCase {
|
||||
|
||||
public function testLifetime() {
|
||||
$elg = new EventLoopGroup();
|
||||
$this->assertNotNull($elg, "Failed to create default EventLoopGroup");
|
||||
$elg = null;
|
||||
}
|
||||
|
||||
public function testConstructionWithOptions() {
|
||||
$options = EventLoopGroup::defaults();
|
||||
$options['num_threads'] = 1;
|
||||
$elg = new EventLoopGroup($options);
|
||||
$this->assertNotNull($elg, "Failed to create EventLoopGroup with 1 thread");
|
||||
$elg = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
use AWS\CRT\HTTP\Headers;
|
||||
use AWS\CRT\HTTP\Request;
|
||||
use AWS\CRT\HTTP\Response;
|
||||
|
||||
require_once('common.inc');
|
||||
|
||||
final class HttpMessageTest extends CrtTestCase {
|
||||
public function testHeaders() {
|
||||
$headers = new Headers();
|
||||
$this->assertSame(0, $headers->count());
|
||||
}
|
||||
|
||||
public function testHeadersMarshalling() {
|
||||
$headers_array = [
|
||||
"host" => "s3.amazonaws.com",
|
||||
"test" => "this is a test header value"
|
||||
];
|
||||
$headers = new Headers($headers_array);
|
||||
$this->assertSame(2, $headers->count());
|
||||
$this->assertSame($headers_array['host'], $headers->get('host'));
|
||||
$this->assertSame($headers_array['test'], $headers->get('test'));
|
||||
$buffer = Headers::marshall($headers);
|
||||
$headers_copy = Headers::unmarshall($buffer);
|
||||
$this->assertSame(2, $headers_copy->count());
|
||||
$this->assertSame($headers_array['host'], $headers_copy->get('host'));
|
||||
$this->assertSame($headers_array['test'], $headers_copy->get('test'));
|
||||
}
|
||||
|
||||
private function assertMessagesMatch($a, $b) {
|
||||
$this->assertSame($a->method(), $b->method());
|
||||
$this->assertSame($a->path(), $b->path());
|
||||
$this->assertSame($a->query(), $b->query());
|
||||
$this->assertSame($a->headers()->toArray(), $b->headers()->toArray());
|
||||
}
|
||||
|
||||
public function testRequestMarshalling() {
|
||||
$headers = [
|
||||
"host" => "s3.amazonaws.com",
|
||||
"test" => "this is a test header value"
|
||||
];
|
||||
$method = "GET";
|
||||
$path = "/index.php";
|
||||
$query = [];
|
||||
|
||||
$msg = new Request($method, $path, $query, $headers);
|
||||
$msg_buf = Request::marshall($msg);
|
||||
$msg_copy = Request::unmarshall($msg_buf);
|
||||
|
||||
$this->assertMessagesMatch($msg, $msg_copy);
|
||||
}
|
||||
|
||||
public function testRequestMarshallingWithQueryParams() {
|
||||
$headers = [
|
||||
"host" => "s3.amazonaws.com",
|
||||
"test" => "this is a test header value"
|
||||
];
|
||||
$method = "GET";
|
||||
$path = "/index.php";
|
||||
$query = [
|
||||
'request' => '1',
|
||||
'test' => 'true',
|
||||
'answer' => '42',
|
||||
'foo' => 'bar',
|
||||
];
|
||||
|
||||
$msg = new Request($method, $path, $query, $headers);
|
||||
$msg_buf = Request::marshall($msg);
|
||||
$msg_copy = Request::unmarshall($msg_buf);
|
||||
|
||||
$this->assertMessagesMatch($msg, $msg_copy);
|
||||
}
|
||||
|
||||
public function testResponseMarshalling() {
|
||||
$headers = [
|
||||
"content-length" => "42",
|
||||
"test" => "this is a test header value"
|
||||
];
|
||||
$method = "GET";
|
||||
$path = "/index.php";
|
||||
$query = [
|
||||
'response' => '1'
|
||||
];
|
||||
|
||||
$msg = new Response($method, $path, $query, $headers, 200);
|
||||
$msg_buf = Request::marshall($msg);
|
||||
$msg_copy = Request::unmarshall($msg_buf);
|
||||
|
||||
$this->assertMessagesMatch($msg, $msg_copy);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
use AWS\CRT\IO\InputStream as InputStream;
|
||||
|
||||
require_once('common.inc');
|
||||
|
||||
final class InputStreamTest extends CrtTestCase {
|
||||
|
||||
const MEM_STREAM_CONTENTS = "THIS IS A TEST";
|
||||
|
||||
private function getMemoryStream() {
|
||||
$stream = fopen("php://memory", 'r+');
|
||||
fputs($stream, self::MEM_STREAM_CONTENTS);
|
||||
rewind($stream);
|
||||
return $stream;
|
||||
}
|
||||
|
||||
public function testMemoryStream() {
|
||||
$mem_stream = $this->getMemoryStream();
|
||||
$stream = new InputStream($mem_stream);
|
||||
$this->assertNotNull($stream, "Failed to create InputStream from PHP memory stream");
|
||||
$this->assertEquals(strlen(self::MEM_STREAM_CONTENTS), $stream->length(), "Stream length doesn't match source buffer");
|
||||
$this->assertEquals(self::MEM_STREAM_CONTENTS, $stream->read(), "Stream doesn't match source buffer");
|
||||
$this->assertTrue($stream->eof(), "Stream is not EOF after reading");
|
||||
$this->assertEquals(0, $stream->seek(0, InputStream::SEEK_BEGIN), "Unable to rewind stream");
|
||||
$this->assertFalse($stream->eof(), "Stream is EOF after rewinding");
|
||||
$this->assertEquals(0, $stream->seek(0, InputStream::SEEK_END), "Unable to seek to end of stream");
|
||||
$this->assertTrue($stream->eof(), "Stream is not EOF after seeking to end");
|
||||
$stream = null;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
--TEST--
|
||||
aws_crt_error_debug_str
|
||||
--SKIPIF--
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/skipif.inc');
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/common.inc');
|
||||
|
||||
echo aws_crt_error_debug_str(0);
|
||||
?>
|
||||
--EXPECT--
|
||||
aws-c-common: AWS_ERROR_SUCCESS, Success.
|
||||
@@ -1,14 +0,0 @@
|
||||
--TEST--
|
||||
aws_crt_error_name
|
||||
--SKIPIF--
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/skipif.inc');
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/common.inc');
|
||||
|
||||
echo aws_crt_error_name(0);
|
||||
?>
|
||||
--EXPECT--
|
||||
AWS_ERROR_SUCCESS
|
||||
@@ -1,14 +0,0 @@
|
||||
--TEST--
|
||||
aws_crt_error_str
|
||||
--SKIPIF--
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/skipif.inc');
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/common.inc');
|
||||
|
||||
echo aws_crt_error_str(0);
|
||||
?>
|
||||
--EXPECT--
|
||||
Success.
|
||||
@@ -1,16 +0,0 @@
|
||||
--TEST--
|
||||
aws_crt_event_loop_group
|
||||
--SKIPIF--
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/skipif.inc');
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/common.inc');
|
||||
|
||||
$elg = aws_crt_event_loop_group_new(1);
|
||||
var_dump($elg);
|
||||
aws_crt_event_loop_group_release($elg);
|
||||
?>
|
||||
--EXPECTREGEX--
|
||||
int\(\d+\)
|
||||
@@ -1,14 +0,0 @@
|
||||
--TEST--
|
||||
aws_crt_last_error
|
||||
--SKIPIF--
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/skipif.inc');
|
||||
?>
|
||||
--FILE--
|
||||
<?php
|
||||
require_once(dirname(__FILE__) . '/common.inc');
|
||||
|
||||
echo aws_crt_last_error();
|
||||
?>
|
||||
--EXPECT--
|
||||
0
|
||||
+14
-2
@@ -1,3 +1,15 @@
|
||||
<?php
|
||||
// Common setup for tests:
|
||||
?>
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
use AWS\CRT\CRT as CRT;
|
||||
|
||||
abstract class CrtTestCase extends PHPUnit_Framework_TestCase {
|
||||
// Ensure that after every test there are no errors in the CRT itself
|
||||
protected function assertPostConditions() {
|
||||
if (CRT::last_error()) {
|
||||
$this->fail("Test left an error on the stack: " . CRT::error_name(CRT::last_error()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
<?php
|
||||
extension_loaded('awscrt') or die('awscrt is not available in this build');
|
||||
?>
|
||||
Reference in New Issue
Block a user