|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Exceptions; |
| 4 | + |
| 5 | +use Gsousadev\LaravelProblemDetailExceptions\Enums\ExceptionsFieldsEnum; |
| 6 | +use Gsousadev\LaravelProblemDetailExceptions\Exceptions\ClientException; |
| 7 | +use Gsousadev\LaravelProblemDetailExceptions\Exceptions\ServerException; |
| 8 | +use Illuminate\Http\Response; |
| 9 | +use Illuminate\Support\Facades\Log; |
| 10 | +use Orchestra\Testbench\TestCase; |
| 11 | + |
| 12 | +class ProblemDetailExceptionTest extends TestCase |
| 13 | +{ |
| 14 | + public function testShouldReturnFieldsByConfig() |
| 15 | + { |
| 16 | + config()->set('problem-detail-exceptions.app_name', 'TEST'); |
| 17 | + config()->set('problem-detail-exceptions.enable_log_in_exception', true); |
| 18 | + config()->set('problem-detail-exceptions.available_fields_list', [ |
| 19 | + ExceptionsFieldsEnum::MESSAGE, |
| 20 | + ExceptionsFieldsEnum::STATUS |
| 21 | + ]); |
| 22 | + config()->set('problem-detail-exceptions.renderable_fields_list', [ |
| 23 | + ExceptionsFieldsEnum::STATUS |
| 24 | + ]); |
| 25 | + |
| 26 | + $exception = new ServerException(); |
| 27 | + |
| 28 | + $this->assertEquals( |
| 29 | + [ |
| 30 | + 'message' => 'Erro interno do servidor - O servidor encontrou um erro ' . |
| 31 | + 'interno e não foi capaz de completar sua requisição', |
| 32 | + 'status' => '500' |
| 33 | + ], |
| 34 | + $exception->toArray() |
| 35 | + ); |
| 36 | + |
| 37 | + $this->assertEquals(500, $exception->getCode()); |
| 38 | + $this->assertInstanceOf(Response::class, $exception->render()); |
| 39 | + |
| 40 | + Log::shouldReceive('sharedContext'); |
| 41 | + Log::shouldReceive('error')->with( |
| 42 | + '[TEST][UNEX0001]', |
| 43 | + [ |
| 44 | + 'message' => 'Erro interno do servidor - O servidor encontrou um erro ' . |
| 45 | + 'interno e não foi capaz de completar sua requisição', |
| 46 | + 'status' => '500' |
| 47 | + ] |
| 48 | + ); |
| 49 | + |
| 50 | + $this->assertTrue($exception->report()); |
| 51 | + |
| 52 | + |
| 53 | + $this->assertEquals('Erro interno do servidor', $exception->getTitle()); |
| 54 | + $this->assertEquals( |
| 55 | + 'O servidor encontrou um erro interno e não foi capaz de completar sua requisição', |
| 56 | + $exception->getDetail() |
| 57 | + ); |
| 58 | + $this->assertEquals('Erro interno do servidor', $exception->getUserTitle()); |
| 59 | + $this->assertEquals( |
| 60 | + 'O servidor encontrou um erro interno e não foi capaz de completar sua requisição.', |
| 61 | + $exception->getUserMessage() |
| 62 | + ); |
| 63 | + $this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $exception->getHttpStatus()); |
| 64 | + $this->assertEquals('UNEX0001', $exception->getInternalCode()); |
| 65 | + $this->assertEquals(null, $exception->getPrevious()); |
| 66 | + } |
| 67 | + |
| 68 | + public function testShouldThrowExceptionWhenFieldsConfigIsNotArray() |
| 69 | + { |
| 70 | + config()->set('problem-detail-exceptions.app_name', 'TEST'); |
| 71 | + config()->set('problem-detail-exceptions.enable_log_in_exception', false); |
| 72 | + config()->set('problem-detail-exceptions.available_fields_list', 'teste'); |
| 73 | + config()->set('problem-detail-exceptions.renderable_fields_list', 'teste'); |
| 74 | + |
| 75 | + $this->expectException(\InvalidArgumentException::class); |
| 76 | + $this->expectExceptionMessage('renderable_fields must be an array in config/problem-detail-exceptions.php'); |
| 77 | + |
| 78 | + new ClientException(); |
| 79 | + } |
| 80 | + |
| 81 | + public function testShouldThrowExceptionWhenFieldsConfigCountIsZero() |
| 82 | + { |
| 83 | + config()->set('problem-detail-exceptions.app_name', 'TEST'); |
| 84 | + config()->set('problem-detail-exceptions.enable_log_in_exception', false); |
| 85 | + config()->set('problem-detail-exceptions.available_fields_list', []); |
| 86 | + config()->set('problem-detail-exceptions.renderable_fields_list', []); |
| 87 | + |
| 88 | + $this->expectException(\InvalidArgumentException::class); |
| 89 | + $this->expectExceptionMessage( |
| 90 | + 'renderable_fields must have at least one element in ' . |
| 91 | + 'config/problem-detail-exceptions.php' |
| 92 | + ); |
| 93 | + |
| 94 | + new ClientException(); |
| 95 | + } |
| 96 | + |
| 97 | + public function testShouldThrowExceptionWhenSameFieldConfigTypeIsNotSupported() |
| 98 | + { |
| 99 | + config()->set('problem-detail-exceptions.app_name', 'TEST'); |
| 100 | + config()->set('problem-detail-exceptions.enable_log_in_exception', false); |
| 101 | + config()->set( |
| 102 | + 'problem-detail-exceptions.available_fields_list', |
| 103 | + [ |
| 104 | + 'teste', |
| 105 | + ExceptionsFieldsEnum::MESSAGE |
| 106 | + ] |
| 107 | + ); |
| 108 | + config()->set( |
| 109 | + 'problem-detail-exceptions.renderable_fields_list', |
| 110 | + [ |
| 111 | + ExceptionsFieldsEnum::MESSAGE, |
| 112 | + ExceptionsFieldsEnum::STATUS |
| 113 | + ] |
| 114 | + ); |
| 115 | + |
| 116 | + $this->expectException(\InvalidArgumentException::class); |
| 117 | + $this->expectExceptionMessage( |
| 118 | + 'fields must be an array of ExceptionsFieldsEnum in ' . |
| 119 | + 'config/problem-detail-exceptions.php' |
| 120 | + ); |
| 121 | + |
| 122 | + new ClientException(); |
| 123 | + } |
| 124 | + |
| 125 | + public function testShouldThrowExceptionWhenSameRenderableFieldConfigTypeIsNotSupported() |
| 126 | + { |
| 127 | + config()->set('problem-detail-exceptions.app_name', 'TEST'); |
| 128 | + config()->set('problem-detail-exceptions.enable_log_in_exception', false); |
| 129 | + config()->set('problem-detail-exceptions.available_fields_list', [ |
| 130 | + ExceptionsFieldsEnum::STATUS, |
| 131 | + ExceptionsFieldsEnum::MESSAGE |
| 132 | + ]); |
| 133 | + config()->set( |
| 134 | + 'problem-detail-exceptions.renderable_fields_list', |
| 135 | + [ |
| 136 | + 'teste', |
| 137 | + ExceptionsFieldsEnum::STATUS |
| 138 | + ] |
| 139 | + ); |
| 140 | + |
| 141 | + $this->expectException(\InvalidArgumentException::class); |
| 142 | + $this->expectExceptionMessage( |
| 143 | + 'renderable_fields must be an array of ExceptionsFieldsEnum in ' . |
| 144 | + 'config/problem-detail-exceptions.php' |
| 145 | + ); |
| 146 | + |
| 147 | + new ClientException(); |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments