Completed
Push — fix/scrutinizer-config ( 160cd0 )
by Guillem
02:50
created

itAllowTheResponseValidationToBeEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
1
<?php
2
namespace ElevenLabs\Api\Service\Functional;
3
4
use ElevenLabs\Api\Service\ApiServiceBuilder;
5
use ElevenLabs\Api\Service\Exception\RequestViolations;
6
use ElevenLabs\Api\Service\Exception\ResponseViolations;
7
use ElevenLabs\Api\Service\Pagination\Pagination;
8
use ElevenLabs\Api\Service\Pagination\Provider\PaginationHeader;
9
use ElevenLabs\Api\Service\Resource\Collection;
10
use ElevenLabs\Api\Service\Resource\Resource;
11
use GuzzleHttp\Psr7\Response;
12
use Http\Mock\Client;
13
use Http\Promise\Promise;
14
use PHPUnit\Framework\TestCase;
15
16
class ApiServiceTest extends TestCase
17
{
18
    /** @var string */
19
    private $schemaFile;
20
    /** @var Client */
21
    private $httpMockClient;
22
23
    public function setUp()
24
    {
25
        $this->schemaFile = 'file://'.__DIR__.'/../fixtures/httpbin.yml';
26
        $this->httpMockClient = new Client();
27
    }
28
29
    /** @test */
30
    public function itCanMakeASynchronousCall()
31
    {
32
        $apiService = ApiServiceBuilder::create()
33
            ->withHttpClient($this->httpMockClient)
34
            ->build($this->schemaFile);
35
36
        $this->httpMockClient->addResponse(
37
            new Response(200, ['Content-Type' => 'application/json'], '{}')
38
        );
39
40
        $apiService->call('dumpGetRequest');
41
    }
42
43
    /** @test */
44
    /*public function itCanMakeAnAsynchronousCall()
45
    {
46
        $apiService = ApiServiceBuilder::create()
47
            ->withHttpClient($this->httpMockClient)
48
            ->build($this->schemaFile);
49
50
        $this->httpMockClient->addResponse(
51
            new Response(200, ['Content-Type' => 'application/json'], '{}')
52
        );
53
54
        $promise = $apiService->callAsync('dumpGetRequest');
55
        assertThat($promise, isInstanceOf(Promise::class));
56
57
        $resource = $promise->wait();
58
        assertThat($resource, isInstanceOf(Resource::class));
59
    }*/
60
61
    /** @test */
62
    public function itValidateTheRequestByDefault()
63
    {
64
        $this->expectException(RequestViolations::class);
65
66
        $apiService = ApiServiceBuilder::create()
67
            ->build($this->schemaFile);
68
69
        $apiService->call('dumpGetRequest', ['aDate' => 'notADateString']);
70
    }
71
72
    /** @test */
73
    public function itAllowTheRequestValidationToBeDisable()
74
    {
75
        $apiService = ApiServiceBuilder::create()
76
            ->disableRequestValidation()
77
            ->withHttpClient($this->httpMockClient)
78
            ->withBaseUri('https://domain.tld')
79
            ->build($this->schemaFile);
80
81
        $this->httpMockClient->addResponse(new Response(200, ['Content-Type' => 'application/json'], '{}'));
82
83
        $apiService->call('dumpGetRequest', [
84
            'aPath' => 1,
85
            'aDate' => 'notADateString',
86
            'aBody' => ['foo' => 'bar']
87
        ]);
88
89
        $request = current($this->httpMockClient->getRequests());
90
91
        assertThat($request->getUri()->__toString(), equalTo('https://domain.tld/get/1?aDate=notADateString'));
92
    }
93
94
    /** @test */
95
    public function itAllowTheResponseValidationToBeEnabled()
96
    {
97
        $this->expectException(ResponseViolations::class);
98
99
        $apiService = ApiServiceBuilder::create()
100
            ->enableResponseValidation()
101
            ->withHttpClient($this->httpMockClient)
102
            ->withBaseUri('https://domain.tld')
103
            ->build($this->schemaFile);
104
105
        $this->httpMockClient->addResponse(
106
            new Response(
107
                200,
108
                ['Content-Type' => 'application/json'],
109
                '{"notAValidProperty": "oups"}'
110
            )
111
        );
112
113
        $apiService->call('dumpGetRequest');
114
    }
115
116
    public function itCanPaginate()
117
    {
118
        $apiService = ApiServiceBuilder::create()
0 ignored issues
show
Bug introduced by
Are you sure the usage of ElevenLabs\Api\Service\A...der\PaginationHeader()) targeting ElevenLabs\Api\Service\A...ithPaginationProvider() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
119
            ->withHttpClient($this->httpMockClient)
120
            ->withBaseUri('https://domain.tld')
121
            ->withPaginationProvider(new PaginationHeader())
122
            ->build($this->schemaFile);
123
124
        $this->httpMockClient->addResponse(
125
            new Response(
126
                200,
127
                [
128
                    'Content-Type' => 'application/json',
129
                    'X-Page' => '1',
130
                    'X-Per-Page' => '10',
131
                    'X-Total-Pages' => '10',
132
                    'X-Total-Items' => '100',
133
                    'Link' => [
134
                        '<http://domain.tld?page=1>; rel="first"',
135
                        '<http://domain.tld?page=10>; rel="last"',
136
                        '<http://domain.tld?page=4>; rel="next"',
137
                        '<http://domain.tld?page=2>; rel="prev"',
138
                    ]
139
                ],
140
                '[{"foo": "value 1"}, {"foo": "value 2"}]'
141
            )
142
        );
143
144
        $resource = $apiService->call('getFakeCollection');
145
146
        assertThat($resource, isInstanceOf(Collection::class));
147
        assertThat($resource->hasPagination(), isTrue());
148
        assertThat($resource->getPagination(), isInstanceOf(Pagination::class));
149
    }
150
}
151