123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- namespace Illuminate\Tests\Foundation;
- use Exception;
- use Illuminate\Contracts\Config\Repository;
- use Illuminate\Foundation\Application;
- use Illuminate\Foundation\Mix;
- use Illuminate\Support\Str;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- use stdClass;
- class FoundationHelpersTest extends TestCase
- {
- protected function tearDown(): void
- {
- m::close();
- }
- public function testCache()
- {
- $app = new Application;
- $app['cache'] = $cache = m::mock(stdClass::class);
- // 1. cache()
- $this->assertInstanceOf(stdClass::class, cache());
- // 2. cache(['foo' => 'bar'], 1);
- $cache->shouldReceive('put')->once()->with('foo', 'bar', 1);
- cache(['foo' => 'bar'], 1);
- // 3. cache('foo');
- $cache->shouldReceive('get')->once()->with('foo')->andReturn('bar');
- $this->assertSame('bar', cache('foo'));
- // 4. cache('foo', null);
- $cache->shouldReceive('get')->once()->with('foo', null)->andReturn('bar');
- $this->assertSame('bar', cache('foo', null));
- // 5. cache('baz', 'default');
- $cache->shouldReceive('get')->once()->with('baz', 'default')->andReturn('default');
- $this->assertSame('default', cache('baz', 'default'));
- }
- public function testMixDoesNotIncludeHost()
- {
- $app = new Application;
- $app['config'] = m::mock(Repository::class);
- $app['config']->shouldReceive('get')->with('app.mix_url');
- $app['config']->shouldReceive('get')->with('app.mix_hot_proxy_url');
- $manifest = $this->makeManifest();
- $result = mix('/unversioned.css');
- $this->assertSame('/versioned.css', $result->toHtml());
- unlink($manifest);
- }
- public function testMixCachesManifestForSubsequentCalls()
- {
- $app = new Application;
- $app['config'] = m::mock(Repository::class);
- $app['config']->shouldReceive('get')->with('app.mix_url');
- $app['config']->shouldReceive('get')->with('app.mix_hot_proxy_url');
- $manifest = $this->makeManifest();
- mix('unversioned.css');
- unlink($manifest);
- $result = mix('/unversioned.css');
- $this->assertSame('/versioned.css', $result->toHtml());
- }
- public function testMixAssetMissingStartingSlashHaveItAdded()
- {
- $app = new Application;
- $app['config'] = m::mock(Repository::class);
- $app['config']->shouldReceive('get')->with('app.mix_url');
- $app['config']->shouldReceive('get')->with('app.mix_hot_proxy_url');
- $manifest = $this->makeManifest();
- $result = mix('unversioned.css');
- $this->assertSame('/versioned.css', $result->toHtml());
- unlink($manifest);
- }
- public function testMixMissingManifestThrowsException()
- {
- $this->expectException(Exception::class);
- $this->expectExceptionMessage('The Mix manifest does not exist.');
- mix('unversioned.css', 'missing');
- }
- public function testMixWithManifestDirectory()
- {
- $app = new Application;
- $app['config'] = m::mock(Repository::class);
- $app['config']->shouldReceive('get')->with('app.mix_url');
- $app['config']->shouldReceive('get')->with('app.mix_hot_proxy_url');
- mkdir($directory = __DIR__.'/mix');
- $manifest = $this->makeManifest('mix');
- $result = mix('unversioned.css', 'mix');
- $this->assertSame('/mix/versioned.css', $result->toHtml());
- unlink($manifest);
- rmdir($directory);
- }
- public function testMixManifestDirectoryMissingStartingSlashHasItAdded()
- {
- mkdir($directory = __DIR__.'/mix');
- $manifest = $this->makeManifest('/mix');
- $result = mix('unversioned.css', 'mix');
- $this->assertSame('/mix/versioned.css', $result->toHtml());
- unlink($manifest);
- rmdir($directory);
- }
- public function testMixHotModuleReloadingGetsUrlFromFileWithHttps()
- {
- $path = $this->makeHotModuleReloadFile('https://laravel.com/docs');
- $result = mix('unversioned.css');
- $this->assertSame('//laravel.com/docs/unversioned.css', $result->toHtml());
- unlink($path);
- }
- public function testMixHotModuleReloadingGetsUrlFromFileWithHttp()
- {
- $path = $this->makeHotModuleReloadFile('http://laravel.com/docs');
- $result = mix('unversioned.css');
- $this->assertSame('//laravel.com/docs/unversioned.css', $result->toHtml());
- unlink($path);
- }
- public function testMixHotModuleReloadingGetsUrlFromFileWithManifestDirectoryAndHttps()
- {
- mkdir($directory = __DIR__.'/mix');
- $path = $this->makeHotModuleReloadFile('https://laravel.com/docs', 'mix');
- $result = mix('unversioned.css', 'mix');
- $this->assertSame('//laravel.com/docs/unversioned.css', $result->toHtml());
- unlink($path);
- rmdir($directory);
- }
- public function testMixHotModuleReloadingGetsUrlFromFileWithManifestDirectoryAndHttp()
- {
- mkdir($directory = __DIR__.'/mix');
- $path = $this->makeHotModuleReloadFile('http://laravel.com/docs', 'mix');
- $result = mix('unversioned.css', 'mix');
- $this->assertSame('//laravel.com/docs/unversioned.css', $result->toHtml());
- unlink($path);
- rmdir($directory);
- }
- public function testMixHotModuleReloadingUsesLocalhostIfNoHttpScheme()
- {
- $path = $this->makeHotModuleReloadFile('');
- $result = mix('unversioned.css');
- $this->assertSame('//localhost:8080/unversioned.css', $result->toHtml());
- unlink($path);
- }
- public function testMixHotModuleReloadingWithManifestDirectoryUsesLocalhostIfNoHttpScheme()
- {
- mkdir($directory = __DIR__.'/mix');
- $path = $this->makeHotModuleReloadFile('', 'mix');
- $result = mix('unversioned.css', 'mix');
- $this->assertSame('//localhost:8080/unversioned.css', $result->toHtml());
- unlink($path);
- rmdir($directory);
- }
- protected function makeHotModuleReloadFile($url, $directory = '')
- {
- app()->singleton('path.public', function () {
- return __DIR__;
- });
- $path = public_path(Str::finish($directory, '/').'hot');
- // Laravel mix when run 'hot' has a new line after the
- // url, so for consistency this "\n" is added.
- file_put_contents($path, "{$url}\n");
- return $path;
- }
- protected function makeManifest($directory = '')
- {
- app()->singleton('path.public', function () {
- return __DIR__;
- });
- $path = public_path(Str::finish($directory, '/').'mix-manifest.json');
- touch($path);
- // Laravel mix prints JSON pretty and with escaped
- // slashes, so we are doing that here for consistency.
- $content = json_encode(['/unversioned.css' => '/versioned.css'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
- file_put_contents($path, $content);
- return $path;
- }
- public function testMixIsSwappableForTests()
- {
- (new Application)->instance(Mix::class, function () {
- return 'expected';
- });
- $this->assertSame('expected', mix('asset.png'));
- }
- }
|