Autoload.php
9 years ago
TestEmbera.php
9 years ago
TestFakeResponse.php
9 years ago
TestFormatter.php
9 years ago
TestHttpRequest.php
9 years ago
TestOembed.php
9 years ago
TestProviders.php
9 years ago
TestServiceAmCharts.php
9 years ago
TestServiceAnimatron.php
9 years ago
TestServiceAnimoto.php
9 years ago
TestServiceAolOn.php
9 years ago
TestServiceAppNet.php
9 years ago
TestServiceAudioSnaps.php
9 years ago
TestServiceBambuser.php
9 years ago
TestServiceBlipTv.php
9 years ago
TestServiceCacoo.php
9 years ago
TestServiceChartblocks.php
9 years ago
TestServiceChirbit.php
9 years ago
TestServiceCircuitLab.php
9 years ago
TestServiceClyp.php
9 years ago
TestServiceCodepoints.php
9 years ago
TestServiceCollegeHumor.php
9 years ago
TestServiceCoub.php
9 years ago
TestServiceCrowdRanking.php
9 years ago
TestServiceDailyMile.php
9 years ago
TestServiceDailyMotion.php
9 years ago
TestServiceDeviantart.php
9 years ago
TestServiceDipity.php
9 years ago
TestServiceDotSub.php
9 years ago
TestServiceEdocr.php
9 years ago
TestServiceEgliseInfo.php
9 years ago
TestServiceFacebook.php
9 years ago
TestServiceFlickr.php
9 years ago
TestServiceFunnyOrDie.php
9 years ago
TestServiceGeographCI.php
9 years ago
TestServiceGeographDe.php
9 years ago
TestServiceGeographUk.php
9 years ago
TestServiceGettyImages.php
9 years ago
TestServiceGfycat.php
9 years ago
TestServiceGithubGist.php
9 years ago
TestServiceGmep.php
9 years ago
TestServiceHq23.php
9 years ago
TestServiceHuffduffer.php
9 years ago
TestServiceHulu.php
9 years ago
TestServiceIFTTT.php
9 years ago
TestServiceIFixIt.php
9 years ago
TestServiceInfogram.php
9 years ago
TestServiceInstagram.php
9 years ago
TestServiceIssuu.php
9 years ago
TestServiceIssuuFakeResponse.php
9 years ago
TestServiceKickstarter.php
9 years ago
TestServiceLearningApps.php
9 years ago
TestServiceMeetup.php
9 years ago
TestServiceMixCloud.php
9 years ago
TestServiceMobyPicture.php
9 years ago
TestServiceNFB.php
9 years ago
TestServiceOfficeMix.php
9 years ago
TestServiceOfficialFM.php
9 years ago
TestServiceOumy.php
9 years ago
TestServicePastery.php
9 years ago
TestServicePollDaddy.php
9 years ago
TestServicePollEverywhere.php
9 years ago
TestServicePortfolium.php
9 years ago
TestServiceRapidengage.php
9 years ago
TestServiceRdio.php
9 years ago
TestServiceReleaseWire.php
9 years ago
TestServiceRepubHub.php
9 years ago
TestServiceReverbnation.php
9 years ago
TestServiceRevision3.php
9 years ago
TestServiceRoomshare.php
9 years ago
TestServiceRutube.php
9 years ago
TestServiceSapo.php
9 years ago
TestServiceScreenr.php
9 years ago
TestServiceScribd.php
9 years ago
TestServiceShortNote.php
9 years ago
TestServiceShoudio.php
9 years ago
TestServiceShowTheWay.php
9 years ago
TestServiceSilk.php
9 years ago
TestServiceSketchfab.php
9 years ago
TestServiceSlideshare.php
9 years ago
TestServiceSoundCloud.php
9 years ago
TestServiceSpeakerDeck.php
9 years ago
TestServiceSpotify.php
9 years ago
TestServiceSway.php
9 years ago
TestServiceTed.php
9 years ago
TestServiceTheySaidSo.php
9 years ago
TestServiceTwitter.php
9 years ago
TestServiceUstream.php
9 years ago
TestServiceVerse.php
9 years ago
TestServiceViddler.php
9 years ago
TestServiceVideoFork.php
9 years ago
TestServiceVideoJug.php
9 years ago
TestServiceVimeo.php
9 years ago
TestServiceVine.php
9 years ago
TestServiceWordpressTV.php
9 years ago
TestServiceYFrog.php
9 years ago
TestServiceYandex.php
9 years ago
TestServiceYoutube.php
9 years ago
TestUrl.php
9 years ago
TestProviders.php
238 lines
| 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 |