summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/tests/unit/Common/Transport
diff options
context:
space:
mode:
authorEoleDev <root@serverpc.home>2016-03-09 15:37:42 +0100
committerEoleDev <root@serverpc.home>2016-03-09 15:37:42 +0100
commit1d3ed3af6d57316640c143002ddf80d61e6c098a (patch)
tree97ac49bc7ff0f16150aefee821da3557dc0d0644 /server/vendor/php-opencloud/common/tests/unit/Common/Transport
parentd69adc4f9d1f6019927de235ef84885c68e6e508 (diff)
parent08ea5ef31abcc4e23a39a780cacb64fa27f19194 (diff)
Merge branch 'Evan' into develop
Diffstat (limited to 'server/vendor/php-opencloud/common/tests/unit/Common/Transport')
-rw-r--r--server/vendor/php-opencloud/common/tests/unit/Common/Transport/HandlerStackTest.php15
-rw-r--r--server/vendor/php-opencloud/common/tests/unit/Common/Transport/JsonSerializerTest.php85
-rw-r--r--server/vendor/php-opencloud/common/tests/unit/Common/Transport/MiddlewareTest.php59
-rw-r--r--server/vendor/php-opencloud/common/tests/unit/Common/Transport/RequestSerializerTest.php112
-rw-r--r--server/vendor/php-opencloud/common/tests/unit/Common/Transport/UtilsTest.php30
5 files changed, 301 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Transport/HandlerStackTest.php b/server/vendor/php-opencloud/common/tests/unit/Common/Transport/HandlerStackTest.php
new file mode 100644
index 0000000..ec1cf85
--- /dev/null
+++ b/server/vendor/php-opencloud/common/tests/unit/Common/Transport/HandlerStackTest.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace OpenCloud\Test\Common\Transport;
+
+use GuzzleHttp\Handler\MockHandler;
+use OpenCloud\Common\Transport\HandlerStack;
+use OpenCloud\Test\TestCase;
+
+class HandlerStackTest extends TestCase
+{
+ public function test_it_is_created()
+ {
+ $this->assertInstanceOf(HandlerStack::class, HandlerStack::create(new MockHandler()));
+ }
+}
diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Transport/JsonSerializerTest.php b/server/vendor/php-opencloud/common/tests/unit/Common/Transport/JsonSerializerTest.php
new file mode 100644
index 0000000..9075594
--- /dev/null
+++ b/server/vendor/php-opencloud/common/tests/unit/Common/Transport/JsonSerializerTest.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace OpenCloud\Test\Common\Transport;
+
+use OpenCloud\Common\Api\Parameter;
+use OpenCloud\Common\Transport\JsonSerializer;
+
+class JsonSerializerTest extends \PHPUnit_Framework_TestCase
+{
+ private $serializer;
+
+ public function setUp()
+ {
+ $this->serializer = new JsonSerializer();
+ }
+
+ public function test_it_embeds_params_according_to_path()
+ {
+ $param = $this->prophesize(Parameter::class);
+ $param->isArray()->shouldBeCalled()->willReturn(false);
+ $param->isObject()->shouldBeCalled()->willReturn(false);
+ $param->getName()->shouldBeCalled()->willReturn('username');
+ $param->getPath()->shouldBeCalled()->willReturn('auth.passwordCredentials');
+
+ $userValue = 'fooBar';
+
+ $expected = [
+ 'auth' => [
+ 'passwordCredentials' => [
+ 'username' => $userValue,
+ ],
+ ],
+ ];
+
+ $actual = $this->serializer->stockJson($param->reveal(), $userValue, []);
+
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function test_it_serializes_arrays()
+ {
+ $param = $this->prophesize(Parameter::class);
+ $param->isArray()->shouldBeCalled()->willReturn(true);
+ $param->getName()->shouldBeCalled()->willReturn('fooBar');
+ $param->getPath()->shouldBeCalled()->willReturn(false);
+
+ $itemSchema = $this->prophesize(Parameter::class);
+ $itemSchema->isArray()->shouldBeCalled()->willReturn(false);
+ $itemSchema->isObject()->shouldBeCalled()->willReturn(false);
+ $itemSchema->getName()->shouldBeCalled()->willReturn(null);
+ $itemSchema->getPath()->shouldBeCalled()->willReturn(null);
+
+ $param->getItemSchema()->shouldBeCalled()->willReturn($itemSchema);
+
+ $userValues = ['1', '2', '3'];
+
+ $expected = ['fooBar' => $userValues];
+
+ $actual = $this->serializer->stockJson($param->reveal(), $userValues, []);
+
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function test_it_serializes_objects()
+ {
+ $prop = $this->prophesize(Parameter::class);
+ $prop->isArray()->shouldBeCalled()->willReturn(false);
+ $prop->isObject()->shouldBeCalled()->willReturn(false);
+ $prop->getName()->shouldBeCalled()->willReturn('foo');
+ $prop->getPath()->shouldBeCalled()->willReturn(null);
+
+ $param = $this->prophesize(Parameter::class);
+ $param->isArray()->shouldBeCalled()->willReturn(false);
+ $param->isObject()->shouldBeCalled()->willReturn(true);
+ $param->getName()->shouldBeCalled()->willReturn('topLevel');
+ $param->getPath()->shouldBeCalled()->willReturn(false);
+ $param->getProperty('foo')->shouldBeCalled()->willReturn($prop);
+
+ $expected = ['topLevel' => ['foo' => true]];
+
+ $json = $this->serializer->stockJson($param->reveal(), ['foo' => true], []);
+
+ $this->assertEquals($expected, $json);
+ }
+}
diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Transport/MiddlewareTest.php b/server/vendor/php-opencloud/common/tests/unit/Common/Transport/MiddlewareTest.php
new file mode 100644
index 0000000..c445783
--- /dev/null
+++ b/server/vendor/php-opencloud/common/tests/unit/Common/Transport/MiddlewareTest.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace unit\Common\Transport;
+
+use GuzzleHttp\Handler\MockHandler;
+use GuzzleHttp\Psr7\Request;
+use GuzzleHttp\Psr7\Response;
+use OpenCloud\Common\Transport\Middleware;
+use OpenCloud\Test\TestCase;
+use OpenCloud\Common\Auth\AuthHandler;
+
+class MiddlewareTest extends TestCase
+{
+ /**
+ * @expectedException \OpenCloud\Common\Error\BadResponseError
+ */
+ public function test_exception_is_thrown_for_4xx_statuses()
+ {
+ $middleware = Middleware::httpErrors();
+
+ $handler = new MockHandler([new Response(404)]);
+ $fn = $middleware($handler);
+
+ $promise = $fn(new Request('GET', 'http://foo.com'), []);
+ $this->assertEquals('pending', $promise->getState());
+
+ $promise->wait();
+ $this->assertEquals('rejected', $promise->getState());
+ }
+
+ public function test_responses_are_left_alone_when_status_under_400()
+ {
+ $middleware = Middleware::httpErrors();
+
+ $response = new Response(204);
+ $handler = new MockHandler([$response]);
+ $fn = $middleware($handler);
+
+ $promise = $fn(new Request('GET', 'http://foo.com'), []);
+
+ $promise->then(function ($val) use ($response) {
+ $this->assertEquals($val, $response);
+ });
+
+ $promise->wait();
+ }
+
+ public function test_auth_handler_is_returned()
+ {
+ $generator = function () {};
+
+ $middleware = Middleware::authHandler($generator);
+
+ $handler = new MockHandler([new Response(204)]);
+ $fn = $middleware($handler);
+
+ $this->assertInstanceOf(AuthHandler::class, $fn);
+ }
+}
diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Transport/RequestSerializerTest.php b/server/vendor/php-opencloud/common/tests/unit/Common/Transport/RequestSerializerTest.php
new file mode 100644
index 0000000..cf06220
--- /dev/null
+++ b/server/vendor/php-opencloud/common/tests/unit/Common/Transport/RequestSerializerTest.php
@@ -0,0 +1,112 @@
+<?php
+
+namespace OpenCloud\Test\Common\Transport;
+
+use OpenCloud\Common\Api\Operation;
+use OpenCloud\Common\Api\Parameter;
+use OpenCloud\Common\Transport\JsonSerializer;
+use OpenCloud\Common\Transport\RequestSerializer;
+use OpenCloud\Test\TestCase;
+
+class RequestSerializerTest extends TestCase
+{
+ private $rs;
+ private $js;
+
+ public function setUp()
+ {
+ $this->js = $this->prophesize(JsonSerializer::class);
+
+ $this->rs = new RequestSerializer($this->js->reveal());
+ }
+
+ public function test_it_ignores_undefined_params()
+ {
+ $op = $this->prophesize(Operation::class);
+ $op->getParam('foo')->shouldBeCalled()->willReturn(null);
+
+ $this->assertEquals(['headers' => []], $this->rs->serializeOptions($op->reveal(), ['foo' => 'bar']));
+ }
+
+ public function test_it_serializes_queries()
+ {
+ $sch = $this->prophesize(Parameter::class);
+ $sch->getName()->shouldBeCalled()->willReturn('fooAlias');
+ $sch->getLocation()->shouldBeCalled()->willReturn('query');
+
+ $op = $this->prophesize(Operation::class);
+ $op->getParam('foo')->shouldBeCalled()->willReturn($sch);
+
+ $actual = $this->rs->serializeOptions($op->reveal(), ['foo' => 'bar']);
+ $expected = ['query' => ['fooAlias' => 'bar'], 'headers' => []];
+
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function test_it_serializes_headers()
+ {
+ $sch = $this->prophesize(Parameter::class);
+ $sch->getLocation()->shouldBeCalled()->willReturn('header');
+ $sch->getName()->shouldBeCalled()->willReturn('fooAlias');
+ $sch->getPrefixedName()->shouldBeCalled()->willReturn('prefix-fooAlias');
+
+ $op = $this->prophesize(Operation::class);
+ $op->getParam('foo')->shouldBeCalled()->willReturn($sch);
+
+ $actual = $this->rs->serializeOptions($op->reveal(), ['foo' => 'bar']);
+ $expected = ['headers' => ['prefix-fooAlias' => 'bar']];
+
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function test_it_serializes_metadata_headers()
+ {
+ $itemSch = $this->prophesize(Parameter::class);
+ $itemSch->getName()->shouldBeCalled()->willReturn('foo');
+ $itemSch->getPrefixedName()->shouldBeCalled()->willReturn('prefix-foo');
+
+ $sch = $this->prophesize(Parameter::class);
+ $sch->getItemSchema()->shouldBeCalled()->willReturn($itemSch);
+ $sch->getLocation()->shouldBeCalled()->willReturn('header');
+ $sch->getName()->shouldBeCalled()->willReturn('metadata');
+
+ $op = $this->prophesize(Operation::class);
+ $op->getParam('metadata')->shouldBeCalled()->willReturn($sch);
+
+ $actual = $this->rs->serializeOptions($op->reveal(), ['metadata' => ['foo' => 'bar']]);
+ $expected = ['headers' => ['prefix-foo' => 'bar']];
+
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function test_it_serializes_json()
+ {
+ $sch = $this->prophesize(Parameter::class);
+ $sch->getLocation()->shouldBeCalled()->willReturn('json');
+
+ $op = $this->prophesize(Operation::class);
+ $op->getParam('foo')->shouldBeCalled()->willReturn($sch);
+ $op->getJsonKey()->shouldBeCalled()->willReturn('jsonKey');
+
+ $this->js->stockJson($sch, 'bar', [])->shouldBeCalled()->willReturn(['foo' => 'bar']);
+
+ $actual = $this->rs->serializeOptions($op->reveal(), ['foo' => 'bar']);
+ $expected = ['json' => ['jsonKey' => ['foo' => 'bar']], 'headers' => []];
+
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function test_it_serializes_raw_vals()
+ {
+ $sch = $this->prophesize(Parameter::class);
+ $sch->getLocation()->shouldBeCalled()->willReturn('raw');
+
+ $op = $this->prophesize(Operation::class);
+ $op->getParam('foo')->shouldBeCalled()->willReturn($sch);
+
+ $actual = $this->rs->serializeOptions($op->reveal(), ['foo' => 'bar']);
+ $expected = ['body' => 'bar', 'headers' => []];
+
+ $this->assertEquals($expected, $actual);
+ }
+}
diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Transport/UtilsTest.php b/server/vendor/php-opencloud/common/tests/unit/Common/Transport/UtilsTest.php
new file mode 100644
index 0000000..3103ad4
--- /dev/null
+++ b/server/vendor/php-opencloud/common/tests/unit/Common/Transport/UtilsTest.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace OpenCloud\Test\Common\Transport;
+
+use GuzzleHttp\Psr7\Response;
+use function GuzzleHttp\Psr7\uri_for;
+use GuzzleHttp\Psr7\Uri;
+use OpenCloud\Common\Transport\Utils;
+use OpenCloud\Test\TestCase;
+
+class UtilsTest extends TestCase
+{
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function test_decoding_malformed_json_throws_error()
+ {
+ $response = new Response(200, [], \GuzzleHttp\Psr7\stream_for('{'));
+
+ Utils::jsonDecode($response);
+ }
+
+ public function test_it_adds_paths()
+ {
+ $uri = Utils::addPaths(uri_for('http://openstack.org/foo'), 'bar', 'baz', '1', '2');
+
+ $this->assertInstanceOf(Uri::class, $uri);
+ $this->assertEquals(uri_for('http://openstack.org/foo/bar/baz/1/2'), $uri);
+ }
+}