FoundationHelpersTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Illuminate\Tests\Integration\Foundation;
  3. use Exception;
  4. use Illuminate\Contracts\Debug\ExceptionHandler;
  5. use Illuminate\Support\Facades\Route;
  6. use Illuminate\Support\Str;
  7. use Orchestra\Testbench\TestCase;
  8. class FoundationHelpersTest extends TestCase
  9. {
  10. public function testRescue()
  11. {
  12. $this->assertEquals(
  13. 'rescued!',
  14. rescue(function () {
  15. throw new Exception;
  16. }, 'rescued!')
  17. );
  18. $this->assertEquals(
  19. 'rescued!',
  20. rescue(function () {
  21. throw new Exception;
  22. }, function () {
  23. return 'rescued!';
  24. })
  25. );
  26. $this->assertEquals(
  27. 'no need to rescue',
  28. rescue(function () {
  29. return 'no need to rescue';
  30. }, 'rescued!')
  31. );
  32. $testClass = new class
  33. {
  34. public function test(int $a)
  35. {
  36. return $a;
  37. }
  38. };
  39. $this->assertEquals(
  40. 'rescued!',
  41. rescue(function () use ($testClass) {
  42. $testClass->test([]);
  43. }, 'rescued!')
  44. );
  45. }
  46. public function testMixReportsExceptionWhenAssetIsMissingFromManifest()
  47. {
  48. $handler = new FakeHandler;
  49. $this->app->instance(ExceptionHandler::class, $handler);
  50. $manifest = $this->makeManifest();
  51. mix('missing.js');
  52. $this->assertInstanceOf(Exception::class, $handler->reported[0]);
  53. $this->assertSame('Unable to locate Mix file: /missing.js.', $handler->reported[0]->getMessage());
  54. unlink($manifest);
  55. }
  56. public function testMixSilentlyFailsWhenAssetIsMissingFromManifestWhenNotInDebugMode()
  57. {
  58. $this->app['config']->set('app.debug', false);
  59. $manifest = $this->makeManifest();
  60. $path = mix('missing.js');
  61. $this->assertSame('/missing.js', $path);
  62. unlink($manifest);
  63. }
  64. public function testMixThrowsExceptionWhenAssetIsMissingFromManifestWhenInDebugMode()
  65. {
  66. $this->expectException(Exception::class);
  67. $this->expectExceptionMessage('Unable to locate Mix file: /missing.js.');
  68. $this->app['config']->set('app.debug', true);
  69. $manifest = $this->makeManifest();
  70. try {
  71. mix('missing.js');
  72. } catch (Exception $e) {
  73. throw $e;
  74. } finally { // make sure we can cleanup the file
  75. unlink($manifest);
  76. }
  77. }
  78. public function testMixOnlyThrowsAndReportsOneExceptionWhenAssetIsMissingFromManifestWhenInDebugMode()
  79. {
  80. $handler = new FakeHandler;
  81. $this->app->instance(ExceptionHandler::class, $handler);
  82. $this->app['config']->set('app.debug', true);
  83. $manifest = $this->makeManifest();
  84. Route::get('test-route', function () {
  85. mix('missing.js');
  86. });
  87. $this->get('/test-route');
  88. $this->assertCount(1, $handler->reported);
  89. unlink($manifest);
  90. }
  91. protected function makeManifest($directory = '')
  92. {
  93. $this->app->singleton('path.public', function () {
  94. return __DIR__;
  95. });
  96. $path = public_path(Str::finish($directory, '/').'mix-manifest.json');
  97. touch($path);
  98. // Laravel mix prints JSON pretty and with escaped
  99. // slashes, so we are doing that here for consistency.
  100. $content = json_encode(['/unversioned.css' => '/versioned.css'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  101. file_put_contents($path, $content);
  102. return $path;
  103. }
  104. }
  105. class FakeHandler
  106. {
  107. public $reported = [];
  108. public function report($exception)
  109. {
  110. $this->reported[] = $exception;
  111. }
  112. public function render($exception)
  113. {
  114. //
  115. }
  116. }