diff options
Diffstat (limited to 'server/vendor/php-opencloud/common/tests/unit/Common/Api')
5 files changed, 466 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Api/OperationTest.php b/server/vendor/php-opencloud/common/tests/unit/Common/Api/OperationTest.php new file mode 100644 index 0000000..ea0f7c8 --- /dev/null +++ b/server/vendor/php-opencloud/common/tests/unit/Common/Api/OperationTest.php @@ -0,0 +1,74 @@ +<?php + +namespace OpenCloud\Test\Common\Api; + +use OpenCloud\Common\Api\Operation; +use OpenCloud\Common\Api\Parameter; +use OpenCloud\Test\Fixtures\ComputeV2Api; + +class OperationTest extends \PHPUnit_Framework_TestCase +{ + private $operation; + + public function setUp() + { + $def = (new ComputeV2Api())->postServer(); + + $this->operation = new Operation($def); + } + + public function test_it_reveals_whether_params_are_set_or_not() + { + $this->assertFalse($this->operation->hasParam('foo')); + $this->assertTrue($this->operation->hasParam('name')); + } + + public function test_it_gets_params() + { + $this->assertInstanceOf(Parameter::class, $this->operation->getParam('name')); + } + + public function test_it_validates_params() + { + $this->assertTrue($this->operation->validate([ + 'name' => 'foo', + 'imageId' => 'bar', + 'flavorId' => 'baz', + ])); + } + + /** + * @expectedException \Exception + */ + public function test_exceptions_are_propagated() + { + $this->assertFalse($this->operation->validate([ + 'name' => true, + 'imageId' => 'bar', + 'flavorId' => 'baz', + ])); + } + + /** + * @expectedException \Exception + */ + public function test_an_exception_is_thrown_when_user_does_not_provide_required_options() + { + $this->operation->validate([]); + } + + /** + * @expectedException \Exception + */ + public function test_it_throws_exception_when_user_provides_undefined_options() + { + $userData = ['name' => 'new_server', 'undefined_opt' => 'bah']; + + $this->operation->validate($userData); + } + + public function test_it_gets_json_key() + { + $this->assertEquals('server', $this->operation->getJsonKey()); + } +} diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Api/OperatorTest.php b/server/vendor/php-opencloud/common/tests/unit/Common/Api/OperatorTest.php new file mode 100644 index 0000000..43c5804 --- /dev/null +++ b/server/vendor/php-opencloud/common/tests/unit/Common/Api/OperatorTest.php @@ -0,0 +1,137 @@ +<?php + +namespace OpenCloud\Test\Common\Api; + +use function GuzzleHttp\Psr7\uri_for; +use GuzzleHttp\Promise\Promise; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Psr7\Uri; +use OpenCloud\Common\Api\Operator; +use OpenCloud\Common\Resource\AbstractResource; +use OpenCloud\Common\Resource\ResourceInterface; +use OpenCloud\Compute\v2\Models\Server; +use OpenCloud\Test\Fixtures\ComputeV2Api; +use OpenCloud\Test\TestCase; +use Prophecy\Argument; + +class OperatorTest extends TestCase +{ + private $operator; + private $def; + + public function setUp() + { + parent::setUp(); + + $this->rootFixturesDir = __DIR__; + + $this->def = [ + 'method' => 'GET', + 'path' => 'test', + 'params' => [], + ]; + + $this->operator = new TestOperator($this->client->reveal(), new ComputeV2Api()); + } + + public function test_it_returns_operations() + { + $this->assertInstanceOf( + 'OpenCloud\Common\Api\Operation', + $this->operator->getOperation($this->def, []) + ); + } + + public function test_it_sends_a_request_when_operations_are_executed() + { + $this->client->request('GET', 'test', ['headers' => []])->willReturn(new Request('GET', 'test')); + + $this->operator->execute($this->def, []); + } + + public function test_it_sends_a_request_when_async_operations_are_executed() + { + $this->client->requestAsync('GET', 'test', ['headers' => []])->willReturn(new Promise()); + + $this->operator->executeAsync($this->def, []); + } + + public function test_it_returns_a_model_instance() + { + $this->assertInstanceOf(ResourceInterface::class, $this->operator->model(TestResource::class)); + } + + public function test_it_populates_models_from_response() + { + $this->assertInstanceOf(ResourceInterface::class, $this->operator->model(TestResource::class, new Response(200))); + } + + public function test_it_populates_models_from_arrays() + { + $data = ['flavor' => [], 'image' => []]; + $this->assertInstanceOf(ResourceInterface::class, $this->operator->model(TestResource::class, $data)); + } + + public function test_it_wraps_sequential_ops_in_promise_when_async_is_appended_to_method_name() + { + $promise = $this->operator->createAsync('something'); + + $this->assertInstanceOf(Promise::class, $promise); + + $promise->then(function ($val) { + $this->assertEquals('Created something', $val); + }); + + $promise->wait(); + } + + /** + * @expectedException \RuntimeException + */ + public function test_it_throws_exception_when_async_is_called_on_a_non_existent_method() + { + $this->operator->fooAsync(); + } + + public function test_it_retrieves_base_http_url() + { + $returnedUri = uri_for('http://foo.com'); + + $this->client->getConfig('base_uri')->shouldBeCalled()->willReturn($returnedUri); + + $uri = $this->operator->testBaseUri(); + + $this->assertInstanceOf(Uri::class, $uri); + $this->assertEquals($returnedUri, $uri); + } + + /** + * @expectedException \Exception + */ + public function test_undefined_methods_result_in_error() + { + $this->operator->foo(); + } +} + +class TestResource extends AbstractResource +{ +} + +class TestOperator extends Operator +{ + public function testBaseUri() + { + return $this->getHttpBaseUrl(); + } + + public function create($str) + { + return 'Created ' . $str; + } + + public function fail() + { + } +} diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Api/ParameterTest.php b/server/vendor/php-opencloud/common/tests/unit/Common/Api/ParameterTest.php new file mode 100644 index 0000000..65d4760 --- /dev/null +++ b/server/vendor/php-opencloud/common/tests/unit/Common/Api/ParameterTest.php @@ -0,0 +1,203 @@ +<?php + +namespace OpenCloud\Test\Common\Api; + +use OpenCloud\Common\Api\Parameter; +use OpenCloud\Test\Fixtures\ComputeV2Api; + +class ParameterTest extends \PHPUnit_Framework_TestCase +{ + const PARAMETER_CLASS = 'OpenCloud\Common\Api\Parameter'; + + private $param; + private $data; + private $api; + + public function setUp() + { + $this->api = new ComputeV2Api(); + + $this->data = $this->api->postServer()['params']['name'] + ['name' => 'name']; + $this->param = new Parameter($this->data); + } + + /** + * @expectedException \RuntimeException + */ + public function test_exception_is_thrown_for_invalid_locations() + { + $data = $this->data; + $data['location'] = 'foo'; + new Parameter($data); + } + + public function test_it_should_provide_access_to_a_name() + { + $this->assertEquals($this->data['name'], $this->param->getName()); + } + + public function test_it_should_use_sentAs_alias_for_name_if_one_is_set() + { + $data = $this->data + ['sentAs' => 'foo']; + $param = new Parameter($data); + + $this->assertEquals($data['sentAs'], $param->getName()); + } + + public function test_it_indicates_whether_it_is_required_or_not() + { + $this->assertTrue($this->param->isRequired()); + } + + public function test_it_indicates_its_item_schema() + { + $data = $this->api->postServer()['params']['networks'] + ['name' => 'networks']; + $param = new Parameter($data); + + $this->assertInstanceOf(self::PARAMETER_CLASS, $param->getItemSchema()); + } + + public function test_it_allows_property_retrieval() + { + $definition = $this->api->postServer()['params']['networks']['items'] + ['name' => 'network']; + $param = new Parameter($definition); + + $this->assertInstanceOf(self::PARAMETER_CLASS, $param->getProperty('uuid')); + } + + public function test_it_indicates_its_path() + { + $path = 'foo.bar.baz'; + $param = new Parameter($this->data + ['path' => $path]); + + $this->assertEquals($path, $param->getPath()); + } + + public function test_it_verifies_a_given_location_with_a_boolean() + { + $this->assertFalse($this->param->hasLocation('foo')); + $this->assertTrue($this->param->hasLocation('json')); + } + + public function test_it_should_return_true_when_required_attributes_are_provided_and_match_their_definitions() + { + $this->assertTrue($this->param->validate('TestName')); + } + + /** + * @expectedException \Exception + */ + public function test_it_throws_exception_when_values_do_not_match_their_definition_types() + { + $data = $this->api->postServer()['params']['networks'] + ['name' => 'networks']; + $param = new Parameter($data); + + $param->validate('a_network!'); // should be an array + } + + /** + * @expectedException \Exception + */ + public function test_it_throws_exception_when_deeply_nested_values_have_wrong_types() + { + $data = $this->api->postServer()['params']['networks'] + ['name' => 'networks']; + + $param = new Parameter($data); + $param->validate(['name' => false]); // value should be a string, not bool + } + + public function test_metadata_properties_are_handled_differently() + { + $params = [ + 'name' => 'metadata', + 'type' => 'object', + 'properties' => [ + 'type' => 'string', + ], + ]; + + $userValues = ['some' => 'value']; + + $param = new Parameter($params); + $this->assertTrue($param->validate($userValues)); + } + + public function test_it_passes_validation_when_array_values_pass() + { + $params = [ + 'name' => 'foo', + 'type' => 'array', + 'items' => ['type' => 'string'], + ]; + + $userVals = ['1', '2', '3']; + + $param = new Parameter($params); + $this->assertTrue($param->validate($userVals)); + } + + /** + * @expectedException \Exception + */ + public function test_an_exception_is_thrown_when_an_undefined_property_is_provided() + { + $params = ['type' => 'object', 'properties' => ['foo' => ['type' => 'string']]]; + $userVals = ['bar' => 'baz']; + + $param = new Parameter($params); + $param->validate($userVals); + } + + public function test_it_passes_validation_when_all_subproperties_pass() + { + $params = ['type' => 'object', 'properties' => ['foo' => ['type' => 'string']]]; + $userVals = ['foo' => 'baz']; + + $param = new Parameter($params); + $this->assertTrue($param->validate($userVals)); + } + + public function test_it_sets_name() + { + $this->param->setName('foo'); + $this->assertEquals($this->param->getName(), 'foo'); + } + + public function test_it_gets_property() + { + $property = new Parameter([ + 'name' => 'metadata', + 'properties' => [ + 'type' => 'string', + 'prefix' => 'foo', + ], + ]); + + $prop = $property->getProperty('metadata'); + + $this->assertInstanceOf(Parameter::class, $prop); + $this->assertEquals('foo', $prop->getPrefix()); + } + + public function test_it_gets_prefixed_name() + { + $property = new Parameter([ + 'name' => 'metadata', + 'prefix' => 'foo-', + ]); + + $this->assertEquals('foo-metadata', $property->getPrefixedName()); + } + + /** + * @expectedException \Exception + */ + public function test_exception_is_thrown_when_value_is_not_in_enum_list() + { + $data = $this->data; + $data['enum'] = ['foo']; + + $param = new Parameter($data); + $param->validate('blah'); + } +} diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Api/fixtures/headers.php b/server/vendor/php-opencloud/common/tests/unit/Common/Api/fixtures/headers.php new file mode 100644 index 0000000..f428992 --- /dev/null +++ b/server/vendor/php-opencloud/common/tests/unit/Common/Api/fixtures/headers.php @@ -0,0 +1,25 @@ +<?php + +return [ + 'method' => 'POST', + 'path' => 'something', + 'params' => [ + 'name' => [ + 'type' => 'string', + 'location' => 'header', + 'sentAs' => 'X-Foo-Name' + ], + 'age' => [ + 'type' => 'integer', + 'location' => 'header' + ], + 'metadata' => [ + 'type' => 'object', + 'location' => 'header', + 'items' => [ + 'prefix' => 'X-Meta-' + ] + ], + 'other' => ['type' => 'string'] // should not be a header + ], +]; diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Api/fixtures/jsonBody.php b/server/vendor/php-opencloud/common/tests/unit/Common/Api/fixtures/jsonBody.php new file mode 100644 index 0000000..350affd --- /dev/null +++ b/server/vendor/php-opencloud/common/tests/unit/Common/Api/fixtures/jsonBody.php @@ -0,0 +1,27 @@ +<?php + +return [ + 'method' => 'POST', + 'path' => 'something', + 'params' => [ + 'name' => [ + 'type' => 'string', + 'sentAs' => 'server_name', + ], + 'other' => [ + 'type' => 'array', + 'sentAs' => 'other_params', + 'items' => [ + 'type' => 'string' + ] + ], + 'etc' => [ + 'type' => 'object', + 'sentAs' => 'etcetc', + 'properties' => [ + 'dob' => ['type' => 'string'], + 'age' => ['type' => 'integer', 'sentAs' => 'current_age'], + ] + ], + ], +]; |
