Completed
Pull Request — master (#14)
by Woody
01:52
created

SchemaTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 47
dl 0
loc 95
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A itCanIterateAvailableOperations() 0 18 2
A itProvideARequestDefinition() 0 14 1
A itThrowAnExceptionWhenNoOperationIdCanBeResolved() 0 10 1
A itCanResolveAnOperationIdFromAPathAndMethod() 0 15 1
A itThrowAnExceptionWhenNoRequestDefinitionIsFound() 0 10 1
A itCanBeSerialized() 0 9 1
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