FoundationHelpersTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace Illuminate\Tests\Foundation;
  3. use Exception;
  4. use Illuminate\Contracts\Config\Repository;
  5. use Illuminate\Foundation\Application;
  6. use Illuminate\Foundation\Mix;
  7. use Illuminate\Support\Str;
  8. use Mockery as m;
  9. use PHPUnit\Framework\TestCase;
  10. use stdClass;
  11. class FoundationHelpersTest extends TestCase
  12. {
  13. protected function tearDown(): void
  14. {
  15. m::close();
  16. }
  17. public function testCache()
  18. {
  19. $app = new Application;
  20. $app['cache'] = $cache = m::mock(stdClass::class);
  21. // 1. cache()
  22. $this->assertInstanceOf(stdClass::class, cache());
  23. // 2. cache(['foo' => 'bar'], 1);
  24. $cache->shouldReceive('put')->once()->with('foo', 'bar', 1);
  25. cache(['foo' => 'bar'], 1);
  26. // 3. cache('foo');
  27. $cache->shouldReceive('get')->once()->with('foo')->andReturn('bar');
  28. $this->assertSame('bar', cache('foo'));
  29. // 4. cache('foo', null);
  30. $cache->shouldReceive('get')->once()->with('foo', null)->andReturn('bar');
  31. $this->assertSame('bar', cache('foo', null));
  32. // 5. cache('baz', 'default');
  33. $cache->shouldReceive('get')->once()->with('baz', 'default')->andReturn('default');
  34. $this->assertSame('default', cache('baz', 'default'));
  35. }
  36. public function testMixDoesNotIncludeHost()
  37. {
  38. $app = new Application;
  39. $app['config'] = m::mock(Repository::class);
  40. $app['config']->shouldReceive('get')->with('app.mix_url');
  41. $app['config']->shouldReceive('get')->with('app.mix_hot_proxy_url');
  42. $manifest = $this->makeManifest();
  43. $result = mix('/unversioned.css');
  44. $this->assertSame('/versioned.css', $result->toHtml());
  45. unlink($manifest);
  46. }
  47. public function testMixCachesManifestForSubsequentCalls()
  48. {
  49. $app = new Application;
  50. $app['config'] = m::mock(Repository::class);
  51. $app['config']->shouldReceive('get')->with('app.mix_url');
  52. $app['config']->shouldReceive('get')->with('app.mix_hot_proxy_url');
  53. $manifest = $this->makeManifest();
  54. mix('unversioned.css');
  55. unlink($manifest);
  56. $result = mix('/unversioned.css');
  57. $this->assertSame('/versioned.css', $result->toHtml());
  58. }
  59. public function testMixAssetMissingStartingSlashHaveItAdded()
  60. {
  61. $app = new Application;
  62. $app['config'] = m::mock(Repository::class);
  63. $app['config']->shouldReceive('get')->with('app.mix_url');
  64. $app['config']->shouldReceive('get')->with('app.mix_hot_proxy_url');
  65. $manifest = $this->makeManifest();
  66. $result = mix('unversioned.css');
  67. $this->assertSame('/versioned.css', $result->toHtml());
  68. unlink($manifest);
  69. }
  70. public function testMixMissingManifestThrowsException()
  71. {
  72. $this->expectException(Exception::class);
  73. $this->expectExceptionMessage('The Mix manifest does not exist.');
  74. mix('unversioned.css', 'missing');
  75. }
  76. public function testMixWithManifestDirectory()
  77. {
  78. $app = new Application;
  79. $app['config'] = m::mock(Repository::class);
  80. $app['config']->shouldReceive('get')->with('app.mix_url');
  81. $app['config']->shouldReceive('get')->with('app.mix_hot_proxy_url');
  82. mkdir($directory = __DIR__.'/mix');
  83. $manifest = $this->makeManifest('mix');
  84. $result = mix('unversioned.css', 'mix');
  85. $this->assertSame('/mix/versioned.css', $result->toHtml());
  86. unlink($manifest);
  87. rmdir($directory);
  88. }
  89. public function testMixManifestDirectoryMissingStartingSlashHasItAdded()
  90. {
  91. mkdir($directory = __DIR__.'/mix');
  92. $manifest = $this->makeManifest('/mix');
  93. $result = mix('unversioned.css', 'mix');
  94. $this->assertSame('/mix/versioned.css', $result->toHtml());
  95. unlink($manifest);
  96. rmdir($directory);
  97. }
  98. public function testMixHotModuleReloadingGetsUrlFromFileWithHttps()
  99. {
  100. $path = $this->makeHotModuleReloadFile('https://laravel.com/docs');
  101. $result = mix('unversioned.css');
  102. $this->assertSame('//laravel.com/docs/unversioned.css', $result->toHtml());
  103. unlink($path);
  104. }
  105. public function testMixHotModuleReloadingGetsUrlFromFileWithHttp()
  106. {
  107. $path = $this->makeHotModuleReloadFile('http://laravel.com/docs');
  108. $result = mix('unversioned.css');
  109. $this->assertSame('//laravel.com/docs/unversioned.css', $result->toHtml());
  110. unlink($path);
  111. }
  112. public function testMixHotModuleReloadingGetsUrlFromFileWithManifestDirectoryAndHttps()
  113. {
  114. mkdir($directory = __DIR__.'/mix');
  115. $path = $this->makeHotModuleReloadFile('https://laravel.com/docs', 'mix');
  116. $result = mix('unversioned.css', 'mix');
  117. $this->assertSame('//laravel.com/docs/unversioned.css', $result->toHtml());
  118. unlink($path);
  119. rmdir($directory);
  120. }
  121. public function testMixHotModuleReloadingGetsUrlFromFileWithManifestDirectoryAndHttp()
  122. {
  123. mkdir($directory = __DIR__.'/mix');
  124. $path = $this->makeHotModuleReloadFile('http://laravel.com/docs', 'mix');
  125. $result = mix('unversioned.css', 'mix');
  126. $this->assertSame('//laravel.com/docs/unversioned.css', $result->toHtml());
  127. unlink($path);
  128. rmdir($directory);
  129. }
  130. public function testMixHotModuleReloadingUsesLocalhostIfNoHttpScheme()
  131. {
  132. $path = $this->makeHotModuleReloadFile('');
  133. $result = mix('unversioned.css');
  134. $this->assertSame('//localhost:8080/unversioned.css', $result->toHtml());
  135. unlink($path);
  136. }
  137. public function testMixHotModuleReloadingWithManifestDirectoryUsesLocalhostIfNoHttpScheme()
  138. {
  139. mkdir($directory = __DIR__.'/mix');
  140. $path = $this->makeHotModuleReloadFile('', 'mix');
  141. $result = mix('unversioned.css', 'mix');
  142. $this->assertSame('//localhost:8080/unversioned.css', $result->toHtml());
  143. unlink($path);
  144. rmdir($directory);
  145. }
  146. protected function makeHotModuleReloadFile($url, $directory = '')
  147. {
  148. app()->singleton('path.public', function () {
  149. return __DIR__;
  150. });
  151. $path = public_path(Str::finish($directory, '/').'hot');
  152. // Laravel mix when run 'hot' has a new line after the
  153. // url, so for consistency this "\n" is added.
  154. file_put_contents($path, "{$url}\n");
  155. return $path;
  156. }
  157. protected function makeManifest($directory = '')
  158. {
  159. app()->singleton('path.public', function () {
  160. return __DIR__;
  161. });
  162. $path = public_path(Str::finish($directory, '/').'mix-manifest.json');
  163. touch($path);
  164. // Laravel mix prints JSON pretty and with escaped
  165. // slashes, so we are doing that here for consistency.
  166. $content = json_encode(['/unversioned.css' => '/versioned.css'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  167. file_put_contents($path, $content);
  168. return $path;
  169. }
  170. public function testMixIsSwappableForTests()
  171. {
  172. (new Application)->instance(Mix::class, function () {
  173. return 'expected';
  174. });
  175. $this->assertSame('expected', mix('asset.png'));
  176. }
  177. }