summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/tests/unit/Common/Resource/AbstractResourceTest.php
blob: f3342929df609f7008e08733f82a9f801d76c245 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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);
    }
}