/
githubmirror
/
framework
Обзор
Документация
Войти
/
githubmirror
/
framework
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
13.x
tests/Integration/Mail/MarkdownParserTest.php
169 строк
7 KB
Lucas Michot
Centralize per-test static state resets in AfterEachTestSubscriber (#61078)
07 авг 2026, 19:38
Не верифицирован
07 авг 2026, 19:38
3eef88d
Код
Авторство
О чём код?
<?php namespace Illuminate\Tests\Integration\Mail; use Illuminate\Mail\Markdown; use Illuminate\Support\EncodedHtmlString; use Illuminate\Support\HtmlString; use Orchestra\Testbench\TestCase; use PHPUnit\Framework\Attributes\DataProvider; class MarkdownParserTest extends TestCase { protected function tearDown(): void { Markdown::flushState(); EncodedHtmlString::flushState(); parent::tearDown(); } #[DataProvider('markdownDataProvider')] public function testItCanParseMarkdownString($given, $expected) { tap(Markdown::parse($given), function ($html) use ($expected) { $this->assertInstanceOf(HtmlString::class, $html); $this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html); $this->assertSame((string) $html, (string) $html->toHtml()); }); } #[DataProvider('markdownEncodedDataProvider')] public function testItCanParseMarkdownEncodedString($given, $expected) { tap(Markdown::parse($given, encoded: true), function ($html) use ($expected) { $this->assertInstanceOf(HtmlString::class, $html); $this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html); }); } public static function markdownDataProvider() { yield ['[Laravel](https://laravel.com)', '<p><a href="https://laravel.com">Laravel</a></p>']; yield ['\[Laravel](https://laravel.com)', '<p>[Laravel](https://laravel.com)</p>']; yield ['', '<p><img src="https://laravel.com/assets/img/welcome/background.svg" alt="Welcome to Laravel" /></p>']; yield ['!\[Welcome to Laravel](https://laravel.com/assets/img/welcome/background.svg)', '<p></p>']; yield ['Visit https://laravel.com/docs to browse the documentation', '<p>Visit https://laravel.com/docs to browse the documentation</p>']; yield ['Visit <https://laravel.com/docs> to browse the documentation', '<p>Visit <a href="https://laravel.com/docs">https://laravel.com/docs</a> to browse the documentation</p>']; yield ['Visit <span>https://laravel.com/docs</span> to browse the documentation', '<p>Visit <span>https://laravel.com/docs</span> to browse the documentation</p>']; } public static function markdownEncodedDataProvider() { yield [new EncodedHtmlString('[Laravel](https://laravel.com)'), '<p>[Laravel](https://laravel.com)</p>']; yield [ new EncodedHtmlString(''), '<p></p>', ]; yield [ new EncodedHtmlString('Visit https://laravel.com/docs to browse the documentation'), '<p>Visit https://laravel.com/docs to browse the documentation</p>', ]; yield [ new EncodedHtmlString('Visit <https://laravel.com/docs> to browse the documentation'), '<p>Visit <https://laravel.com/docs> to browse the documentation</p>', ]; yield [ new EncodedHtmlString('Visit <span>https://laravel.com/docs</span> to browse the documentation'), '<p>Visit <span>https://laravel.com/docs</span> to browse the documentation</p>', ]; yield [ new EncodedHtmlString(new HtmlString('Visit <span>https://laravel.com/docs</span> to browse the documentation')), '<p>Visit <span>https://laravel.com/docs</span> to browse the documentation</p>', ]; yield [ '<br />'.new EncodedHtmlString('Visit <span>https://laravel.com/docs</span> to browse the documentation'), '<p><img src="https://laravel.com/assets/img/welcome/background.svg" alt="Welcome to Laravel" /><br />Visit <span>https://laravel.com/docs</span> to browse the documentation</p>', ]; } public function testItCanParseMarkdownWithCustomExtensionsViaConfig(): void { $this->configureMarkdownExtensions([ \League\CommonMark\Extension\Strikethrough\StrikethroughExtension::class, ]); tap(Markdown::parse('~~strikethrough text~~'), function ($html) { $this->assertInstanceOf(HtmlString::class, $html); $expected = '<p><del>strikethrough text</del></p>'; $this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html); $this->assertSame((string) $html, (string) $html->toHtml()); }); } public function testItCanParseMarkdownWithoutCustomExtensionsDoesNotApplyThem(): void { $this->configureMarkdownExtensions([]); tap(Markdown::parse('~~strikethrough text~~'), function ($html) { $this->assertInstanceOf(HtmlString::class, $html); $expected = '<p>~~strikethrough text~~</p>'; $this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html); $this->assertSame((string) $html, (string) $html->toHtml()); }); } public function testItCanParseMarkdownWithMultipleCustomExtensions(): void { $this->configureMarkdownExtensions([ \League\CommonMark\Extension\Strikethrough\StrikethroughExtension::class, \League\CommonMark\Extension\TaskList\TaskListExtension::class, ]); tap(Markdown::parse('~~strikethrough~~'), function ($html) { $this->assertInstanceOf(HtmlString::class, $html); $expected = '<p><del>strikethrough</del></p>'; $this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html); $this->assertSame((string) $html, (string) $html->toHtml()); }); tap(Markdown::parse('- [ ] Task item'), function ($html) { $this->assertInstanceOf(HtmlString::class, $html); $expected = "<ul>\n<li><input disabled=\"\" type=\"checkbox\"> Task item</li>\n</ul>"; $this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html); $this->assertSame((string) $html, (string) $html->toHtml()); }); } public function testItCanParseMarkdownEncodedStringWithCustomExtensions(): void { $this->configureMarkdownExtensions([ \League\CommonMark\Extension\Strikethrough\StrikethroughExtension::class, ]); tap(Markdown::parse(new EncodedHtmlString('~~strikethrough text~~'), encoded: true), function ($html) { $this->assertInstanceOf(HtmlString::class, $html); $expected = '<p><del>strikethrough text</del></p>'; $this->assertStringEqualsStringIgnoringLineEndings($expected.PHP_EOL, (string) $html); }); } /** * @param array<int, class-string<\League\CommonMark\Extension\ExtensionInterface>> $extensions */ protected function configureMarkdownExtensions(array $extensions): void { $this->app['config']->set('mail.markdown.extensions', $extensions); $this->app->forgetInstance(Markdown::class); $this->app->make(Markdown::class); } }