OEmbedResourceUrlAlterEventTest.php in Hook Event Dispatcher 3.x
File
modules/media_event_dispatcher/tests/src/Unit/Media/OEmbedResourceUrlAlterEventTest.php
View source
<?php
namespace Drupal\Tests\media_event_dispatcher\Unit\Media;
use Drupal;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\media\OEmbed\Provider;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Drupal\media_event_dispatcher\Event\Media\OEmbedResourceUrlAlterEvent;
use Drupal\Tests\hook_event_dispatcher\Unit\HookEventDispatcherManagerSpy;
use PHPUnit\Framework\TestCase;
use function media_event_dispatcher_oembed_resource_url_alter;
class OEmbedResourceUrlAlterEventTest extends TestCase {
private $manager;
public function setUp() : void {
$builder = new ContainerBuilder();
$this->manager = new HookEventDispatcherManagerSpy();
$builder
->set('hook_event_dispatcher.manager', $this->manager);
$builder
->compile();
Drupal::setContainer($builder);
}
private function getMockProvider() : Provider {
return new Provider('Vimeo', 'https://vimeo.com/', [
[
'url' => 'https://vimeo.com/api/oembed.json',
'schemes' => [
'https://vimeo.com/*',
],
],
]);
}
public function testOembedResourceParsedUrlAlter() {
$url = $expected = [
'path' => 'https://vimeo.com/api/oembed.json',
'query' => [
'url' => 'https://vimeo.com/118146193',
],
'fragment' => '',
];
$provider = $this
->getMockProvider();
$this->manager
->setEventCallbacks([
HookEventDispatcherInterface::MEDIA_OEMBED_RESOURCE_DATA_ALTER => function (OEmbedResourceUrlAlterEvent $event) {
$url =& $event
->getParsedURL();
$url['query']['width'] = '1280';
},
]);
media_event_dispatcher_oembed_resource_url_alter($url, $provider);
$expected['query']['width'] = '1280';
self::assertSame($expected, $url);
$event = $this->manager
->getRegisteredEvent(HookEventDispatcherInterface::MEDIA_OEMBED_RESOURCE_DATA_ALTER);
self::assertSame($url, $event
->getParsedURL());
}
}