|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use App\Exceptions\BizException; |
| 6 | +use Illuminate\Support\Facades\Log; |
| 7 | +use Illuminate\Support\Str; |
| 8 | +use Illuminate\Http\JsonResponse; |
| 9 | + |
| 10 | +beforeEach(function () { |
| 11 | + $this->exception = BizException::make('test.message'); |
| 12 | +}); |
| 13 | + |
| 14 | +it('can be created using make method', function () { |
| 15 | + $exception = BizException::make('test.message'); |
| 16 | + |
| 17 | + expect($exception)->toBeInstanceOf(BizException::class) |
| 18 | + ->and($exception->getMessage())->toBe('test.message'); |
| 19 | +}); |
| 20 | + |
| 21 | +it('can set error code', function () { |
| 22 | + $this->exception->code(422); |
| 23 | + |
| 24 | + expect($this->exception->getCode())->toBe(422); |
| 25 | +}); |
| 26 | + |
| 27 | +it('can add translation parameters using with method with key-value pair', function () { |
| 28 | + $this->exception->with('name', 'John'); |
| 29 | + |
| 30 | + $reflectionClass = new ReflectionClass($this->exception); |
| 31 | + $property = $reflectionClass->getProperty('transParams'); |
| 32 | + $property->setAccessible(true); |
| 33 | + |
| 34 | + expect($property->getValue($this->exception))->toBe(['name' => 'John']); |
| 35 | +}); |
| 36 | + |
| 37 | +it('can add translation parameters using with method with array', function () { |
| 38 | + $this->exception->with(['name' => 'John', 'age' => 30]); |
| 39 | + |
| 40 | + $reflectionClass = new ReflectionClass($this->exception); |
| 41 | + $property = $reflectionClass->getProperty('transParams'); |
| 42 | + $property->setAccessible(true); |
| 43 | + |
| 44 | + expect($property->getValue($this->exception))->toBe(['name' => 'John', 'age' => 30]); |
| 45 | +}); |
| 46 | + |
| 47 | +it('can set log message', function () { |
| 48 | + $this->exception->logMessage('Custom log message'); |
| 49 | + |
| 50 | + $reflectionClass = new ReflectionClass($this->exception); |
| 51 | + $property = $reflectionClass->getProperty('logMessage'); |
| 52 | + $property->setAccessible(true); |
| 53 | + |
| 54 | + expect($property->getValue($this->exception))->toBe('Custom log message'); |
| 55 | +}); |
| 56 | + |
| 57 | +it('can set log context', function () { |
| 58 | + $context = ['user_id' => 1, 'action' => 'create']; |
| 59 | + $this->exception->logContext($context); |
| 60 | + |
| 61 | + $reflectionClass = new ReflectionClass($this->exception); |
| 62 | + $property = $reflectionClass->getProperty('logContext'); |
| 63 | + $property->setAccessible(true); |
| 64 | + |
| 65 | + expect($property->getValue($this->exception))->toBe($context); |
| 66 | +}); |
| 67 | + |
| 68 | +it('reports to log channel', function () { |
| 69 | + Log::shouldReceive('channel') |
| 70 | + ->once() |
| 71 | + ->with('biz') |
| 72 | + ->andReturnSelf(); |
| 73 | + |
| 74 | + Log::shouldReceive('error') |
| 75 | + ->once() |
| 76 | + ->withArgs(function ($message, $data) { |
| 77 | + return $message === '[BizError] test.message' && |
| 78 | + isset($data['client_params']) && |
| 79 | + isset($data['log_context']) && |
| 80 | + isset($data['file']) && |
| 81 | + isset($data['line']) && |
| 82 | + isset($data['filtered_trace']); |
| 83 | + }); |
| 84 | + |
| 85 | + $this->exception->report(); |
| 86 | +}); |
| 87 | + |
| 88 | +it('renders as JSON response', function () { |
| 89 | + $this->exception->code(422)->with('param', 'value'); |
| 90 | + |
| 91 | + $response = $this->exception->render(); |
| 92 | + |
| 93 | + expect($response)->toBeInstanceOf(JsonResponse::class) |
| 94 | + ->and($response->getStatusCode())->toBe(422) |
| 95 | + ->and(json_decode($response->getContent(), true))->toBe([ |
| 96 | + 'success' => false, |
| 97 | + 'message' => 'test.message' |
| 98 | + ]); |
| 99 | +}); |
| 100 | + |
| 101 | +it('uses default error code 400 when no code set', function () { |
| 102 | + // Mock the trans function |
| 103 | + $this->mock('alias:trans', function ($mock) { |
| 104 | + $mock->shouldReceive('__invoke') |
| 105 | + ->with('test.message', []) |
| 106 | + ->andReturn('Translated message'); |
| 107 | + }); |
| 108 | + |
| 109 | + $response = $this->exception->render(); |
| 110 | + |
| 111 | + expect($response->getStatusCode())->toBe(400); |
| 112 | +}); |
| 113 | + |
| 114 | +it('uses custom log message when available', function () { |
| 115 | + $this->exception->logMessage('Custom log message'); |
| 116 | + |
| 117 | + $reflectionClass = new ReflectionClass($this->exception); |
| 118 | + $method = $reflectionClass->getMethod('getLogMessage'); |
| 119 | + $method->setAccessible(true); |
| 120 | + |
| 121 | + expect($method->invoke($this->exception))->toBe('Custom log message'); |
| 122 | +}); |
| 123 | + |
| 124 | +it('formats caller correctly with class type and function', function () { |
| 125 | + $reflectionClass = new ReflectionClass($this->exception); |
| 126 | + $method = $reflectionClass->getMethod('formatCaller'); |
| 127 | + $method->setAccessible(true); |
| 128 | + |
| 129 | + $trace = [ |
| 130 | + 'class' => 'TestClass', |
| 131 | + 'type' => '::', |
| 132 | + 'function' => 'testMethod' |
| 133 | + ]; |
| 134 | + |
| 135 | + expect($method->invoke($this->exception, $trace))->toBe('TestClass::testMethod()'); |
| 136 | +}); |
| 137 | + |
| 138 | +it('formats caller correctly with only function', function () { |
| 139 | + $reflectionClass = new ReflectionClass($this->exception); |
| 140 | + $method = $reflectionClass->getMethod('formatCaller'); |
| 141 | + $method->setAccessible(true); |
| 142 | + |
| 143 | + $trace = [ |
| 144 | + 'function' => 'testMethod' |
| 145 | + ]; |
| 146 | + |
| 147 | + expect($method->invoke($this->exception, $trace))->toBe('testMethod()'); |
| 148 | +}); |
0 commit comments