summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/tests/unit/Common/Resource/AbstractResourceTest.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/Common/Resource/AbstractResourceTest.php
parent8ad216dedf017f3d6de047a25d08db3b98e16361 (diff)
parent03ef74d0cfe675a6e18a91f039182ca1b248d8f5 (diff)
Merge branch 'develop' into loic
Diffstat (limited to 'server/vendor/php-opencloud/common/tests/unit/Common/Resource/AbstractResourceTest.php')
-rw-r--r--server/vendor/php-opencloud/common/tests/unit/Common/Resource/AbstractResourceTest.php161
1 files changed, 161 insertions, 0 deletions
diff --git a/server/vendor/php-opencloud/common/tests/unit/Common/Resource/AbstractResourceTest.php b/server/vendor/php-opencloud/common/tests/unit/Common/Resource/AbstractResourceTest.php
new file mode 100644
index 0000000..f334292
--- /dev/null
+++ b/server/vendor/php-opencloud/common/tests/unit/Common/Resource/AbstractResourceTest.php
@@ -0,0 +1,161 @@
+<?php
+
+namespace OpenCloud\Test\Common\Resource;
+
+use function GuzzleHttp\Psr7\stream_for;
+use GuzzleHttp\Psr7\Response;
+use OpenCloud\Common\Resource\AbstractResource;
+use OpenCloud\Common\Resource\Generator;
+use OpenCloud\Test\Fixtures\ComputeV2Api;
+use OpenCloud\Test\TestCase;
+use Prophecy\Argument;
+
+class AbstractResourceTest extends TestCase
+{
+ private $resource;
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->rootFixturesDir = __DIR__;
+ $this->resource = new TestResource($this->client->reveal(), new ComputeV2Api());
+ }
+
+ public function test_it_populates_from_response()
+ {
+ $response = new Response(200, ['Content-Type' => 'application/json'], stream_for(
+ json_encode(['foo' => ['bar' => '1']])
+ ));
+
+ $this->resource->populateFromResponse($response);
+
+ $this->assertEquals('1', $this->resource->bar);
+ }
+
+ public function test_it_populates_datetimes_from_arrays()
+ {
+ $dt = new \DateTimeImmutable('2015');
+
+ $this->resource->populateFromArray(['created' => '2015']);
+
+ $this->assertEquals($this->resource->created, $dt);
+ }
+
+ public function test_it_populates_arrays_from_arrays()
+ {
+ $this->resource->populateFromArray(['children' => [$this->resource, $this->resource]]);
+
+ $this->assertInstanceOf(TestResource::class, $this->resource->children[0]);
+ }
+
+ public function test_it_gets_attrs()
+ {
+ $this->resource->bar = 'foo';
+
+ $this->assertEquals(['bar' => 'foo'], $this->resource->getAttrs(['bar']));
+ }
+
+ public function test_it_executes_with_state()
+ {
+ $this->resource->id = 'foo';
+ $this->resource->bar = 'bar';
+
+ $expectedJson = ['id' => 'foo', 'bar' => 'bar'];
+
+ $this->setupMock('GET', 'foo', $expectedJson, [], new Response(204));
+
+ $this->resource->executeWithState((new ComputeV2Api())->test());
+ }
+
+ public function test_it_executes_operations_until_a_204_is_received()
+ {
+ $this->client
+ ->request('GET', 'servers', ['headers' => []])
+ ->shouldBeCalled()
+ ->willReturn($this->getFixture('servers-page1'));
+
+ $this->client
+ ->request('GET', 'servers', ['query' => ['marker' => '5'], 'headers' => []])
+ ->shouldBeCalled()
+ ->willReturn(new Response(204));
+
+ $count = 0;
+
+ $api = new ComputeV2Api();
+
+ foreach ($this->resource->enumerate($api->getServers()) as $item) {
+ $count++;
+ $this->assertInstanceOf(TestResource::class, $item);
+ }
+
+ $this->assertEquals(5, $count);
+ }
+
+ public function test_it_invokes_function_if_provided()
+ {
+ $this->client
+ ->request('GET', 'servers', ['headers' => []])
+ ->shouldBeCalled()
+ ->willReturn($this->getFixture('servers-page1'));
+
+ $this->client
+ ->request('GET', 'servers', ['query' => ['marker' => '5'], 'headers' => []])
+ ->shouldBeCalled()
+ ->willReturn(new Response(204));
+
+ $api = new ComputeV2Api();
+
+ $count = 0;
+
+ $fn = function () use (&$count) {
+ $count++;
+ };
+
+ foreach ($this->resource->enumerate($api->getServers(), [], $fn) as $item) {
+ }
+
+ $this->assertEquals(5, $count);
+ }
+
+ public function test_it_halts_when_user_provided_limit_is_reached()
+ {
+ $this->client
+ ->request('GET', 'servers', ['query' => ['limit' => 2], 'headers' => []])
+ ->shouldBeCalled()
+ ->willReturn($this->getFixture('servers-page1'));
+
+ $count = 0;
+
+ $api = new ComputeV2Api();
+
+ foreach ($this->resource->enumerate($api->getServers(), ['limit' => 2]) as $item) {
+ $count++;
+ }
+
+ $this->assertEquals(2, $count);
+ }
+}
+
+class TestResource extends AbstractResource
+{
+ protected $resourceKey = 'foo';
+ protected $resourcesKey = 'servers';
+ protected $markerKey = 'id';
+
+ /** @var string */
+ public $bar;
+
+ public $id;
+
+ /** @var \DateTimeImmutable */
+ public $created;
+
+ /** @var []TestResource */
+ public $children;
+
+ public function getAttrs(array $keys)
+ {
+ return parent::getAttrs($keys);
+ }
+}