forked from cloudconvert/cloudconvert-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiTest.php
More file actions
202 lines (157 loc) · 5.72 KB
/
ApiTest.php
File metadata and controls
202 lines (157 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
namespace CloudConvert\Tests;
use CloudConvert\Api;
use CloudConvert\Exceptions\ApiTemporaryUnavailableException;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
/**
* Tests of Api class
*
* @package CloudConvert
* @category CloudConvert
* @author Josias Montag <josias@montag.info>
*/
class ApiTest extends \PHPUnit_Framework_TestCase
{
/**
* Define id to create object
*/
protected function setUp()
{
$this->api_key = "tests";
}
/**
* Get private and protected method to unit test it
*/
protected static function getPrivateMethod($name)
{
$class = new \ReflectionClass('CloudConvert\Api');
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method;
}
protected static function getPrivateProperty($name)
{
$class = new \ReflectionClass('CloudConvert\Api');
$property = $class->getProperty($name);
$property->setAccessible(true);
return $property;
}
/**
* Test if request without authentication works
*/
public function testIfRequestWithoutAuthenticationWorks()
{
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json; charset=utf-8'], "[{\"inputformat\":\"pdf\",\"outputformat\":\"pdf\",\"converter\":\"test\",\"converteroptions\":{\"test_option\":true}}]"),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$api = new Api($this->api_key, $client);
$invoker = self::getPrivateMethod('rawCall');
$result = $invoker->invokeArgs($api, array(
'GET',
'/conversiontypes',
array(
'inputformat' => 'pdf',
'outputformat' => 'pdf',
),
false,
));
$this->assertNotEmpty($result);
$this->assertCount(1, $result);
$this->assertEquals("test",$result[0]['converter']);
}
/**
* Test if request with authentication works
*/
public function testIfRequestWithAuthenticationWorks()
{
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json; charset=utf-8'], "{\"url\":\"//processurl\"}"),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$api = new Api($this->api_key, $client);
$invoker = self::getPrivateMethod('rawCall');
$result = $invoker->invokeArgs($api, array(
'POST',
'/process',
array(
'inputformat' => 'pdf',
'outputformat' => 'pdf',
),
true,
));
$this->assertEquals("//processurl",$result['url']);
$last = $mock->getLastRequest();
$this->assertEquals('Bearer ' . $this->api_key, $last->getHeaderLine('Authorization'));
}
/**
* Test if Process creation works
*/
public function testIfProcessCreationWorks()
{
$mock = new MockHandler([
new Response(200, ['Content-Type' => 'application/json; charset=utf-8'], "{\"url\":\"//processurl\"}"),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$api = new Api($this->api_key, $client);
$process = $api->createProcess(array(
'inputformat' => 'pdf',
'outputformat' => 'pdf',
));
$this->assertInstanceOf('CloudConvert\Process', $process);
$this->assertEquals("//processurl", $process->url);
}
/**
* Test if Process creation with invalid format throws a CloudConvert\Exceptions\ApiException
*/
public function testIfProcessCreationWithInvalidFormatThrowsTheRightException()
{
$mock = new MockHandler([
new Response(400, ['Content-Type' => 'application/json; charset=utf-8'], "{\"message\":\"This conversiontype is not supported!\"}"),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$api = new Api($this->api_key, $client);
$this->setExpectedException('CloudConvert\Exceptions\ApiBadRequestException', 'This conversiontype is not supported!', 400);
$api->createProcess(array(
'inputformat' => 'invalid',
'outputformat' => 'pdf',
));
}
/**
* Test if API error 503 throws a CloudConvert\Exceptions\ApiTemporaryUnavailableException with correct retryAfter value
*/
public function testIfApiTemporaryUnavailableExceptionIsThrown()
{
$mock = new MockHandler([
new Response(503, ['Retry-After' => 30, 'Content-Type' => 'application/json; charset=utf-8'], "{\"message\":\"API unavailable. Please try later.\"}"),
]);
$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);
$api = new Api($this->api_key, $client);
$invoker = self::getPrivateMethod('rawCall');
try {
$invoker->invokeArgs($api, array(
'GET',
'/conversiontypes',
array(
'inputformat' => 'pdf',
'outputformat' => 'pdf',
),
false,
));
}
catch (ApiTemporaryUnavailableException $expected) {
$this->assertEquals(30, $expected->retryAfter);
$this->assertEquals("API unavailable. Please try later.", $expected->getMessage());
return;
}
$this->fail('CloudConvert\Exceptions\ApiTemporaryUnavailableException has not been raised.');
}
}