-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
ProviderTester.php
executable file
·323 lines (278 loc) · 11 KB
/
ProviderTester.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* TestProviders.php
*
* @package Tests
* @author Michael Pratt <pratt@hablarmierda.net>
* @link http://www.michael-pratt.com/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Embera;
use Embera\Http\HttpClient;
use Embera\Http\OembedClient;
use Embera\ProviderCollection\DefaultProviderCollection;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
class ProviderTester extends TestCase
{
/** @var array with urls */
protected $tasks;
/**
* Returns an array with random data from an array
*
* @param array $data
* @param int $count
*
* @return array
*
*/
protected function getRandomDataFrom(array $data, $count = 1)
{
if ($count >= count($data)) {
return $data;
}
shuffle($data);
return array_slice($data, 0, $count);
}
/**
* This is where it gets nasty.
*
* This method is used to validate the behaviour of a Provider.
* All the other methods found down here, are used to test different
* parts of the provider
*
* Now, every provider has a test file which extends this class
* and uses this method to validate everything.
*
* Why? I had so much duplicated code...
* I know it looks ugly, but, its more convenient.
*
* @param string $p The provider Name
* @param array $config Additional config options
* @return void
*
* @large
*/
protected function validateProvider($p, array $config = [])
{
$rounds = 1000;
$validateRealResponse = true;
if ((bool) getenv('TRAVIS')) {
$rounds = 1;
$validateRealResponse = false;
}
if (empty($this->tasks)) {
throw new \Exception(sprintf('The Provider %s doesnt have tasks', $p));
}
$this->validateUrlDetection($p, $this->tasks, $config);
$this->validateCollectionDetection($p, $this->tasks, $config);
if (!empty($this->tasks['normalize_urls'])) {
$this->validateUrlNormalization($p, $this->tasks['normalize_urls'], $config);
}
if (!empty($this->tasks['fake_response'])) {
$this->validateFakeResponse($p, $this->tasks, $rounds, $config);
}
if ($validateRealResponse) {
$this->validateRealResponse($p, $this->tasks, $rounds, $config);
}
}
/**
* Checks if the main library detects this provider
*
* @param string $providerName Provider Name
* @param array $tasks
* @param array $config
* @return void
*
* @medium
*/
protected function validateCollectionDetection($providerName, array $tasks, array $config = [])
{
$config = array_merge([
'https_only' => false,
'fake_responses' => Embera::ALLOW_FAKE_RESPONSES,
'maxwidth' => 430,
'maxheight' => 270,
], $config);
$collection = new DefaultProviderCollection($config);
$providers = $collection->findProviders($tasks['valid_urls']);
$this->assertNotEmpty($providers, 'Could not detect urls from the main Embera library.');
foreach ($providers as $p) {
$this->assertEquals($p->getProviderName(), $providerName);
}
}
/**
* Checks if all valid urls are correctly detected
*
* @param string $providerName Provider Name
* @param array $tasks
* @param array $config
* @return void
*
* @medium
*/
protected function validateUrlDetection($providerName, array $tasks, array $config = [])
{
$validCount = 0;
$validUrlCount = count($tasks['valid_urls']);
foreach ($tasks['valid_urls'] as $valid) {
$reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
$provider = $reflection->newInstance($valid, $config);
if ($provider->validateUrl($provider->getUrl(false))) {
$validCount++;
} else {
$this->fail(sprintf('The url %s was not recognized as valid', $valid));
}
}
$this->assertEquals($validUrlCount, $validCount, sprintf('The provider %s has problem detecting valid urls.', $providerName));
$validCount = 0;
foreach (array_merge($tasks['valid_urls'], $tasks['invalid_urls']) as $mixed) {
$reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
$provider = $reflection->newInstance($mixed, $config);
if ($provider->validateUrl($provider->getUrl(false))) {
$validCount++;
}
}
$this->assertEquals($validUrlCount, $validCount, sprintf('The provider %s is detecting wrong urls as valid.', $providerName));
}
/**
* Validates that a url on this provider is correctly normalized
*
* @param string $providerName
* @param array $data
* @param array $config
* @return void
*
* @medium
*/
protected function validateUrlNormalization($providerName, array $data, array $config = [])
{
$reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
foreach ($data as $givenUrl => $expectedUrl) {
$provider = $reflection->newInstance($givenUrl, $config);
$normalizedUrl = $provider->getUrl();
$this->assertEquals($expectedUrl, $normalizedUrl, sprintf('We expected %s and were given %s', $expectedUrl, $normalizedUrl));
}
}
/**
* Validates a fake response
*
* @param string $providerName
* @param array $tasks
* @param int $rounds
* @param array $config
*
* @return void
*
* @large
*/
protected function validateFakeResponse($providerName, array $tasks, $rounds, array $config = [])
{
// Percentage accepted for fake response similarity
$similarMinimumThreshold = 80;
$urls = $this->getRandomDataFrom($tasks['valid_urls'], $rounds);
$fakeOembedClient = new OembedClient(array_merge($config, [
'fake_responses' => Embera::ONLY_FAKE_RESPONSES,
]), new HttpClient());
$realOembedClient = new OembedClient(array_merge($config, [
'fake_responses' => Embera::DISABLE_FAKE_RESPONSES,
]), new HttpClient());
foreach ($urls as $url) {
$reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
$provider = $reflection->newInstance($url, $config);
$fakeResponse = $fakeOembedClient->getResponseFrom($provider);
$this->assertNotEmpty($fakeResponse['html'], sprintf(
'Fake Response doesnt have html data on url %s', $url
));
$this->assertStringContainsStringIgnoringCase($tasks['fake_response']['html'], $fakeResponse['html'], sprintf(
'Could not determine the existance of %s in the url %s. The response was %s',
$tasks['fake_response']['html'],
$url,
$fakeResponse['html']
));
$types = $tasks['fake_response']['type'];
if (strpos($types, '|') !== false) {
$typesArray = explode('|', $types);
$this->assertContains($fakeResponse['type'], $typesArray, sprintf(
'Fake Response is type %s but we only are allowing %s',
$fakeResponse['type'], $types
));
} else {
$this->assertEquals($types, $fakeResponse['type'], sprintf(
'Fake Response type is not type %s on %s, %s given',
$types,
$url,
$fakeResponse['type']
));
}
$this->assertEquals(1, $fakeResponse['embera_using_fake_response'], sprintf(
'Fake response flag is not correct. Expecting "1", recieved "%s" on url %s',
$fakeResponse['embera_using_fake_response'], $url
));
$realResponse = $realOembedClient->getResponseFrom($provider);
if (!isset($realResponse['embera_using_fake_response'])) {
$this->markTestIncomplete(sprintf(
'The Provider %s doesnt define the embera_using_fake_response on url %s. Probably the response took to long',
$providerName, $url
));
return ;
}
$this->assertEquals(0, $realResponse['embera_using_fake_response'], sprintf(
'Fake response flag is not correct. Expecting "0", recieved "%s" on url %s',
$realResponse['embera_using_fake_response'], $url
));
$this->assertNotEmpty($realResponse['html'], sprintf(
'Real Response doesnt have html data on url %s', $url
));
if (!empty($realResponse['type']) && strpos($types, '|') === false) {
$this->assertEquals(strtolower($realResponse['type']), strtolower($fakeResponse['type']), sprintf(
'The real response is type %s and not %s',
$realResponse['type'],
$fakeResponse['type']
));
}
similar_text($fakeResponse['html'], $realResponse['html'], $percent);
$this->assertTrue($percent >= $similarMinimumThreshold, sprintf(
'The Fake/Real response for url %s is off. The current threshold is %s and we got %s. Real response: "%s" | Fake response: "%s"',
$url, $similarMinimumThreshold, $percent, $realResponse['html'], $fakeResponse['html']
));
}
}
/**
* Validates the real response from a given provider
*
* @param string $providerName
* @param array $tasks
* @param int $rounds
* @param array $config
*
* @return void
*
* @large
*/
protected function validateRealResponse($providerName, array $tasks, $rounds, array $config = [])
{
$urls = $this->getRandomDataFrom($tasks['valid_urls'], $rounds);
$oembedClient = new OembedClient(array_merge($config, [
'fake_responses' => Embera::DISABLE_FAKE_RESPONSES,
]), new HttpClient());
foreach ($urls as $url) {
$reflection = new ReflectionClass('Embera\Provider\\' . $providerName);
$provider = $reflection->newInstance($url, $config);
$response = $oembedClient->getResponseFrom($provider);
if (!isset($response['embera_using_fake_response'])) {
$this->markTestIncomplete(sprintf(
'The Provider %s doesnt define the embera_using_fake_response on url %s. Probably the response took to long',
$providerName, $url
));
return ;
}
$this->assertEquals(0, $response['embera_using_fake_response'], sprintf(
'Fake response flag is not correct. Expecting "0", recieved "%s" on url %s',
$response['embera_using_fake_response'], $url
));
}
}
}