Skip to content

Commit 2b40059

Browse files
committed
wip
1 parent 0af9c03 commit 2b40059

File tree

3 files changed

+342
-0
lines changed

3 files changed

+342
-0
lines changed

tests/Unit/DTO/MFilesErrorTest.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use CodebarAg\MFiles\DTO\MFilesError;
6+
use CodebarAg\MFiles\Exceptions\MFilesErrorException;
7+
8+
it('creates exception with MFilesError', function () {
9+
$error = new MFilesError(
10+
errorCode: 'ERROR_001',
11+
status: 403,
12+
url: '/session/vaults',
13+
method: 'GET',
14+
exceptionName: 'UnauthorizedAccessException',
15+
exceptionMessage: 'Login to application failed',
16+
stack: 'Stack trace here'
17+
);
18+
19+
$exception = new MFilesErrorException($error);
20+
21+
expect($exception->error)->toBe($error);
22+
expect($exception->getMessage())->toBe('Login to application failed');
23+
expect($exception->getCode())->toBe(403);
24+
});
25+
26+
it('extends base Exception class', function () {
27+
$error = new MFilesError(
28+
errorCode: 'ERROR_002',
29+
status: 404,
30+
url: '/objects/123',
31+
method: 'GET',
32+
exceptionName: 'NotFoundException',
33+
exceptionMessage: 'Object not found',
34+
stack: null
35+
);
36+
37+
$exception = new MFilesErrorException($error);
38+
39+
expect($exception)->toBeInstanceOf(Exception::class);
40+
});
41+
42+
it('can access error properties through exception', function () {
43+
$error = new MFilesError(
44+
errorCode: 'ERROR_003',
45+
status: 500,
46+
url: '/api/endpoint',
47+
method: 'POST',
48+
exceptionName: 'InternalServerError',
49+
exceptionMessage: 'Internal server error occurred',
50+
stack: 'Detailed stack trace'
51+
);
52+
53+
$exception = new MFilesErrorException($error);
54+
55+
expect($exception->error->errorCode)->toBe('ERROR_003');
56+
expect($exception->error->status)->toBe(500);
57+
expect($exception->error->url)->toBe('/api/endpoint');
58+
expect($exception->error->method)->toBe('POST');
59+
expect($exception->error->exceptionName)->toBe('InternalServerError');
60+
expect($exception->error->exceptionMessage)->toBe('Internal server error occurred');
61+
expect($exception->error->stack)->toBe('Detailed stack trace');
62+
});
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use CodebarAg\MFiles\DTO\MFilesError;
6+
use CodebarAg\MFiles\Responses\ErrorResponse;
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Connector;
9+
use Saloon\Http\Faking\MockResponse;
10+
use Saloon\Http\Request;
11+
use Saloon\Laravel\Facades\Saloon;
12+
13+
it('creates MFilesError from response json', function () {
14+
$json = [
15+
'ErrorCode' => 'ERROR_001',
16+
'Status' => 403,
17+
'URL' => '/session/vaults',
18+
'Method' => 'GET',
19+
'Exception' => [
20+
'Name' => 'UnauthorizedAccessException',
21+
'Message' => 'Login to application failed',
22+
],
23+
'Stack' => 'Stack trace here',
24+
];
25+
26+
$request = new class extends Request
27+
{
28+
protected Method $method = Method::GET;
29+
30+
public function resolveEndpoint(): string
31+
{
32+
return '/test';
33+
}
34+
};
35+
36+
Saloon::fake([
37+
get_class($request) => MockResponse::make(status: 403, body: json_encode($json)),
38+
]);
39+
40+
$connector = new class extends Connector
41+
{
42+
public function resolveBaseUrl(): string
43+
{
44+
return 'https://test.example.com';
45+
}
46+
};
47+
48+
$response = $connector->send($request);
49+
50+
$error = ErrorResponse::createDtoFromResponse($response);
51+
52+
expect($error)->toBeInstanceOf(MFilesError::class);
53+
expect($error->errorCode)->toBe('ERROR_001');
54+
expect($error->status)->toBe(403);
55+
expect($error->url)->toBe('/session/vaults');
56+
expect($error->method)->toBe('GET');
57+
expect($error->exceptionName)->toBe('UnauthorizedAccessException');
58+
expect($error->exceptionMessage)->toBe('Login to application failed');
59+
expect($error->stack)->toBe('Stack trace here');
60+
});
61+
62+
it('handles response with missing optional fields', function () {
63+
$json = [
64+
'Status' => 400,
65+
'URL' => '/test',
66+
'Method' => 'POST',
67+
'Exception' => [
68+
'Name' => 'BadRequest',
69+
'Message' => 'Bad request',
70+
],
71+
];
72+
73+
$request = new class extends Request
74+
{
75+
protected Method $method = Method::GET;
76+
77+
public function resolveEndpoint(): string
78+
{
79+
return '/test';
80+
}
81+
};
82+
83+
Saloon::fake([
84+
get_class($request) => MockResponse::make(status: 400, body: json_encode($json)),
85+
]);
86+
87+
$connector = new class extends Connector
88+
{
89+
public function resolveBaseUrl(): string
90+
{
91+
return 'https://test.example.com';
92+
}
93+
};
94+
95+
$response = $connector->send($request);
96+
97+
$error = ErrorResponse::createDtoFromResponse($response);
98+
99+
expect($error->errorCode)->toBe('');
100+
expect($error->stack)->toBeNull();
101+
});
102+
103+
it('handles empty exception object', function () {
104+
$json = [
105+
'Status' => 500,
106+
'URL' => '/api',
107+
'Method' => 'GET',
108+
'Exception' => [],
109+
];
110+
111+
$request = new class extends Request
112+
{
113+
protected Method $method = Method::GET;
114+
115+
public function resolveEndpoint(): string
116+
{
117+
return '/test';
118+
}
119+
};
120+
121+
Saloon::fake([
122+
get_class($request) => MockResponse::make(status: 500, body: json_encode($json)),
123+
]);
124+
125+
$connector = new class extends Connector
126+
{
127+
public function resolveBaseUrl(): string
128+
{
129+
return 'https://test.example.com';
130+
}
131+
};
132+
133+
$response = $connector->send($request);
134+
135+
$error = ErrorResponse::createDtoFromResponse($response);
136+
137+
expect($error->exceptionName)->toBe('');
138+
expect($error->exceptionMessage)->toBe('');
139+
});

0 commit comments

Comments
 (0)