ViewTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Facade\Ignition\Tests;
  3. use Facade\Ignition\Exceptions\ViewException;
  4. use Facade\Ignition\Exceptions\ViewExceptionWithSolution;
  5. use Facade\IgnitionContracts\BaseSolution;
  6. use Facade\IgnitionContracts\ProvidesSolution;
  7. use Facade\IgnitionContracts\Solution;
  8. use Illuminate\Foundation\Auth\User;
  9. use Illuminate\Support\Facades\View;
  10. class ViewTest extends TestCase
  11. {
  12. public function setUp(): void
  13. {
  14. parent::setUp();
  15. View::addLocation(__DIR__.'/stubs/views');
  16. }
  17. /** @test */
  18. public function it_detects_blade_view_exceptions()
  19. {
  20. $this->expectException(ViewException::class);
  21. view('blade-exception')->render();
  22. }
  23. /** @test */
  24. public function it_detects_the_original_line_number_in_view_exceptions()
  25. {
  26. try {
  27. view('blade-exception')->render();
  28. } catch (ViewException $exception) {
  29. $this->assertSame(3, $exception->getLine());
  30. }
  31. }
  32. /** @test */
  33. public function it_detects_the_original_line_number_in_view_exceptions_with_utf8_characters()
  34. {
  35. try {
  36. view('blade-exception-utf8')->render();
  37. } catch (ViewException $exception) {
  38. $this->assertSame(11, $exception->getLine());
  39. }
  40. }
  41. /** @test */
  42. public function it_adds_additional_blade_information_to_the_exception()
  43. {
  44. $viewData = [
  45. 'app' => 'foo',
  46. 'data' => true,
  47. 'user' => new User(),
  48. ];
  49. try {
  50. view('blade-exception', $viewData)->render();
  51. } catch (ViewException $exception) {
  52. $this->assertSame($viewData, $exception->getViewData());
  53. }
  54. }
  55. /** @test */
  56. public function it_adds_base_exception_solution_to_view_exception()
  57. {
  58. try {
  59. $exception = new ExceptionWithSolution();
  60. view('solution-exception', ['exception' => $exception])->render();
  61. } catch (ViewException $exception) {
  62. $this->assertTrue($exception instanceof ViewExceptionWithSolution);
  63. $this->assertInstanceOf(Solution::class, $exception->getSolution());
  64. $this->assertSame('This is a solution', $exception->getSolution()->getSolutionTitle());
  65. }
  66. }
  67. /** @test */
  68. public function it_detects_php_view_exceptions()
  69. {
  70. $this->expectException(ViewException::class);
  71. view('php-exception')->render();
  72. }
  73. }
  74. class ExceptionWithSolution extends \Exception implements ProvidesSolution
  75. {
  76. public function getSolution(): Solution
  77. {
  78. return BaseSolution::create('This is a solution')
  79. ->setSolutionDescription('With a description');
  80. }
  81. }