Skip to content

Commit

Permalink
Add Spec for YoutubeConverter Extension (#6)
Browse files Browse the repository at this point in the history
* Add Spec for YoutubeConverter Extension

Provide a spec for the YoutubeConverter for better test coverage and to
fix regression bug reported in issue #5.

Fixes #5

* OCD Kicking In D:
  • Loading branch information
paulredmond committed May 30, 2016
1 parent 67049bc commit 91fb7f0
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 1 deletion.
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

0 comments on commit 91fb7f0

Please sign in to comment.