|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Boo\WebpackEncorePlugin\Tests\TagRenderer; |
| 6 | + |
| 7 | +use Boo\WebpackEncorePlugin\Asset\EntrypointLookupInterface; |
| 8 | +use Boo\WebpackEncorePlugin\TagRenderer\TagRenderer; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +class TagRendererTest extends TestCase |
| 12 | +{ |
| 13 | + private $model; |
| 14 | + |
| 15 | + private $entrypointLookupMock; |
| 16 | + |
| 17 | + protected function setUp(): void |
| 18 | + { |
| 19 | + $this->entrypointLookupMock = $this->getMockBuilder(EntrypointLookupInterface::class) |
| 20 | + ->disableOriginalConstructor() |
| 21 | + ->onlyMethods(['getCssFiles', 'getJavaScriptFiles']) |
| 22 | + ->getMockForAbstractClass(); |
| 23 | + |
| 24 | + $this->model = new TagRenderer($this->entrypointLookupMock); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @dataProvider renderWebpackLinkTagsDataProvider |
| 29 | + */ |
| 30 | + public function testRenderWebpackLinkTags(string $entryName, array $extraAttributes, array $cssFiles, string $expectedLinkTagString) |
| 31 | + { |
| 32 | + $this->entrypointLookupMock->expects($this->once()) |
| 33 | + ->method('getCssFiles') |
| 34 | + ->with($entryName) |
| 35 | + ->willReturn($cssFiles); |
| 36 | + |
| 37 | + $this->entrypointLookupMock->expects($this->never()) |
| 38 | + ->method('getJavaScriptFiles'); |
| 39 | + |
| 40 | + $this->entrypointLookupMock->expects($this->never()) |
| 41 | + ->method('entryExists'); |
| 42 | + |
| 43 | + $this->assertEquals($expectedLinkTagString, $this->model->renderWebpackLinkTags($entryName, $extraAttributes)); |
| 44 | + } |
| 45 | + |
| 46 | + public function renderWebpackLinkTagsDataProvider(): array |
| 47 | + { |
| 48 | + return [ |
| 49 | + 'some-entry-with-two-css-files' => [ |
| 50 | + 'entryName' => 'some-entry-name', |
| 51 | + 'extraAttributes' => [], |
| 52 | + 'cssFiles' => [ |
| 53 | + '/path/to/css/file.css', |
| 54 | + '/path/to/another/css/file.css', |
| 55 | + ], |
| 56 | + 'expectedLinkTagString' => '<link rel="stylesheet" href="/path/to/css/file.css"><link rel="stylesheet" href="/path/to/another/css/file.css">', |
| 57 | + ] |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @dataProvider renderWebpackScriptTagsDataProvider |
| 63 | + */ |
| 64 | + public function testRenderWebpackScriptTags(string $entryName, array $extraAttributes, array $jsFiles, string $expectedScriptTagString) |
| 65 | + { |
| 66 | + $this->entrypointLookupMock->expects($this->once()) |
| 67 | + ->method('getJavaScriptFiles') |
| 68 | + ->with($entryName) |
| 69 | + ->willReturn($jsFiles); |
| 70 | + |
| 71 | + $this->entrypointLookupMock->expects($this->never()) |
| 72 | + ->method('getCssFiles'); |
| 73 | + |
| 74 | + $this->entrypointLookupMock->expects($this->never()) |
| 75 | + ->method('entryExists'); |
| 76 | + |
| 77 | + $this->assertEquals($expectedScriptTagString, $this->model->renderWebpackScriptTags($entryName, $extraAttributes)); |
| 78 | + } |
| 79 | + |
| 80 | + public function renderWebpackScriptTagsDataProvider(): array |
| 81 | + { |
| 82 | + return [ |
| 83 | + 'some-entry-name' => [ |
| 84 | + 'entryName' => 'some-entry-name', |
| 85 | + 'extraAttributes' => [], |
| 86 | + 'jsFiles' => [ |
| 87 | + '/path/to/js/file.js', |
| 88 | + '/path/to/another/js/file.js', |
| 89 | + ], |
| 90 | + 'expectedScriptTagString' => '<script src="/path/to/js/file.js" type="application/javascript"></script><script src="/path/to/another/js/file.js" type="application/javascript"></script>', |
| 91 | + ] |
| 92 | + ]; |
| 93 | + } |
| 94 | +} |
0 commit comments