1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Tests\Unit\Extension\Embed;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
use League\CommonMark\Extension\Embed\Embed;
use League\CommonMark\Extension\Embed\EmbedAdapterInterface;
use League\CommonMark\Extension\Embed\EmbedProcessor;
use League\CommonMark\Node\Block\Document;
use League\CommonMark\Node\Block\Paragraph;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
use PHPUnit\Framework\TestCase;
final class EmbedProcessorTest extends TestCase
{
public function testUpdatesEmbeds(): void
{
$adapter = new FakeAdapter();
$processor = new EmbedProcessor($adapter);
$document = new Document();
$document->appendChild($embed1 = new Embed('https://www.youtube.com/watch?v=dQw4w9WgXcQ'));
$document->appendChild($embed2 = new Embed('https://www.youtube.com/watch?v=jsYAYXeVtoM'));
$processor(new DocumentParsedEvent($document));
$this->assertSame([$embed1, $embed2], $adapter->getUpdatedEmbeds());
$this->assertNotNull($embed1->getEmbedCode());
$this->assertNotNull($embed2->getEmbedCode());
}
public function testNoUpdatesEmbedsWithoutEmbeds(): void
{
$adapter = $this->getMockBuilder(EmbedAdapterInterface::class)->getMock();
$adapter->expects($this->never())
->method('updateEmbeds');
$processor = new EmbedProcessor($adapter);
$document = new Document();
$processor(new DocumentParsedEvent($document));
}
#[RequiresPhpunit('< 12')]
public function testWithFallbackRemove(): void
{
// A fake adapter that doesn't do anything (like updating the embed codes)
$adapter = $this->getMockForAbstractClass(EmbedAdapterInterface::class);
$processor = new EmbedProcessor($adapter, EmbedProcessor::FALLBACK_REMOVE);
$document = new Document();
$document->appendChild(new Embed('https://www.youtube.com/watch?v=dQw4w9WgXcQ'));
$document->appendChild(new Embed('https://www.youtube.com/watch?v=jsYAYXeVtoM'));
$processor(new DocumentParsedEvent($document));
$this->assertFalse($document->hasChildren());
}
#[RequiresPhpunit('< 12')]
public function testWithFallbackLink(): void
{
// A fake adapter that doesn't do anything (like updating the embed codes)
$adapter = $this->getMockForAbstractClass(EmbedAdapterInterface::class);
$processor = new EmbedProcessor($adapter, EmbedProcessor::FALLBACK_LINK);
$document = new Document();
$document->appendChild(new Embed('https://www.youtube.com/watch?v=dQw4w9WgXcQ'));
$document->appendChild(new Embed('https://www.youtube.com/watch?v=jsYAYXeVtoM'));
$processor(new DocumentParsedEvent($document));
$this->assertInstanceOf(Paragraph::class, $document->firstChild());
$this->assertInstanceOf(Link::class, $document->firstChild()->firstChild());
$this->assertSame('https://www.youtube.com/watch?v=dQw4w9WgXcQ', $document->firstChild()->firstChild()->getUrl());
$this->assertInstanceOf(Paragraph::class, $document->lastChild());
$this->assertInstanceOf(Link::class, $document->lastChild()->firstChild());
$this->assertSame('https://www.youtube.com/watch?v=jsYAYXeVtoM', $document->lastChild()->firstChild()->getUrl());
}
}
|