Skip to content

Commit 434b1d5

Browse files
committed
Chore:
- Added unit test TagRendererTest;
1 parent 33433b2 commit 434b1d5

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77
"require": {
88
"micro/plugin-twig": "^0"
99
},
10+
"require-dev": {
11+
"phpunit/phpunit": "^9.5"
12+
},
1013
"autoload": {
1114
"psr-4": {
12-
"Boo\\WebpackEncorePlugin\\": "src/"
15+
"Boo\\WebpackEncorePlugin\\": "src/",
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Boo\\WebpackEncorePlugin\\Tests\\": "tests/"
1321
}
1422
},
1523
"authors": [

phpunit.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" verbose="false" beStrictAboutCoversAnnotation="false">
3+
<php>
4+
<ini name="error_reporting" value="-1" force="true"/>
5+
</php>
6+
<testsuites>
7+
<testsuite name="Main test suite">
8+
<directory>tests/</directory>
9+
</testsuite>
10+
</testsuites>
11+
</phpunit>

tests/TagRenderer/TagRendererTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)