New Library

This commit is contained in:
EoleDev 2016-03-09 15:36:02 +01:00
parent 5c6f6c97b7
commit c479658f0b
83 changed files with 5788 additions and 0 deletions

View file

@ -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);
}
}

View file

@ -0,0 +1,6 @@
HTTP/1.1 200 OK
Content-Type: application/json
{
"servers": []
}

View file

@ -0,0 +1,77 @@
HTTP/1.1 200 OK
Content-Type: application/json
{
"servers": [
{
"id": "1",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "bookmark"
}
],
"name": "server1"
},
{
"id": "2",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "bookmark"
}
],
"name": "server2"
},
{
"id": "3",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "bookmark"
}
],
"name": "server3"
},
{
"id": "4",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "bookmark"
}
],
"name": "server4"
},
{
"id": "5",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "bookmark"
}
],
"name": "server5"
}
]
}

View file

@ -0,0 +1,77 @@
HTTP/1.1 200 OK
Content-Type: application/json
{
"servers": [
{
"id": "6",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "bookmark"
}
],
"name": "server6"
},
{
"id": "7",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "bookmark"
}
],
"name": "server7"
},
{
"id": "8",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "bookmark"
}
],
"name": "server8"
},
{
"id": "9",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "bookmark"
}
],
"name": "server9"
},
{
"id": "10",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/servers/616fb98f-46ca-475e-917e-2563e5a8cd19",
"rel": "bookmark"
}
],
"name": "server10"
}
]
}