|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use CodebarAg\MFiles\DTO\MFilesError; |
| 6 | + |
| 7 | +it('creates instance with all properties', function () { |
| 8 | + $error = new MFilesError( |
| 9 | + errorCode: 'ERROR_001', |
| 10 | + status: 403, |
| 11 | + url: '/session/vaults', |
| 12 | + method: 'GET', |
| 13 | + exceptionName: 'UnauthorizedAccessException', |
| 14 | + exceptionMessage: 'Login to application failed', |
| 15 | + stack: 'Stack trace here' |
| 16 | + ); |
| 17 | + |
| 18 | + expect($error->errorCode)->toBe('ERROR_001'); |
| 19 | + expect($error->status)->toBe(403); |
| 20 | + expect($error->url)->toBe('/session/vaults'); |
| 21 | + expect($error->method)->toBe('GET'); |
| 22 | + expect($error->exceptionName)->toBe('UnauthorizedAccessException'); |
| 23 | + expect($error->exceptionMessage)->toBe('Login to application failed'); |
| 24 | + expect($error->stack)->toBe('Stack trace here'); |
| 25 | +}); |
| 26 | + |
| 27 | +it('creates instance with null stack', function () { |
| 28 | + $error = new MFilesError( |
| 29 | + errorCode: 'ERROR_002', |
| 30 | + status: 404, |
| 31 | + url: '/objects/123', |
| 32 | + method: 'GET', |
| 33 | + exceptionName: 'NotFoundException', |
| 34 | + exceptionMessage: 'Object not found', |
| 35 | + stack: null |
| 36 | + ); |
| 37 | + |
| 38 | + expect($error->stack)->toBeNull(); |
| 39 | +}); |
| 40 | + |
| 41 | +it('creates instance from array with all properties', function () { |
| 42 | + $data = [ |
| 43 | + 'ErrorCode' => 'ERROR_003', |
| 44 | + 'Status' => 500, |
| 45 | + 'URL' => '/api/endpoint', |
| 46 | + 'Method' => 'POST', |
| 47 | + 'Exception' => [ |
| 48 | + 'Name' => 'InternalServerError', |
| 49 | + 'Message' => 'Internal server error occurred', |
| 50 | + ], |
| 51 | + 'Stack' => 'Detailed stack trace', |
| 52 | + ]; |
| 53 | + |
| 54 | + $error = MFilesError::fromArray($data); |
| 55 | + |
| 56 | + expect($error->errorCode)->toBe('ERROR_003'); |
| 57 | + expect($error->status)->toBe(500); |
| 58 | + expect($error->url)->toBe('/api/endpoint'); |
| 59 | + expect($error->method)->toBe('POST'); |
| 60 | + expect($error->exceptionName)->toBe('InternalServerError'); |
| 61 | + expect($error->exceptionMessage)->toBe('Internal server error occurred'); |
| 62 | + expect($error->stack)->toBe('Detailed stack trace'); |
| 63 | +}); |
| 64 | + |
| 65 | +it('creates instance from array with missing optional fields', function () { |
| 66 | + $data = [ |
| 67 | + 'Status' => 400, |
| 68 | + 'URL' => '/test', |
| 69 | + 'Method' => 'GET', |
| 70 | + 'Exception' => [ |
| 71 | + 'Name' => 'BadRequest', |
| 72 | + 'Message' => 'Bad request', |
| 73 | + ], |
| 74 | + ]; |
| 75 | + |
| 76 | + $error = MFilesError::fromArray($data); |
| 77 | + |
| 78 | + expect($error->errorCode)->toBe(''); |
| 79 | + expect($error->status)->toBe(400); |
| 80 | + expect($error->url)->toBe('/test'); |
| 81 | + expect($error->method)->toBe('GET'); |
| 82 | + expect($error->exceptionName)->toBe('BadRequest'); |
| 83 | + expect($error->exceptionMessage)->toBe('Bad request'); |
| 84 | + expect($error->stack)->toBeNull(); |
| 85 | +}); |
| 86 | + |
| 87 | +it('creates instance from array with empty exception object', function () { |
| 88 | + $data = [ |
| 89 | + 'ErrorCode' => 'ERROR_004', |
| 90 | + 'Status' => 401, |
| 91 | + 'URL' => '/auth', |
| 92 | + 'Method' => 'POST', |
| 93 | + 'Exception' => [], |
| 94 | + 'Stack' => null, |
| 95 | + ]; |
| 96 | + |
| 97 | + $error = MFilesError::fromArray($data); |
| 98 | + |
| 99 | + expect($error->exceptionName)->toBe(''); |
| 100 | + expect($error->exceptionMessage)->toBe(''); |
| 101 | +}); |
| 102 | + |
| 103 | +it('converts instance to array correctly', function () { |
| 104 | + $error = new MFilesError( |
| 105 | + errorCode: 'ERROR_005', |
| 106 | + status: 422, |
| 107 | + url: '/validate', |
| 108 | + method: 'PUT', |
| 109 | + exceptionName: 'ValidationError', |
| 110 | + exceptionMessage: 'Validation failed', |
| 111 | + stack: 'Error stack' |
| 112 | + ); |
| 113 | + |
| 114 | + $array = $error->toArray(); |
| 115 | + |
| 116 | + expect($array)->toBe([ |
| 117 | + 'errorCode' => 'ERROR_005', |
| 118 | + 'status' => 422, |
| 119 | + 'url' => '/validate', |
| 120 | + 'method' => 'PUT', |
| 121 | + 'exceptionName' => 'ValidationError', |
| 122 | + 'exceptionMessage' => 'Validation failed', |
| 123 | + 'stack' => 'Error stack', |
| 124 | + ]); |
| 125 | +}); |
| 126 | + |
| 127 | +it('converts instance to array with null stack', function () { |
| 128 | + $error = new MFilesError( |
| 129 | + errorCode: 'ERROR_006', |
| 130 | + status: 500, |
| 131 | + url: '/test', |
| 132 | + method: 'DELETE', |
| 133 | + exceptionName: 'ServerError', |
| 134 | + exceptionMessage: 'Server error', |
| 135 | + stack: null |
| 136 | + ); |
| 137 | + |
| 138 | + $array = $error->toArray(); |
| 139 | + |
| 140 | + expect($array['stack'])->toBeNull(); |
| 141 | +}); |
0 commit comments