summaryrefslogtreecommitdiff
path: root/server/vendor/php-opencloud/common/tests/unit/Common/Api/ParameterTest.php
blob: 65d476061782d915d0fcbf16108f68138c619ab1 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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');
    }
}