QueryParamsNormalizerTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 41
dl 0
loc 93
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A itThrowAnExceptionOnUnsupportedCollectionFormat() 0 17 1
A itNormalizeQueryParameters() 0 14 1
A getValidQueryParameters() 0 10 1
A itTransformCollectionFormatIntoArray() 0 18 1
A toObject() 0 3 1
A getValidCollectionFormat() 0 7 1
1
<?php
2
namespace ElevenLabs\Api\Normalizer;
3
4
use PHPUnit\Framework\TestCase;
5
6
class QueryParamsNormalizerTest extends TestCase
7
{
8
    /**
9
     * @test
10
     * @dataProvider getValidQueryParameters
11
     */
12
    public function itNormalizeQueryParameters($schemaType, $actualValue, $expectedValue)
13
    {
14
        $jsonSchema = $this->toObject([
15
            'type' => 'object',
16
            'properties' => [
17
                'param' => [
18
                    'type' => $schemaType
19
                ]
20
            ]
21
        ]);
22
23
        $normalizedValue = QueryParamsNormalizer::normalize(['param' => $actualValue], $jsonSchema);
24
25
        assertThat($normalizedValue['param'], equalTo($expectedValue));
26
    }
27
28
29
    public function getValidQueryParameters()
30
    {
31
        return [
32
            // description => [schemaType, actual, expected]
33
            'with an integer' => ['integer', '123', 123 ],
34
            'with a number' => ['number', '12.15', 12.15 ],
35
            'with true given as a string' => ['boolean', 'true', true ],
36
            'with true given as a numeric' => ['boolean', '1', true ],
37
            'with false given as a string' => ['boolean', 'false', false ],
38
            'with false given as a numeric string' => ['boolean', '0', false ]
39
        ];
40
    }
41
42
    /**
43
     * @test
44
     * @dataProvider getValidCollectionFormat
45
     */
46
    public function itTransformCollectionFormatIntoArray($collectionFormat, $rawValue, array $expectedValue)
47
    {
48
        $jsonSchema = $this->toObject([
49
            'type' => 'object',
50
            'properties' => [
51
                'param' => [
52
                    'type' => 'array',
53
                    'items' => [
54
                        'string'
55
                    ],
56
                    'collectionFormat' => $collectionFormat
57
                ]
58
            ]
59
        ]);
60
61
        $normalizedValue = QueryParamsNormalizer::normalize(['param' => $rawValue], $jsonSchema);
62
63
        assertThat($normalizedValue['param'], equalTo($expectedValue));
64
    }
65
66
    public function getValidCollectionFormat()
67
    {
68
        return [
69
            'with csv' => ['csv', 'foo,bar,baz', ['foo','bar','baz']],
70
            'with ssv' => ['ssv', 'foo bar baz', ['foo','bar','baz']],
71
            'with pipes' => ['pipes', 'foo|bar|baz', ['foo','bar','baz']],
72
            'with tabs' => ['tsv', "foo\tbar\tbaz", ['foo','bar','baz']]
73
        ];
74
    }
75
76
    /** @test */
77
    public function itThrowAnExceptionOnUnsupportedCollectionFormat()
78
    {
79
        $this->expectException(\InvalidArgumentException::class);
80
        $this->expectExceptionMessage('unknown is not a supported query collection format');
81
82
        $jsonSchema = $this->toObject([
83
            'type' => 'object',
84
            'properties' => [
85
                'param' => [
86
                    'type' => 'array',
87
                    'items' => ['string'],
88
                    'collectionFormat' => 'unknown'
89
                ]
90
            ]
91
        ]);
92
93
        QueryParamsNormalizer::normalize(['param' => 'foo%bar'], $jsonSchema);
94
    }
95
96
    private function toObject(array $array)
97
    {
98
        return json_decode(json_encode($array));
99
    }
100
}
101