26
26
27
27
class MappedAssetFactoryTest extends TestCase
28
28
{
29
+ final const DEFAULT_FIXTURES = __DIR__ .'/../Fixtures/assets/vendor ' ;
30
+
29
31
private AssetMapperInterface &MockObject $ assetMapper ;
30
32
31
33
public function testCreateMappedAsset ()
32
34
{
33
- $ factory = $ this ->createFactory ();
35
+ $ factory = $ this ->createFactory (self :: DEFAULT_FIXTURES );
34
36
35
37
$ asset = $ factory ->createMappedAsset ('file2.js ' , __DIR__ .'/../Fixtures/dir1/file2.js ' );
36
38
$ this ->assertSame ('file2.js ' , $ asset ->logicalPath );
@@ -40,7 +42,7 @@ public function testCreateMappedAsset()
40
42
41
43
public function testCreateMappedAssetRespectsPreDigestedPaths ()
42
44
{
43
- $ assetMapper = $ this ->createFactory ();
45
+ $ assetMapper = $ this ->createFactory (self :: DEFAULT_FIXTURES );
44
46
$ asset = $ assetMapper ->createMappedAsset ('already-abcdefVWXYZ0123456789.digested.css ' , __DIR__ .'/../Fixtures/dir2/already-abcdefVWXYZ0123456789.digested.css ' );
45
47
$ this ->assertSame ('already-abcdefVWXYZ0123456789.digested.css ' , $ asset ->logicalPath );
46
48
$ this ->assertSame ('/final-assets/already-abcdefVWXYZ0123456789.digested.css ' , $ asset ->publicPath );
@@ -62,7 +64,7 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
62
64
}
63
65
};
64
66
65
- $ assetMapper = $ this ->createFactory ($ file1Compiler );
67
+ $ assetMapper = $ this ->createFactory (self :: DEFAULT_FIXTURES , $ file1Compiler );
66
68
$ expected = 'totally changed ' ;
67
69
68
70
$ asset = $ assetMapper ->createMappedAsset ('file1.css ' , __DIR__ .'/../Fixtures/dir1/file1.css ' );
@@ -75,15 +77,15 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
75
77
76
78
public function testCreateMappedAssetWithContentThatDoesNotChange ()
77
79
{
78
- $ assetMapper = $ this ->createFactory ();
80
+ $ assetMapper = $ this ->createFactory (self :: DEFAULT_FIXTURES );
79
81
$ asset = $ assetMapper ->createMappedAsset ('file1.css ' , __DIR__ .'/../Fixtures/dir1/file1.css ' );
80
82
// null content because the final content matches the file source
81
83
$ this ->assertNull ($ asset ->content );
82
84
}
83
85
84
86
public function testCreateMappedAssetWithContentErrorsOnCircularReferences ()
85
87
{
86
- $ factory = $ this ->createFactory ();
88
+ $ factory = $ this ->createFactory (self :: DEFAULT_FIXTURES );
87
89
88
90
$ this ->expectException (CircularAssetsException::class);
89
91
$ this ->expectExceptionMessage ('Circular reference detected while creating asset for "circular1.css": "circular1.css -> circular2.css -> circular1.css". ' );
@@ -108,36 +110,47 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
108
110
}
109
111
};
110
112
111
- $ factory = $ this ->createFactory ();
113
+ $ factory = $ this ->createFactory (self :: DEFAULT_FIXTURES );
112
114
$ asset = $ factory ->createMappedAsset ('subdir/file6.js ' , __DIR__ .'/../Fixtures/dir2/subdir/file6.js ' );
113
115
$ this ->assertSame ('7f983f4053a57f07551fed6099c0da4e ' , $ asset ->digest );
114
116
$ this ->assertFalse ($ asset ->isPredigested );
115
117
116
118
// trigger the compiler, which will change file5.js
117
119
// since file6.js imports file5.js, the digest for file6 should change,
118
120
// because, internally, the file path in file6.js to file5.js will need to change
119
- $ factory = $ this ->createFactory ($ file6Compiler );
121
+ $ factory = $ this ->createFactory (self :: DEFAULT_FIXTURES , $ file6Compiler );
120
122
$ asset = $ factory ->createMappedAsset ('subdir/file6.js ' , __DIR__ .'/../Fixtures/dir2/subdir/file6.js ' );
121
123
$ this ->assertSame ('7e4f24ebddd4ab2a3bcf0d89270b9f30 ' , $ asset ->digest );
122
124
}
123
125
124
126
public function testCreateMappedAssetWithPredigested ()
125
127
{
126
- $ assetMapper = $ this ->createFactory ();
128
+ $ assetMapper = $ this ->createFactory (self :: DEFAULT_FIXTURES );
127
129
$ asset = $ assetMapper ->createMappedAsset ('already-abcdefVWXYZ0123456789.digested.css ' , __DIR__ .'/../Fixtures/dir2/already-abcdefVWXYZ0123456789.digested.css ' );
128
130
$ this ->assertSame ('abcdefVWXYZ0123456789.digested ' , $ asset ->digest );
129
131
$ this ->assertTrue ($ asset ->isPredigested );
130
132
}
131
133
132
134
public function testCreateMappedAssetInVendor ()
133
135
{
134
- $ assetMapper = $ this ->createFactory ();
136
+ $ assetMapper = $ this ->createFactory (self :: DEFAULT_FIXTURES );
135
137
$ asset = $ assetMapper ->createMappedAsset ('lodash.js ' , __DIR__ .'/../Fixtures/assets/vendor/lodash/lodash.index.js ' );
136
138
$ this ->assertSame ('lodash.js ' , $ asset ->logicalPath );
137
139
$ this ->assertTrue ($ asset ->isVendor );
138
140
}
139
141
140
- private function createFactory (?AssetCompilerInterface $ extraCompiler = null ): MappedAssetFactory
142
+ public function testCreateMappedAssetInMissingVendor ()
143
+ {
144
+ $ assetMapper = $ this ->createFactory (self ::DEFAULT_FIXTURES .'/this-does-not-exist/ ' );
145
+ $ asset = $ assetMapper ->createMappedAsset ('lodash.js ' , __DIR__ .'/../Fixtures/assets/vendor/lodash/lodash.index.js ' );
146
+ $ this ->assertSame ('lodash.js ' , $ asset ->logicalPath );
147
+ $ this ->assertFalse ($ asset ->isVendor );
148
+ }
149
+
150
+ private function createFactory (
151
+ string $ vendorDir ,
152
+ ?AssetCompilerInterface $ extraCompiler = null ,
153
+ ): MappedAssetFactory
141
154
{
142
155
$ compilers = [
143
156
new JavaScriptImportPathCompiler ($ this ->createMock (ImportMapConfigReader::class)),
@@ -162,7 +175,7 @@ private function createFactory(?AssetCompilerInterface $extraCompiler = null): M
162
175
$ factory = new MappedAssetFactory (
163
176
$ pathResolver ,
164
177
$ compiler ,
165
- __DIR__ . ' /../Fixtures/assets/vendor ' ,
178
+ $ vendorDir ,
166
179
);
167
180
168
181
// mock the AssetMapper to behave like normal: by calling back to the factory
0 commit comments