Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Spec for YoutubeConverter Extension #6

Merged
merged 2 commits into from
May 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions spec/Converter/Extensions/YoutubeConverterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace spec\Predmond\HtmlToAmp\Converter\Extensions;

use Prophecy\Argument;
use PhpSpec\ObjectBehavior;
use Predmond\HtmlToAmp\Element;
use League\Event\EventInterface;
use Predmond\HtmlToAmp\ElementInterface;
use Predmond\HtmlToAmp\Converter\Extensions\YoutubeConverter;

class YoutubeConverterSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(YoutubeConverter::class);
}

function it_can_convert_a_youtube_iframe_tag(
EventInterface $event,
ElementInterface $element,
Element $writeableElement,
Element $ampYoutubeElement
) {
$event
->stopPropagation()
->shouldBeCalled()
;

$writeableElement
->appendChild($ampYoutubeElement)
->shouldBeCalled()
;

$element
->getAttribute('src')
->shouldBeCalled()
->willReturn('http://www.youtube.com/embed/XGSy3_Czz8k?autoplay=1')
;

$element
->createWritableElement('div', [
'class' => 'youtube-container'
])
->shouldBeCalled()
->willReturn($writeableElement)
;

$element->replaceWith($writeableElement)->shouldBeCalled();

$element
->createWritableElement(
'amp-youtube',
[
'data-videoid' => 'XGSy3_Czz8k',
"layout" => "responsive",
"width" => "560",
"height" => "315"
]
)
->shouldBeCalled()
->willReturn($ampYoutubeElement)
;

$this->handleIframe($event, $element);
}

function it_skips_converting_non_youtube_iframes(
EventInterface $event,
ElementInterface $element
) {
$element
->getAttribute('src')
->shouldBeCalled()
->willReturn('http://metube.com/embed/XGSy3_Czz8k')
;

$event->stopPropagation()->shouldNotBeCalled();
$element->createWritableElement('div', [
'class' => 'youtube-container'
])->shouldNotBeCalled();

$this->handleIframe($event, $element);
}

function it_converts_a_youtube_object_tag(
EventInterface $event,
ElementInterface $element,
Element $child,
Element $containerElement,
Element $ampYoutubeElement
) {
$event
->stopPropagation()
->shouldBeCalled()
;

$element
->getChildren()
->shouldBeCalled()
->willReturn([$child])
;

$child
->getAttribute('value')
->shouldBeCalled()
->willReturn('http://www.youtube.com/embed/XGSy3_Czz8k?autoplay=1')
;

$element
->createWritableElement('div', [
'class' => 'youtube-container'
])
->shouldBeCalled()
->willReturn($containerElement)
;

$containerElement
->appendChild($ampYoutubeElement)
->shouldBeCalled()
;

$element
->replaceWith($containerElement)
->shouldBeCalled()
;

$element
->createWritableElement(
'amp-youtube',
[
'data-videoid' => 'XGSy3_Czz8k',
'layout' => 'responsive',
'width' => '560',
'height' => '315'
]
)
->shouldBeCalled()
->willReturn($ampYoutubeElement)
;

$this->handleObject($event, $element);
}

function it_skips_converting_non_youtube_object_tags(
EventInterface $event,
ElementInterface $element,
ElementInterface $childElement
) {
$element
->getChildren()
->shouldBeCalled()
->willReturn([$childElement])
;

$childElement
->getAttribute('value')
->shouldBeCalled()
->willReturn('http://metube.com/embed/XGSy3_Czz8k')
;

$event->stopPropagation()->shouldNotBeCalled();
$element->createWritableElement('div', [
'class' => 'youtube-container'
])->shouldNotBeCalled();

$this->handleObject($event, $element);
}
}
6 changes: 5 additions & 1 deletion src/Converter/Extensions/YoutubeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public function handleObject(EventInterface $event, ElementInterface $element)
}

if ($embedCode !== false) {
$container = $element->createWritableElement('div', 'youtube-container');

$container = $element->createWritableElement('div', [
'class' => 'youtube-container'
]);

$container->appendChild($this->createAmpTag($element, $embedCode));
$element->replaceWith($container);
$event->stopPropagation();
Expand Down