Completed
Push — master ( b013a4...773920 )
by Guillem
01:52
created

SchemaTest::itCanIterateAvailableOperations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
namespace ElevenLabs\Api;
3
4
use ElevenLabs\Api\Definition\RequestDefinition;
5
use ElevenLabs\Api\Definition\RequestDefinitions;
6
use PHPUnit\Framework\TestCase;
7
8
class SchemaTest extends TestCase
9
{
10
    /** @test */
11
    public function itCanIterateAvailableOperations()
12
    {
13
        $request = $this->prophesize(RequestDefinition::class);
14
        $request->getMethod()->willReturn('GET');
15
        $request->getPathTemplate()->willReturn('/api/pets/{id}');
16
        $request->getOperationId()->willReturn('getPet');
17
18
        $requests = $this->prophesize(RequestDefinitions::class);
19
        $requests->getIterator()->willReturn(new \ArrayIterator([$request->reveal()]));
20
21
        $schema = new Schema($requests->reveal());
22
23
        $operations = $schema->getRequestDefinitions();
24
25
        assertTrue($operations instanceof \Generator);
26
27
        foreach ($operations as $operationId => $operation) {
28
            assertThat($operationId, equalTo('getPet'));
29
        }
30
    }
31
32
    /** @test */
33
    public function itCanResolveAnOperationIdFromAPathAndMethod()
34
    {
35
        $request = $this->prophesize(RequestDefinition::class);
36
        $request->getMethod()->willReturn('GET');
37
        $request->getPathTemplate()->willReturn('/api/pets/{id}');
38
        $request->getOperationId()->willReturn('getPet');
39
40
        $requests = $this->prophesize(RequestDefinitions::class);
41
        $requests->getIterator()->willReturn(new \ArrayIterator([$request->reveal()]));
42
43
        $schema = new Schema($requests->reveal());
44
45
        $operationId = $schema->findOperationId('GET', '/api/pets/1234');
46
47
        assertThat($operationId, equalTo('getPet'));
48
    }
49
50
    /** @test */
51
    public function itThrowAnExceptionWhenNoOperationIdCanBeResolved()
52
    {
53
        $this->expectException(\InvalidArgumentException::class);
54
        $this->expectExceptionMessage('Unable to resolve the operationId for path /api/pets/1234');
55
56
        $requests = $this->prophesize(RequestDefinitions::class);
57
        $requests->getIterator()->willReturn(new \ArrayIterator());
58
59
        $schema = new Schema($requests->reveal(), '/api');
60
        $schema->findOperationId('GET', '/api/pets/1234');
61
    }
62
63
    /** @test */
64
    public function itProvideARequestDefinition()
65
    {
66
        $request = $this->prophesize(RequestDefinition::class);
67
        $request->getMethod()->willReturn('GET');
68
        $request->getPathTemplate()->willReturn('/pets/{id}');
69
        $request->getOperationId()->willReturn('getPet');
70
71
        $requests = $this->prophesize(RequestDefinitions::class);
72
        $requests->getIterator()->willReturn(new \ArrayIterator([$request->reveal()]));
73
74
        $schema = new Schema($requests->reveal(), '/api');
75
        $actual = $schema->getRequestDefinition('getPet');
76
77
        assertThat($actual, equalTo($request->reveal()));
78
    }
79
80
    /** @test */
81
    public function itThrowAnExceptionWhenNoRequestDefinitionIsFound()
82
    {
83
        $this->expectException(\InvalidArgumentException::class);
84
        $this->expectExceptionMessage('Unable to get the request definition for getPet');
85
86
        $requests = $this->prophesize(RequestDefinitions::class);
87
        $requests->getIterator()->willReturn(new \ArrayIterator());
88
89
        $schema = new Schema($requests->reveal(), '/api');
90
        $schema->getRequestDefinition('getPet');
91
    }
92
93
    /** @test */
94
    public function itCanBeSerialized()
95
    {
96
        $requests = $this->prophesize(RequestDefinitions::class);
97
        $requests->getIterator()->willReturn(new \ArrayIterator());
98
99
        $schema = new Schema($requests->reveal());
100
        $serialized = serialize($schema);
101
102
        assertThat(unserialize($serialized), equalTo($schema));
103
    }
104
}
105