Skip to content

Commit 721912a

Browse files
committed
Chore: Unit tests
- Added more unit tests; - Small fixes;
1 parent 434b1d5 commit 721912a

File tree

5 files changed

+136
-7
lines changed

5 files changed

+136
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor
22
composer.lock
3+
.phpunit.result.cache

src/Asset/EntrypointLookup.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ private function validateEntryName(string $entryName)
6363
{
6464
$entriesData = $this->getEntriesData();
6565
if (!isset($entriesData['entrypoints'][$entryName])) {
66-
$withoutExtension = substr($entryName, 0, strrpos($entryName, '.'));
66+
if (false !== $dotPos = strrpos($entryName, '.')) {
67+
$withoutExtension = substr($entryName, 0, $dotPos);
6768

68-
if (isset($entriesData['entrypoints'][$withoutExtension])) {
69-
throw new EntrypointNotFoundException(sprintf('Could not find the entry "%s". Try "%s" instead (without the extension).', $entryName, $withoutExtension));
69+
if (isset($entriesData['entrypoints'][$withoutExtension])) {
70+
throw new EntrypointNotFoundException(sprintf('Could not find the entry "%s". Try "%s" instead (without the extension).', $entryName, $withoutExtension));
71+
}
7072
}
7173

7274
throw new EntrypointNotFoundException(sprintf('Could not find the entry "%s" in "%s". Found: %s.', $entryName, $this->getEntrypointJsonPath(), implode(', ', array_keys($entriesData['entrypoints']))));

tests/Asset/EntrypointLookupTest.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Boo\WebpackEncorePlugin\Tests\Asset;
6+
7+
use Boo\WebpackEncorePlugin\Asset\EntrypointLookup;
8+
use Boo\WebpackEncorePlugin\WebpackEncorePluginConfigurationInterface;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class EntrypointLookupTest extends TestCase
12+
{
13+
private EntrypointLookup $model;
14+
15+
protected function setUp(): void
16+
{
17+
$configurationMock = $this->getMockBuilder(WebpackEncorePluginConfigurationInterface::class)
18+
->disableOriginalConstructor()
19+
->onlyMethods(['getOutputPath'])
20+
->getMockForAbstractClass();
21+
22+
$configurationMock->expects($this->any())
23+
->method('getOutputPath')
24+
->willReturn(__DIR__.'/../fixtures/build');
25+
26+
$this->model = new EntrypointLookup($configurationMock);
27+
}
28+
29+
/**
30+
* @dataProvider getCssFilesDataProvider
31+
*/
32+
public function testGetCssFiles(string $entryName, array $expectedResult): void
33+
{
34+
$this->assertEquals($expectedResult, $this->model->getCssFiles($entryName));
35+
}
36+
37+
public function getCssFilesDataProvider(): array
38+
{
39+
return [
40+
[
41+
'entryName' => 'entrypoint1',
42+
'expectedResult' => [],
43+
],
44+
[
45+
'entryName' => 'entrypoint2',
46+
'expectedResult' => [
47+
"/some/path/to/js/file.css"
48+
],
49+
],
50+
];
51+
}
52+
53+
/**
54+
* @dataProvider getJavaScriptFilesDataProvider
55+
*/
56+
public function testGetJavaScriptFiles(string $entryName, array $expectedResult): void
57+
{
58+
$this->assertEquals($expectedResult, $this->model->getJavaScriptFiles($entryName));
59+
}
60+
61+
public function getJavaScriptFilesDataProvider(): array
62+
{
63+
return [
64+
[
65+
'entryName' => 'entrypoint1',
66+
'expectedResult' => [
67+
"/some/path/to/jsFile.js",
68+
"/another/path/to/js/file.js",
69+
"/build/app.js",
70+
],
71+
],
72+
[
73+
'entryName' => 'entrypoint2',
74+
'expectedResult' => [
75+
"/some/path/to/another/js/file.js",
76+
],
77+
],
78+
];
79+
}
80+
81+
/**
82+
* @dataProvider entryExistsDataProvider
83+
*/
84+
public function testEntryExists(string $entryName, bool $expectedResult): void
85+
{
86+
$this->assertEquals($expectedResult, $this->model->entryExists($entryName));
87+
}
88+
89+
public function entryExistsDataProvider(): array
90+
{
91+
return [
92+
[
93+
'entryName' => 'entrypoint1',
94+
'expectedResult' => true,
95+
],
96+
[
97+
'entryName' => 'entrypoint2',
98+
'expectedResult' => true,
99+
],
100+
[
101+
'entryName' => 'entrypoint3',
102+
'expectedResult' => false,
103+
],
104+
];
105+
}
106+
}

tests/TagRenderer/TagRendererTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
use Boo\WebpackEncorePlugin\Asset\EntrypointLookupInterface;
88
use Boo\WebpackEncorePlugin\TagRenderer\TagRenderer;
9+
use PHPUnit\Framework\MockObject\MockObject;
910
use PHPUnit\Framework\TestCase;
1011

1112
class TagRendererTest extends TestCase
1213
{
13-
private $model;
14+
private TagRenderer $model;
1415

15-
private $entrypointLookupMock;
16+
private EntrypointLookupInterface|MockObject $entrypointLookupMock;
1617

1718
protected function setUp(): void
1819
{
@@ -53,7 +54,7 @@ public function renderWebpackLinkTagsDataProvider(): array
5354
'/path/to/css/file.css',
5455
'/path/to/another/css/file.css',
5556
],
56-
'expectedLinkTagString' => '<link rel="stylesheet" href="/path/to/css/file.css"><link rel="stylesheet" href="/path/to/another/css/file.css">',
57+
'expectedLinkTagString' => /** @lang text */ '<link rel="stylesheet" href="/path/to/css/file.css"><link rel="stylesheet" href="/path/to/another/css/file.css">',
5758
]
5859
];
5960
}
@@ -87,7 +88,7 @@ public function renderWebpackScriptTagsDataProvider(): array
8788
'/path/to/js/file.js',
8889
'/path/to/another/js/file.js',
8990
],
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+
'expectedScriptTagString' => /** @lang text */ '<script src="/path/to/js/file.js" type="application/javascript"></script><script src="/path/to/another/js/file.js" type="application/javascript"></script>',
9192
]
9293
];
9394
}

tests/fixtures/build/entrypoints.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"entrypoints": {
3+
"entrypoint1": {
4+
"js": [
5+
"/some/path/to/jsFile.js",
6+
"/another/path/to/js/file.js",
7+
"/build/app.js"
8+
]
9+
},
10+
"entrypoint2": {
11+
"js": [
12+
"/some/path/to/another/js/file.js"
13+
],
14+
"css": [
15+
"/some/path/to/js/file.css"
16+
]
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)