PluginProbe
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents / 1.0.0
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents v1.0.0
4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 All 189 releases
embedpress / library / ostraining / embera / Tests / TestProviders.php
TestProviders.php
238 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TestProviders.php
4 *
5 * @package Tests
6 * @author Michael Pratt <pratt@hablarmierda.net>
7 * @link http://www.michael-pratt.com/
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13 class TestProviders extends PHPUnit_Framework_TestCase
14 {
15 /** @var array with urls */
16 protected $urls;
17
18 public function testEmptyOrInvalidProviders()
19 {
20 $oembed = new MockOembed(new MockHttpRequest());
21
22 $p = new \Embera\Providers(array('oembed' => true), $oembed);
23 $this->assertEmpty($p->getAll(array()));
24
25 $p = new \Embera\Providers(array('oembed' => true), $oembed);
26 $this->assertEmpty($p->getAll(null));
27
28 $p = new \Embera\Providers(array('oembed' => true), $oembed);
29 $this->assertEmpty($p->getAll('http://www.unknown.com'));
30
31 $urls = array('http://www.unknown.com/path/stuf/?hi=1', 'http://www.thewalkingdead.com/stuff/');
32 $p = new \Embera\Providers(array('oembed' => true), $oembed);
33 $this->assertEmpty($p->getAll($urls));
34 }
35
36 protected function getRandomUrls(array $urls = array(), $count = 1)
37 {
38 if ($count >= count($urls)) {
39 return $urls;
40 }
41
42 shuffle($urls);
43 return array_slice($urls, 0, $count);
44 }
45
46 /**
47 * This is where it gets nasty.
48 *
49 * This method is used to validate the behaviour of a Service Provider.
50 * All the other methods found down here, are used to test different
51 * parts of the service.
52 *
53 * Now, every service has a test file which extends this class
54 * and uses this method to validate everything.
55 *
56 * Why? I had so much duplicated code...
57 * I know it looks ugly, but, its more convenient.
58 *
59 * @large
60 */
61 protected function validateProvider($s)
62 {
63 if (empty($this->urls)) {
64 throw new Exception('No urls specified for the service ' . $s);
65 }
66
67 // Use all the available urls
68 $rounds = (defined('FULL_TEST') && FULL_TEST ? 1000 : 1);
69
70 $this->validateDetection($s, $this->urls['valid'], $this->urls['invalid']);
71 $this->validateRealResponse($s, $this->getRandomUrls($this->urls['valid'], $rounds));
72
73 if (!empty($this->urls['normalize'])) {
74 $this->validateUrlNormalization($s, $this->urls['normalize']);
75 }
76
77 if (!empty($this->urls['fake'])) {
78 $this->validateFakeResponse($s, $this->getRandomUrls($this->urls['valid'], $rounds), $this->urls['fake']);
79 }
80
81 if (!empty($this->urls['private'])) {
82 $this->validatePrivateUrlResponse($s, $this->urls['private']);
83 }
84
85 $this->validateWrongUrlResponse($s, $this->getRandomUrls($this->urls['invalid'], 3));
86 }
87
88 /**
89 * Checks if all valid urls are correctly detected
90 * @medium
91 */
92 protected function validateDetection($s, array $validUrls, array $invalidUrls)
93 {
94 $oembed = new MockOembed(new MockHttpRequest());
95
96 $p = new \Embera\Providers(array('oembed' => true), $oembed);
97 $this->assertCount(count($validUrls), $p->getAll($validUrls), $s . ' The valid Urls dont seem to be detected correctly');
98
99 $p = new \Embera\Providers(array('oembed' => true), $oembed);
100 $this->assertCount(count($validUrls), $p->getAll(array_merge($validUrls, $invalidUrls)), $s . ' There is at least one invalid url recognized as valid');
101
102 $p = new \Embera\Providers(array('oembed' => true), $oembed);
103 $this->assertCount(1, $p->getAll($validUrls[mt_rand(0, (count($validUrls) - 1))]), $s . ' One Correct url seems to be invalid');
104 }
105
106 /**
107 * Validates the response for an array of urls
108 * @large
109 */
110 protected function validateRealResponse($service, array $validUrls)
111 {
112 $oembed = new \Embera\Oembed(new \Embera\HttpRequest());
113 $service = '\Embera\Providers\\' . $service;
114
115 foreach ($validUrls as $url) {
116 $test = new $service($url, array(
117 'oembed' => true,
118 'fake' => array(),
119 'params' => array()
120 ), $oembed);
121
122 $result = $test->getInfo();
123
124 if (!isset($result['embera_using_fake'])) {
125 $this->markTestIncomplete($service . ': Embera_using_fake index not defined on ' . $url . ' - Probably the response took too long - Using url ' . $test->getUrl());
126 return ;
127 }
128
129 $this->assertTrue($result['embera_using_fake'] == 0, $service . ': Using Fake on ' . $url);
130 }
131 }
132
133 /**
134 * Validates that a url on this service is correctly normalized
135 * @medium
136 */
137 protected function validateUrlNormalization($service, array $normalizeUrls)
138 {
139 $oembed = new MockOembed(new MockHttpRequest());
140 $service = '\Embera\Providers\\' . $service;
141
142 foreach ($normalizeUrls as $given => $expected) {
143 $test = new $service($given, array('oembed' => false, 'fake' => array(), 'params' => array()), $oembed);
144 $this->assertEquals($test->getUrl(), $expected);
145 }
146 }
147
148 /**
149 * Validates a fake response
150 * @large
151 */
152 protected function validateFakeResponse($service, array $validUrls, array $fakeResponseData)
153 {
154 $fakeOembed = new MockOembed(new MockHttpRequest());
155 $service = '\Embera\Providers\\' . $service;
156
157 foreach ($validUrls as $url) {
158 $test = new $service($url, array('fake' => array(), 'params' => array()), $fakeOembed);
159 $response = $test->fakeResponse();
160
161 $this->assertTrue((count($response) > 2), 'Invalid Fake Response for ' . $url . ' - Debug: ' . print_r($response, true));
162 $this->assertContains($fakeResponseData['html'], $response['html'], 'Response is not ' . $fakeResponseData['html']. ' in ' . $url);
163 $this->assertEquals($fakeResponseData['type'], $response['type'], 'Response type is not ' . $fakeResponseData['type'] . ' on ' . $url);
164
165 // Do a real test and comparition
166 $oembed = new \Embera\Oembed(new \Embera\HttpRequest());
167 $test = new $service($url, array('oembed' => true, 'fake' => array(), 'params' => array()), $oembed);
168 $result1 = $test->getInfo();
169
170 if (!isset($result1['embera_using_fake'])) {
171 $this->markTestIncomplete($service . ': Embera_using_fake index not defined on ' . $url . ' - Probably the response took too long');
172 return ;
173 }
174
175 $this->assertTrue(isset($result1['html']), 'Funky response (no html) from ' . $url);
176 $this->assertTrue($result1['embera_using_fake'] == 0, 'Using fake on ' . $url);
177 $this->assertTrue(!empty($result1['html']), 'Empty Html on ' . $url);
178
179 $this->assertContains($fakeResponseData['html'], $result1['html'], 'Response is not ' . $fakeResponseData['html']. ' in ' . $url);
180 $this->assertEquals($fakeResponseData['type'], $result1['type'], 'Response type is not ' . $fakeResponseData['type'] . ' on ' . $url);
181
182 $oembed = new \Embera\Oembed(new \Embera\HttpRequest());
183 $test = new $service($url, array('oembed' => false, 'fake' => array(), 'params' => array()), $oembed);
184 $result2 = $test->getInfo();
185
186 $this->assertTrue($result2['embera_using_fake'] == 1, 'Not Using fake on ' . $url);
187 $this->assertTrue(!empty($result2['html']), 'Empty Html on ' . $url);
188
189 similar_text($result1['html'], $result2['html'], $percent);
190 $this->assertTrue($percent >= 70, 'The Fake/Real response for ' . $url . ' seem a little off | %' . $percent);
191
192 $this->assertTrue(isset($result1['type']), 'No type response on ' . $url);
193 $this->assertEquals(strtolower($result1['type']), strtolower($fakeResponseData['type']), 'Funky type response from ' . $url);
194 }
195 }
196
197 /**
198 * Validate urls that have a private response
199 * @large
200 */
201 protected function validatePrivateUrlResponse($service, array $privateUrls)
202 {
203 $oembed = new \Embera\Oembed(new \Embera\HttpRequest());
204 $service = '\Embera\Providers\\' . $service;
205
206 foreach ($privateUrls as $url) {
207 $test = new $service($url, array('oembed' => true, 'fake' => array(), 'params' => array()), $oembed);
208 $this->assertEmpty($test->getInfo(), $service . ': Invalid response from a private url ' . print_r($test->getInfo(), true));
209 }
210 }
211
212 /**
213 * Validates a response from an invalid url for the current service
214 * @large
215 */
216 protected function validateWrongUrlResponse($service, array $urls)
217 {
218 $service = '\Embera\Providers\\' . $service;
219 $oembed = new MockOembed(new MockHttpRequest());
220
221 foreach ($urls as $url) {
222 try {
223 new $service($url, array(
224 'oembed' => true,
225 'fake' => array(),
226 'params' => array()
227 ), $oembed);
228
229 $this->assertTrue(false, $service . ': The url ' . $url . ' doesnt seem to be invalid');
230 } catch (InvalidArgumentException $e) {
231 // just keep the count
232 $this->assertTrue(true);
233 }
234 }
235 }
236 }
237 ?>
238