1
|
|
|
<?php |
2
|
|
|
namespace ElevenLabs\Api\Service; |
3
|
|
|
|
4
|
|
|
use ElevenLabs\Api\Schema; |
5
|
|
|
use ElevenLabs\Api\Validator\MessageValidator; |
6
|
|
|
use Http\Client\HttpClient; |
7
|
|
|
use Http\Message\MessageFactory; |
8
|
|
|
use Http\Message\UriFactory; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Rize\UriTemplate; |
11
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
12
|
|
|
|
13
|
|
|
class ApiServiceTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var Schema */ |
16
|
|
|
private $schema; |
17
|
|
|
/** @var UriFactory */ |
18
|
|
|
private $uriFactory; |
19
|
|
|
/** @var UriTemplate */ |
20
|
|
|
private $uriTemplate; |
21
|
|
|
/** @var HttpClient */ |
22
|
|
|
private $httpClient; |
23
|
|
|
/** @var MessageFactory */ |
24
|
|
|
private $messageFactory; |
25
|
|
|
/** @var MessageValidator */ |
26
|
|
|
private $messageValidator; |
27
|
|
|
/** @var SerializerInterface */ |
28
|
|
|
private $serializer; |
29
|
|
|
/** @var array */ |
30
|
|
|
private $config; |
31
|
|
|
|
32
|
|
|
public function setUp() |
33
|
|
|
{ |
34
|
|
|
$this->schema = $this->prophesize(Schema::class); |
|
|
|
|
35
|
|
|
$this->uriFactory = $this->prophesize(UriFactory::class); |
|
|
|
|
36
|
|
|
$this->uriTemplate = $this->prophesize(UriTemplate::class); |
|
|
|
|
37
|
|
|
$this->httpClient = $this->prophesize(HttpClient::class); |
|
|
|
|
38
|
|
|
$this->messageFactory = $this->prophesize(MessageFactory::class); |
|
|
|
|
39
|
|
|
$this->messageValidator = $this->prophesize(MessageValidator::class); |
|
|
|
|
40
|
|
|
$this->serializer = $this->prophesize(SerializerInterface::class); |
|
|
|
|
41
|
|
|
$this->config = []; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @test */ |
45
|
|
|
public function itShouldCheckApiSchemaSchemes() |
46
|
|
|
{ |
47
|
|
|
$this->expectException(\LogicException::class); |
48
|
|
|
$this->expectExceptionMessage('You need to provide at least on scheme in your API Schema'); |
49
|
|
|
|
50
|
|
|
$this->getApiService(); |
51
|
|
|
} |
52
|
|
|
/** @test */ |
53
|
|
|
public function itCheckTheHostInApiSchema() |
54
|
|
|
{ |
55
|
|
|
$this->expectException(\LogicException::class); |
56
|
|
|
$this->expectExceptionMessage('The host in the API Schema should not be null'); |
57
|
|
|
|
58
|
|
|
$this->schema->getSchemes()->willReturn(['https']); |
59
|
|
|
$this->schema->getHost()->willReturn(null); |
60
|
|
|
|
61
|
|
|
$this->getApiService(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @test */ |
65
|
|
|
public function itOnlySupportHttpAndHttps() |
66
|
|
|
{ |
67
|
|
|
$this->expectException(\RuntimeException::class); |
68
|
|
|
$this->expectExceptionMessage('Cannot choose a proper scheme from the API Schema. Supported: https, http'); |
69
|
|
|
|
70
|
|
|
$this->schema->getSchemes()->willReturn(['ftp']); |
71
|
|
|
$this->schema->getHost()->willReturn('domain.tld'); |
72
|
|
|
|
73
|
|
|
$this->getApiService(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** @test */ |
77
|
|
|
public function itShouldPreferHttps() |
78
|
|
|
{ |
79
|
|
|
$this->schema->getSchemes()->willReturn(['http', 'https']); |
80
|
|
|
$this->schema->getHost()->willReturn('domain.tld'); |
81
|
|
|
|
82
|
|
|
$this->uriFactory->createUri('https://domain.tld')->shouldBeCalled(); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$this->getApiService(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** @test */ |
88
|
|
|
public function itCreateABaseUriUsingTheOneProvidedInTheConfigArray() |
89
|
|
|
{ |
90
|
|
|
$this->config['baseUri'] = 'https://somewhere.tld'; |
91
|
|
|
$this->uriFactory->createUri('https://somewhere.tld')->shouldBeCalled(); |
92
|
|
|
|
93
|
|
|
$this->getApiService(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function getApiService() |
97
|
|
|
{ |
98
|
|
|
return new ApiService( |
99
|
|
|
$this->uriFactory->reveal(), |
|
|
|
|
100
|
|
|
$this->uriTemplate->reveal(), |
|
|
|
|
101
|
|
|
$this->httpClient->reveal(), |
|
|
|
|
102
|
|
|
$this->messageFactory->reveal(), |
|
|
|
|
103
|
|
|
$this->schema->reveal(), |
|
|
|
|
104
|
|
|
$this->messageValidator->reveal(), |
|
|
|
|
105
|
|
|
$this->serializer->reveal(), |
|
|
|
|
106
|
|
|
$this->config |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..