embedpress
/
vendor
/
wpdevelopers
/
embera
/
tests
/
Embera
/
ProviderCollection
/
DefaultProviderCollectionTest.php
CustomProviderCollectionTest.php
5 years ago
DefaultProviderCollectionTest.php
5 years ago
SlimProviderCollectionTest.php
5 years ago
DefaultProviderCollectionTest.php
125 lines
| 1 | <?php |
| 2 | /** |
| 3 | * DefaultProviderCollectionTest.php |
| 4 | * |
| 5 | * @package Embera |
| 6 | * @author Michael Pratt <yo@michael-pratt.com> |
| 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 | namespace Embera\ProviderCollection; |
| 14 | |
| 15 | use PHPUnit\Framework\TestCase; |
| 16 | use Embera\Embera; |
| 17 | |
| 18 | final class DefaultProviderCollectionTest extends TestCase |
| 19 | { |
| 20 | protected $urls = [ |
| 21 | 'http://www.youtube.com/watch?v=WtPiGYsllos&index=1', |
| 22 | 'https://m.youtube.com/watch?v=mghhLqu31cQ', |
| 23 | 'https://host.com/invalid/url' |
| 24 | ]; |
| 25 | |
| 26 | protected $config = [ |
| 27 | 'https_only' => false, |
| 28 | 'fake_responses' => Embera::ALLOW_FAKE_RESPONSES, |
| 29 | 'maxwidth' => 430, |
| 30 | 'maxheight' => 270, |
| 31 | ]; |
| 32 | |
| 33 | public function testCanFindProviders() |
| 34 | { |
| 35 | $collection = new DefaultProviderCollection($this->config); |
| 36 | $providers = $collection->findProviders($this->urls); |
| 37 | |
| 38 | foreach ($providers as $p) { |
| 39 | $this->assertContains($p->getProviderName(), [ 'Youtube']); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function testCanFindProvidersWithHttpsSupport() |
| 44 | { |
| 45 | $collection = new DefaultProviderCollection(array_merge($this->config, ['https_only' => true])); |
| 46 | $providers = $collection->findProviders($this->urls); |
| 47 | foreach ($providers as $p) { |
| 48 | $this->assertTrue($p->hasHttpsSupport()); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public function testUnsupportedUrlsResponse() |
| 53 | { |
| 54 | $collection = new DefaultProviderCollection($this->config); |
| 55 | $providers = $collection->findProviders([ |
| 56 | 'https://host.com/unsupported/host/url', |
| 57 | 'https://oembed.com/#section1', |
| 58 | 'https://www.youtube.com/feed/subscriptions', |
| 59 | ]); |
| 60 | |
| 61 | $this->assertEmpty($providers); |
| 62 | } |
| 63 | |
| 64 | public function testCanFilterByProviderName() |
| 65 | { |
| 66 | $collection = new DefaultProviderCollection($this->config); |
| 67 | $newCollection = $collection->filter('Youtube'); |
| 68 | |
| 69 | $providers = $newCollection->findProviders($this->urls); |
| 70 | |
| 71 | foreach ($providers as $p) { |
| 72 | $this->assertEquals($p->getProviderName(), 'Youtube'); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public function testCanFilterByClosure() |
| 77 | { |
| 78 | $collection = new DefaultProviderCollection($this->config); |
| 79 | $newCollection = $collection->filter(static function ($elem) { |
| 80 | return (in_array($elem, ['Youtube', 'Vimeo'])); |
| 81 | }); |
| 82 | |
| 83 | $providers = $newCollection->findProviders($this->urls); |
| 84 | |
| 85 | foreach ($providers as $p) { |
| 86 | $this->assertContains($p->getProviderName(), ['Youtube', 'Vimeo']); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public function testCanReadInputWithoutUrls() |
| 91 | { |
| 92 | $collection = new DefaultProviderCollection($this->config); |
| 93 | $providers = $collection->findProviders('This is a text without urls.'); |
| 94 | $this->assertEmpty($providers); |
| 95 | } |
| 96 | |
| 97 | public function testCanDetectUrlsInString() |
| 98 | { |
| 99 | $collection = new DefaultProviderCollection($this->config); |
| 100 | $providers = $collection->findProviders(implode(' <br> ', $this->urls)); |
| 101 | $this->assertTrue(count($providers) > 0); |
| 102 | } |
| 103 | |
| 104 | public function testCanAddNewProviders() |
| 105 | { |
| 106 | $collection = new DefaultProviderCollection($this->config); |
| 107 | |
| 108 | // Remove all Providers |
| 109 | $collection = $collection->filter(static function ($elem) { |
| 110 | return false; |
| 111 | }); |
| 112 | |
| 113 | $providers = $collection->findProviders($this->urls); |
| 114 | $this->assertCount(0, $providers); |
| 115 | |
| 116 | // Add Only Youtube |
| 117 | $collection->addProvider('youtube.com', 'Embera\Provider\Youtube'); |
| 118 | $providers = $collection->findProviders($this->urls); |
| 119 | $this->assertTrue(count($providers) > 0); |
| 120 | foreach ($providers as $p) { |
| 121 | $this->assertEquals($p->getProviderName(), 'Youtube'); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 |