Skip to content

Commit 161a5e8

Browse files
committed
Add test regarding EntrypointLookupCollection.
1 parent 57d9c75 commit 161a5e8

File tree

7 files changed

+194
-5
lines changed

7 files changed

+194
-5
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Symfony\WebpackEncoreBundle\Tests\Asset;
4+
5+
use Symfony\Component\DependencyInjection\ServiceLocator;
6+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class EntrypointLookupCollectionTest extends TestCase
10+
{
11+
/**
12+
* @expectedException Symfony\WebpackEncoreBundle\Exception\UndefinedBuildException
13+
* @expectedExceptionMessage Given entry point "something" is not configured
14+
*/
15+
public function testExceptionOnMissingEntry()
16+
{
17+
$collection = new EntrypointLookupCollection(new ServiceLocator([]));
18+
$collection->getEntrypointLookup('something');
19+
}
20+
}

tests/Asset/EntrypointLookupTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ public function testGetJavaScriptFiles()
4848
['file1.js', 'file2.js'],
4949
$this->entrypointLookup->getJavaScriptFiles('my_entry')
5050
);
51+
52+
$this->assertEquals(
53+
[],
54+
$this->entrypointLookup->getJavaScriptFiles('my_entry')
55+
);
56+
57+
$this->entrypointLookup->reset();
58+
59+
$this->assertEquals(
60+
['file1.js', 'file2.js'],
61+
$this->entrypointLookup->getJavaScriptFiles('my_entry')
62+
);
5163
}
5264

5365
public function testGetJavaScriptFilesReturnsUniqueFilesOnly()
@@ -79,6 +91,32 @@ public function testEmptyReturnOnValidEntryNoJsOrCssFile()
7991
);
8092
}
8193

