-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathImageCharts.test.php
224 lines (184 loc) · 7.51 KB
/
ImageCharts.test.php
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
// Run tests from the repository root directory:
// $ composer install && ./vendor/bin/phpunit ./ImageCharts.test.php
require './ImageCharts.php';
use PHPUnit\Framework\TestCase;
/**
* @codeCoverageIgnore
*/
class ImageChartsTest extends TestCase
{
public function test_CanInstance()
{
$this->assertInstanceOf(ImageCharts::class , new ImageCharts());
}
public function test_toURL_works(){
$this->assertSame((new ImageCharts())->cht("p")->chd("t:1,2,3")->toURL(), "https://image-charts.com:443/chart?cht=p&chd=t%3A1%2C2%2C3");
}
public function test_exposes_parameters_and_use_them(){
function startsWith($string, $startString){
return (substr($string, 0, strlen($startString)) === $startString);
}
function endsWith($string, $endString){
$len = strlen($endString);
return $len == 0 ? true : (substr($string, -$len) === $endString);
}
function is_chart_param($method){
return $method->isPublic() && startsWith($method->name, "c") || startsWith($method->name, "ic");
}
function call_method($chart, $method){
return call_user_func(array($chart, $method->name), "plop");
}
function build_query($query, $method){
$query[] = $method->name . '=plop';
return $query;
}
$class = new ReflectionClass('ImageCharts');
$chart = new ImageCharts();
$methods = array_filter($class->getMethods(), "is_chart_param");
$this->assertSame(array_reduce($methods, "call_method", $chart)->toURL(),
"https://image-charts.com:443/chart?". implode('&', array_reduce($methods, "build_query", array()))
);
}
public function test_adds_a_signature_when_icac_and_secrets_are_defined(){
$this->assertSame(
(new ImageCharts(array( "secret" => "plop")))
->cht("p")
->chd("t:1,2,3")
->chs("100x100")
->icac("test_fixture")
->toURL(),
"https://image-charts.com:443/chart?cht=p&chd=t%3A1%2C2%2C3&chs=100x100&icac=test_fixture&ichm=71bd93758b49ed28fdabd23a0ff366fe7bf877296ea888b9aaf4ede7978bdc8d"
);
}
public function test_rejects_if_a_chs_is_not_defined(){
$this->expectException(ErrorException::class);
$this->expectExceptionMessage('"chs" is required');
(new ImageCharts())->cht("p")->chd("t:1,2,3")->toBinary();
}
public function test_rejects_if_a_icac_is_defined_without_ichm(){
$this->expectException(ErrorException::class);
$this->expectExceptionMessage("The `icac` (ACCOUNT_ID) and `ichm` (HMAC-SHA256 request signature) query parameters must both be defined if specified. [Learn more](https://bit.ly/HMACENT)");
(new ImageCharts())
->cht("p")
->chd("t:1,2,3")
->chs("100x100")
->icac("test_fixture")
->toBinary();
}
public function test_rejects_if_timeout_is_reached(){
$this->expectException(ErrorException::class);
$this->expectExceptionMessage("Timeout was reached");
(new ImageCharts(array("timeout" => 2))) // 1ms
->cht("p")
->chd("t:1,2,3")
->chs("100x100")
->chan("1200")
->toBinary();
}
/*public function test_works(){
$this->assertSame(
(new ImageCharts())->cht("p")->chd("t:1,2,3").chs("2x2")->toBinary()
).resolves.toMatchSnapshot())
}*/
public function test_forwards_package_name_version_as_user_agent(){
$chart = (new ImageCharts())->cht("p")->chd("t:1,2,3")->chs("10x10");
$chart->toBinary();
$this->assertSame($chart->curl_options[CURLOPT_USERAGENT],"php-image-charts/latest");
}
public function test_forwards_package_name_version_icac_as_user_agent(){
$chart = (new ImageCharts(array("secret" => "plop")))->cht("p")->chd("t:1,2,3")->chs("10x10")->icac("MY_ACCOUNT_ID");
try{
$chart->toBinary();
}catch(Exception $ex){/* don't care */}
$this->assertSame($chart->curl_options[CURLOPT_USERAGENT],"php-image-charts/latest (MY_ACCOUNT_ID)");
}
public function test_throw_error_if_account_not_found(){
$this->expectException(ErrorException::class);
$this->expectExceptionMessage('you must be an Image-Charts subscriber');
(new ImageCharts(array("secret" => "plop")))->cht("p")->chd("t:1,2,3")->chs("10x10")->icac("MY_ACCOUNT_ID")->toBinary();
}
public function test_rejects_if_there_was_an_error(){
$this->expectException(ErrorException::class);
$this->expectExceptionMessage('"chs" is required');
(new ImageCharts())->cht("p")->chd("t:1,2,3")->toDataURI();
}
public function test_toDataURI_works(){
$this->assertSame(substr((new ImageCharts())->cht("p")->chd("t:1,2,3")->chs("2x2")->toDataURI(), 0, 30), "data:image/png;base64,iVBORw0K");
}
//
// public function test_toFile_with_bad_path_throw(){
// $this->expectException(Exception::class);
// $this->expectExceptionMessageRegExp('No such file or directory');
// (new ImageCharts())->cht("p")->chd("t:1,2,3")->chs("2x2")->toFile('/tmp_bad_path_sdijsd/plop.png');
// }
public function test_toFile_works(){
(new ImageCharts())->cht("p")->chd("t:1,2,3")->chs("2x2")->toFile('/tmp/plop.png');
$this->assertFileExists('/tmp/plop.png');
}
public function test_support_gif(){
$this->assertSame(substr((new ImageCharts())
->cht("p")
->chd("t:1,2,3")
->chan("100")
->chs("2x2")
->toDataURI(), 0, 30), "data:image/gif;base64,R0lGODlh");
}
public function test_expose_the_protocol(){
$this->assertSame((new ImageCharts())->protocol, "https");
}
public function test_let_protocol_to_be_user_defined(){
$this->assertSame((new ImageCharts([ "protocol" => "http" ]))->protocol, "http");
}
public function test_expose_the_host(){
$this->assertSame((new ImageCharts())->host, "image-charts.com");
}
public function test_let_host_to_be_user_defined(){
$this->assertSame(
(new ImageCharts([ "host" => "on-premise-image-charts.com" ]))->host, "on-premise-image-charts.com");
}
public function test_expose_the_pathname(){
$this->assertSame((new ImageCharts())->pathname, "/chart");
}
public function test_expose_the_port(){
$this->assertSame((new ImageCharts())->port, 443);
}
public function test_let_port_to_be_user_defined(){
$this->assertSame((new ImageCharts([ "port" => 8080 ]))->port, 8080);
}
public function test_expose_the_query(){
$this->assertSame((new ImageCharts())->query, []);
}
public function test_expose_the_query_user_defined(){
$this->assertTrue(arrays_are_similar((new ImageCharts())->cht("p")->chd("t:1,2,3")->icac("plop")->query, array(
"chd" => "t:1,2,3",
"cht" => "p",
"icac" => "plop",
)));
}
}
/**
* Determine if two associative arrays are similar
*
* Both arrays must have the same indexes with identical values
* without respect to key ordering
*
* @param array $a
* @param array $b
* @return bool
*/
function arrays_are_similar($a, $b) {
// if the indexes don't match, return immediately
if (count(array_diff_assoc($a, $b))) {
throw new Exception("$a and $b size mismatch");
}
// we know that the indexes, but maybe not values, match.
// compare the values between the two arrays
foreach($a as $k => $v) {
if ($v !== $b[$k]) {
throw new Exception("$k is not the same in left ($v) and right ($b[$k])");
}
}
// we have identical indexes, and no unequal values
return true;
}