Skip to content

Commit 0212866

Browse files
authored
Merge pull request #3 from gsousadev/feature/testes-unitarios
[Feature] 100% Coverage
2 parents 3d2f4f5 + 0e3075e commit 0212866

File tree

3 files changed

+182
-14
lines changed

3 files changed

+182
-14
lines changed

phpunit.xml

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" bootstrap="vendor/autoload.php" colors="true" failOnRisky="true" failOnWarning="true" processIsolation="false" stopOnError="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd">
3-
<testsuites>
4-
<testsuite name="Laravel TestBench Test Suite">
5-
<directory suffix="Test.php">./Tests</directory>
6-
</testsuite>
7-
</testsuites>
8-
<coverage/>
9-
<source>
10-
<include>
11-
<directory suffix=".php">./src</directory>
12-
</include>
13-
</source>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
backupGlobals="false"
4+
beStrictAboutTestsThatDoNotTestAnything="true"
5+
beStrictAboutOutputDuringTests="true"
6+
bootstrap="vendor/autoload.php"
7+
colors="true" failOnRisky="true"
8+
failOnWarning="true"
9+
processIsolation="false"
10+
stopOnError="false"
11+
stopOnFailure="false"
12+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
13+
>
14+
<testsuites>
15+
<testsuite name="Laravel TestBench Test Suite">
16+
<directory suffix="Test.php">./tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
<coverage cacheDirectory=".phpunit.cache/code-coverage">
20+
<report>
21+
<clover outputFile="phpunit.coverage.xml"/>
22+
</report>
23+
</coverage>
24+
<source>
25+
<include>
26+
<directory suffix=".php">./src/Exceptions</directory>
27+
</include>
28+
</source>
29+
30+
<php>
31+
<server name="XDEBUG_MODE" value="coverage"/>
32+
</php>
1433
</phpunit>

src/Exceptions/ProblemDetailException.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Exception;
88
use Gsousadev\LaravelProblemDetailExceptions\Enums\ExceptionsFieldsEnum;
99
use Illuminate\Http\Response;
10-
use Illuminate\Http\Request;
1110
use Illuminate\Support\Facades\Log;
1211

1312
abstract class ProblemDetailException extends Exception
@@ -63,7 +62,7 @@ function ($key) {
6362
);
6463
}
6564

66-
public function render(Request $request): Response
65+
public function render(): Response
6766
{
6867
$data = array_filter(
6968
$this->toArray(),
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

Comments
 (0)