94+
/**
95+
* @expectedException \InvalidArgumentException
96+
* @expectedExceptionMessageContains There was a problem JSON decoding the
97+
*/
98+
public function testExceptionOnInvalidJson()
99+
{
100+
$filename = tempnam(sys_get_temp_dir(), 'WebpackEncoreBundle');
101+
file_put_contents($filename, "abcd");
102+
103+
$this->entrypointLookup = new EntrypointLookup($filename);
104+
$this->entrypointLookup->getJavaScriptFiles('an_entry');
105+
}
106+
107+
/**
108+
* @expectedException \InvalidArgumentException
109+
* @expectedExceptionMessageContains Could not find an "entrypoints" key in the
110+
*/
111+
public function testExceptionOnMissingEntrypointsKeyInJson()
112+
{
113+
$filename = tempnam(sys_get_temp_dir(), 'WebpackEncoreBundle');
114+
file_put_contents($filename, "{}");
115+
116+
$this->entrypointLookup = new EntrypointLookup($filename);
117+
$this->entrypointLookup->getJavaScriptFiles('an_entry');
118+
}
119+
82120
/**
83121
* @expectedException \InvalidArgumentException
84122
* @expectedExceptionMessage Could not find the entrypoints file

tests/Asset/TagRendererTest.php

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\Asset\Packages;
77
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
8+
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollection;
89
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
910

1011
class TagRendererTest extends TestCase
@@ -15,6 +16,11 @@ public function testRenderScriptTags()
1516
$entrypointLookup->expects($this->once())
1617
->method('getJavaScriptFiles')
1718
->willReturn(['/build/file1.js', '/build/file2.js']);
19+
$entrypointCollection = $this->createMock(EntrypointLookupCollection::class);
20+
$entrypointCollection->expects($this->once())
21+
->method('getEntrypointLookup')
22+
->withConsecutive(['_default'])
23+
->will($this->onConsecutiveCalls($entrypointLookup));
1824

1925
$packages = $this->createMock(Packages::class);
2026
$packages->expects($this->exactly(2))
@@ -26,7 +32,7 @@ public function testRenderScriptTags()
2632
->willReturnCallback(function($path) {
2733
return 'http://localhost:8080'.$path;
2834
});
29-
$renderer = new TagRenderer($entrypointLookup, $packages);
35+
$renderer = new TagRenderer($entrypointCollection, $packages);
3036

3137
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
3238
$this->assertContains(
@@ -45,19 +51,81 @@ public function testRenderScriptTagsWithBadFilename()
4551
$entrypointLookup->expects($this->once())
4652
->method('getJavaScriptFiles')
4753
->willReturn(['/build/file<"bad_chars.js']);
54+
$entrypointCollection = $this->createMock(EntrypointLookupCollection::class);
55+
$entrypointCollection->expects($this->once())
56+
->method('getEntrypointLookup')
57+
->withConsecutive(['_default'])
58+
->will($this->onConsecutiveCalls($entrypointLookup));
4859

4960
$packages = $this->createMock(Packages::class);
5061
$packages->expects($this->once())
5162
->method('getUrl')
5263
->willReturnCallback(function($path) {
5364
return 'http://localhost:8080'.$path;
5465
});
55-
$renderer = new TagRenderer($entrypointLookup, $packages);
66+
$renderer = new TagRenderer($entrypointCollection, $packages);
5667

5768
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
5869
$this->assertContains(
5970
'<script src="http://localhost:8080/build/file&lt;&quot;bad_chars.js"></script>',
6071
$output
6172
);
6273
}
74+
75+
public function testRenderScriptTagsWithinAnEntryPointCollection()
76+
{
77+
$entrypointLookup = $this->createMock(EntrypointLookupInterface::class);
78+
$entrypointLookup->expects($this->once())
79+
->method('getJavaScriptFiles')
80+
->willReturn(['/build/file1.js']);
81+
82+
$secondEntrypointLookup = $this->createMock(EntrypointLookupInterface::class);
83+
$secondEntrypointLookup->expects($this->once())
84+
->method('getJavaScriptFiles')
85+
->willReturn(['/build/file2.js']);
86+
$thirdEntrypointLookup = $this->createMock(EntrypointLookupInterface::class);
87+
$thirdEntrypointLookup->expects($this->once())
88+
->method('getJavaScriptFiles')
89+
->willReturn(['/build/file3.js']);
90+
91+
$entrypointCollection = $this->createMock(EntrypointLookupCollection::class);
92+
$entrypointCollection->expects($this->exactly(3))
93+
->method('getEntrypointLookup')
94+
->withConsecutive(['_default'], ['second'], ['third'])
95+
->will($this->onConsecutiveCalls(
96+
$entrypointLookup,
97+
$secondEntrypointLookup,
98+
$thirdEntrypointLookup
99+
));
100+
101+
$packages = $this->createMock(Packages::class);
102+
$packages->expects($this->exactly(3))
103+
->method('getUrl')
104+
->withConsecutive(
105+
['/build/file1.js', 'custom_package'],
106+
['/build/file2.js', null],
107+
['/build/file3.js', 'specific_package']
108+
)
109+
->willReturnCallback(function($path) {
110+
return 'http://localhost:8080'.$path;
111+
});
112+
$renderer = new TagRenderer($entrypointCollection, $packages);
113+
114+
$output = $renderer->renderWebpackScriptTags('my_entry', 'custom_package');
115+
$this->assertContains(
116+
'<script src="http://localhost:8080/build/file1.js"></script>',
117+
$output
118+
);
119+
$output = $renderer->renderWebpackScriptTags('my_entry', null, 'second');
120+
$this->assertContains(
121+
'<script src="http://localhost:8080/build/file2.js"></script>',
122+
$output
123+
);
124+
$output = $renderer->renderWebpackScriptTags('my_entry', 'specific_package', 'third');
125+
$this->assertContains(
126+
'<script src="http://localhost:8080/build/file3.js"></script>',
127+
$output
128+
);
129+
}
130+
63131
}

tests/IntegrationTest.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,24 @@ public function testTwigIntegration()
1818
$kernel->boot();
1919
$container = $kernel->getContainer();
2020

21-
$html2 = $container->get('twig')->render('@integration_test/template.twig');
21+
$html1 = $container->get('twig')->render('@integration_test/template.twig');
2222
$this->assertContains(
2323
'<script src="/build/file1.js"></script>',
24-
$html2
24+
$html1
25+
);
26+
$this->assertContains(
27+
'<link rel="stylesheet" href="/build/styles.css" />'.
28+
'<link rel="stylesheet" href="/build/styles2.css" />',
29+
$html1
30+
);
31+
$this->assertContains(
32+
'<script src="/build/other3.js"></script>',
33+
$html1
34+
);
35+
$this->assertContains(
36+
'<link rel="stylesheet" href="/build/styles3.css" />'.
37+
'<link rel="stylesheet" href="/build/styles4.css" />',
38+
$html1
2539
);
2640

2741
$html2 = $container->get('twig')->render('@integration_test/manual_template.twig');
@@ -34,6 +48,20 @@ public function testTwigIntegration()
3448
'<script src="/build/file1.js"></script>',
3549
$html2
3650
);
51+
$this->assertContains(
52+
'<script src="/build/other4.js"></script>',
53+
$html2
54+
);
55+
// styles3.css is not repeated
56+
$this->assertNotContains(
57+
'<link rel="stylesheet" href="/build/styles3.css" />',
58+
$html2
59+
);
60+
// styles4.css is not repeated
61+
$this->assertNotContains(
62+
'<link rel="stylesheet" href="/build/styles4.css" />',
63+
$html2
64+
);
3765
}
3866
}
3967

@@ -75,6 +103,9 @@ public function registerContainerConfiguration(LoaderInterface $loader)
75103

76104
$container->loadFromExtension('webpack_encore', [
77105
'output_path' => __DIR__.'/fixtures/build',
106+
'builds' => [
107+
'other' => __DIR__.'/fixtures/other-build'
108+
]
78109
]);
79110
});
80111
}
@@ -88,4 +119,4 @@ public function getLogDir()
88119
{
89120
return sys_get_temp_dir().'/logs'.spl_object_hash($this);
90121
}
91-
}
122+
}

tests/fixtures/manual_template.twig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@
55
{% for cssFile in encore_entry_css_files('other_entry') %}
66
<link rel="stylesheet" href="{{ asset(cssFile) }}" />
77
{% endfor %}
8+
9+
{% for jsFile in encore_entry_js_files('next_entry', 'other') %}
10+
<script src="{{ asset(jsFile) }}"></script>
11+
{% endfor %}
12+
13+
{% for cssFile in encore_entry_css_files('next_entry', 'other') %}
14+
<link rel="stylesheet" href="{{ asset(cssFile) }}" />
15+
{% endfor %}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"entrypoints": {
3+
"third_entry": {
4+
"js": [
5+
"build/other3.js"
6+
],
7+
"css": [
8+
"build/styles3.css",
9+
"build/styles4.css"
10+
]
11+
},
12+
"next_entry": {
13+
"js": [
14+
"build/other4.js"
15+
],
16+
"css": [
17+
"build/styles3.css",
18+
"build/styles4.css"
19+
]
20+
}
21+
}
22+
}

tests/fixtures/template.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
{{ encore_entry_script_tags('my_entry') }}
22
{{ encore_entry_link_tags('my_entry') }}
3+
{{ encore_entry_script_tags('third_entry', null, 'other') }}
4+
{{ encore_entry_link_tags('third_entry', null, 'other') }}

0 commit comments

Comments
 (0)