summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/tests/unit/TestCase.php
diff options
context:
space:
mode:
authormanzerbredes <loic.guegan_secondary@yahoo.fr>2016-03-15 16:17:39 +0100
committermanzerbredes <loic.guegan_secondary@yahoo.fr>2016-03-15 16:17:39 +0100
commit26d10bc0fa4befbac54453228ae1ce89021bdec2 (patch)
tree029d7240ecf7416205e5f76cf9107a6b5bdf8ca3 /server/vendor/php-opencloud/common/tests/unit/TestCase.php
parent8ad216dedf017f3d6de047a25d08db3b98e16361 (diff)
parent03ef74d0cfe675a6e18a91f039182ca1b248d8f5 (diff)
Merge branch 'develop' into loic
Diffstat (limited to 'server/vendor/php-opencloud/common/tests/unit/TestCase.php')
-rw-r--r--server/vendor/php-opencloud/common/tests/unit/TestCase.php99
1 files changed, 99 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/tests/unit/TestCase.php b/server/vendor/php-opencloud/common/tests/unit/TestCase.php
new file mode 100644
index 0000000..610d88a
--- /dev/null
+++ b/server/vendor/php-opencloud/common/tests/unit/TestCase.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace OpenCloud\Test;
+
+use function GuzzleHttp\Psr7\stream_for;
+use function GuzzleHttp\Psr7\parse_response;
+use GuzzleHttp\ClientInterface;
+use GuzzleHttp\Psr7\Response;
+use Prophecy\Argument;
+
+abstract class TestCase extends \PHPUnit_Framework_TestCase
+{
+ /** @var \Prophecy\Prophecy\ObjectProphecy */
+ protected $client;
+
+ /** @var string */
+ protected $rootFixturesDir;
+
+ protected $api;
+
+ protected function setUp()
+ {
+ $this->client = $this->prophesize(ClientInterface::class);
+ }
+
+ protected function createResponse($status, array $headers, array $json)
+ {
+ return new Response($status, $headers, stream_for(json_encode($json)));
+ }
+
+ protected function getFixture($file)
+ {
+ if (!$this->rootFixturesDir) {
+ throw new \RuntimeException('Root fixtures dir not set');
+ }
+
+ $path = $this->rootFixturesDir . '/Fixtures/' . $file . '.resp';
+
+ if (!file_exists($path)) {
+ throw new \RuntimeException(sprintf("%s does not exist", $path));
+ }
+
+ return parse_response(file_get_contents($path));
+ }
+
+ protected function setupMock($method, $path, $body = null, array $headers = [], $response)
+ {
+ $options = ['headers' => $headers];
+
+ if (!empty($body)) {
+ $options[is_array($body) ? 'json' : 'body'] = $body;
+ }
+
+ if (is_string($response)) {
+ $response = $this->getFixture($response);
+ }
+
+ $this->client
+ ->request($method, $path, $options)
+ ->shouldBeCalled()
+ ->willReturn($response);
+ }
+
+ protected function createFn($receiver, $method, $args)
+ {
+ return function () use ($receiver, $method, $args) {
+ return $receiver->$method($args);
+ };
+ }
+
+ protected function listTest(callable $call, $urlPath, $modelName = null, $responseFile = null)
+ {
+ $modelName = $modelName ?: $urlPath;
+ $responseFile = $responseFile ?: $urlPath;
+
+ $this->setupMock('GET', $urlPath, null, [], $responseFile);
+
+ $resources = call_user_func($call);
+
+ $this->assertInstanceOf('\Generator', $resources);
+
+ $count = 0;
+
+ foreach ($resources as $resource) {
+ $this->assertInstanceOf('OpenStack\Identity\v3\Models\\' . ucfirst($modelName), $resource);
+ ++$count;
+ }
+
+ $this->assertEquals(2, $count);
+ }
+
+ protected function getTest(callable $call, $modelName)
+ {
+ $resource = call_user_func($call);
+
+ $this->assertInstanceOf('OpenStack\Identity\v3\Models\\' . ucfirst($modelName), $resource);
+ $this->assertEquals('id', $resource->id);
+ }
+}