Skip to content

Commit feb0d49

Browse files
authored
Merge pull request #4 from karoldabro/feature/update-to-l-11
Update to Laravel 11
2 parents c8c2000 + cad4e58 commit feb0d49

12 files changed

Lines changed: 66 additions & 68 deletions

.github/workflows/laravel.yml

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,6 @@ jobs:
3737
with:
3838
php-version: '8.2'
3939
- uses: actions/checkout@v3
40-
- name: Install Dependencies
41-
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
42-
- name: Create Database
43-
run: |
44-
mkdir -p database
45-
touch database/database.sqlite
46-
- name: Execute tests (Unit and Feature tests) via PHPUnit
47-
env:
48-
DB_CONNECTION: testing
49-
DB_DATABASE: database/database.sqlite
50-
run: vendor/bin/phpunit
51-
52-
53-
tests81:
54-
55-
runs-on: ubuntu-latest
56-
57-
steps:
58-
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
59-
with:
60-
php-version: '8.1'
61-
- uses: actions/checkout@v3
6240
- name: Install Dependencies
6341
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
6442
- name: Create Database

CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# CHANGELOG
2+
3+
## [2.0.0] - 2024-06-16
4+
5+
### Added
6+
- Support for Laravel 11
7+
- Changelog
8+
9+
## [1.1.1] - 2024-06-16
10+
11+
### Fixes
12+
- Publishing assets: config and translations
13+
14+
## [1.1.0] - 2024-06-16
15+
16+
### Added
17+
- Support for php 8.1
18+
19+
### Removed
20+
- ide helper library
21+
22+
## [1.0.0] - 2024-06-03
23+
24+
### Added
25+
- Package to return validation error codes in case of the validation error

README.md

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,16 @@ This package enhances Laravel's validation error responses (status 422) by addin
2323
}
2424
}
2525
```
26+
Error codes allow clients of your API to easily interpret returned validation errors in the way they want or need.
2627

2728
## Installation
28-
First install the package using Composer:
29-
```shell
30-
composer require kdabrow/validation-codes
31-
```
32-
33-
Afterward, extend your `Exception\Handler` file with `Kdabrow\ValidationCodes\Handler`.
34-
```php
35-
<?php
3629

37-
namespace App\Exceptions;
30+
First, install the package using Composer:
3831

39-
class Handler extends \Kdabrow\ValidationCodes\Handler
40-
{
41-
42-
}
43-
```
32+
| PHP | Laravel | Package | Command |
33+
|------|---------|-------------------------------------------------------------------|----------------------------------------------------------|
34+
| ^8.2 | 11 | 2.0 | ```composer require "kdabrow/validation-codes: ^2.0"``` |
35+
| ^8.1 | 10 | [1.1](https://github.com/karoldabro/validation-codes/tree/v1.1.0) | ```composer require "kdabrow/validation-codes: ^1.1" ``` |
4436

4537
## How It Works
4638

@@ -62,7 +54,7 @@ You can then change the validation codes corresponding to the given rules in the
6254
<?php
6355

6456
return [
65-
'fallback_error' => 'E0', // This error code is returned while error code isn't found in this file
57+
'fallback_error' => 'E0', // This error code is returned when an error code isn't found in this file
6658
'accepted' => 'E1',
6759
'accepted_if' => 'E2',
6860
'active_url' => 'E3',
@@ -103,7 +95,7 @@ class YourCustomValidationRule implements ValidationRule
10395
{
10496
// validation logic
10597
}
106-
98+
10799
public static function getCode(): string
108100
{
109101
return 'E10000'; // The validation code to return

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kdabrow/validation-codes",
3-
"description": "Validation codes",
3+
"description": "Validation codes for API",
44
"type": "package",
55
"keywords": [
66
"php",
@@ -17,12 +17,12 @@
1717
}
1818
],
1919
"require": {
20-
"php": ">=8.1.0",
21-
"illuminate/support": "^10.0",
22-
"illuminate/validation": "^10.0"
20+
"php": ">=8.2.0",
21+
"illuminate/support": "^11.0",
22+
"illuminate/validation": "^11.0"
2323
},
2424
"require-dev": {
25-
"orchestra/testbench": "^8.0"
25+
"orchestra/testbench": "^9.0"
2626
},
2727
"autoload": {
2828
"psr-4": {

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
php:
3-
image: webdevops/php-dev:8.1
3+
image: webdevops/php-dev:8.2
44
volumes:
55
- ./:/app/
66
working_dir: /app

src/Providers/ValidationCodesProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Kdabrow\ValidationCodes\Providers;
44

55
use Illuminate\Container\Container;
6+
use Illuminate\Contracts\Debug\ExceptionHandler;
67
use Illuminate\Support\ServiceProvider;
78
use Kdabrow\ValidationCodes\Factory;
9+
use Kdabrow\ValidationCodes\Handler;
810
use Kdabrow\ValidationCodes\Validator;
911

1012
class ValidationCodesProvider extends ServiceProvider
@@ -26,6 +28,8 @@ public function register()
2628

2729
return $factory;
2830
});
31+
32+
$this->app->singleton(ExceptionHandler::class, Handler::class);
2933
}
3034

3135
public function boot()

src/Validator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ protected function validateUsingCustomRule($attribute, $value, $rule)
110110

111111
$this->codes->add(
112112
$attribute,
113-
method_exists($ruleClass, 'getCode') ? $ruleClass::getCode($attribute) : $this->fallbackTranslation(), // TODO: change E0 to translation
114-
); // TODO: test
113+
method_exists($ruleClass, 'getCode') ? $ruleClass::getCode($attribute) : $this->fallbackTranslation(),
114+
);
115115
}
116116
}
117117
}
@@ -122,7 +122,7 @@ protected function findErrorCode($attribute, string $rule): string
122122

123123
$key = $this->getKey($lowerRule);
124124

125-
if ($key !== ($this->getTranslation($lowerRule))) { // TODO: test
125+
if ($key !== ($this->getTranslation($lowerRule))) {
126126
return $this->getCustomMessageFromTranslator(
127127
in_array($rule, $this->sizeRules)
128128
? [$key.".{$this->getAttributeType($attribute)}", $key]
@@ -132,7 +132,7 @@ protected function findErrorCode($attribute, string $rule): string
132132

133133
return $this->getFromLocalArray(
134134
$attribute, $lowerRule, $this->fallbackCodes
135-
) ?: $this->fallbackTranslation(); // TODO: test
135+
) ?: $this->fallbackTranslation();
136136
}
137137

138138
/**

tests/ApiTest.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
namespace Kdabrow\ValidationCodes\Tests;
44

55
use Illuminate\Testing\Fluent\AssertableJson;
6+
use PHPUnit\Framework\Attributes\Test;
67

78
class ApiTest extends TestCase
89
{
9-
/** @test */
10+
#[Test]
1011
public function response_returns_status_422_validation_error()
1112
{
1213
$response = $this->json('POST', '/test');
1314

1415
$response->assertStatus(422);
1516
}
1617

17-
/** @test */
18+
#[Test]
1819
public function error_response_contains_validation_errors_key()
1920
{
2021
$response = $this->json('POST', '/test');
@@ -29,7 +30,7 @@ public function error_response_contains_validation_errors_key()
2930
]);
3031
}
3132

32-
/** @test */
33+
#[Test]
3334
public function error_response_contains_validation_codes_key()
3435
{
3536
$response = $this->json('POST', '/test');
@@ -39,7 +40,7 @@ public function error_response_contains_validation_codes_key()
3940
});
4041
}
4142

42-
/** @test */
43+
#[Test]
4344
public function error_response_codes_array_contains_field_with_validation_error_code()
4445
{
4546
$response = $this->json('POST', '/test');
@@ -53,7 +54,7 @@ public function error_response_codes_array_contains_field_with_validation_error_
5354
]);
5455
}
5556

56-
/** @test */
57+
#[Test]
5758
public function response_contains_only_codes_while_configuration_show_only_codes_is_true()
5859
{
5960
config()->set('validation_codes.show_only_codes', true);
@@ -69,7 +70,7 @@ public function response_contains_only_codes_while_configuration_show_only_codes
6970
]);
7071
}
7172

72-
/** @test */
73+
#[Test]
7374
public function response_contains_multiple_codes()
7475
{
7576
$response = $this->json('POST', '/test_with_multiple_errors', ["field_1" => "test"]);
@@ -83,7 +84,7 @@ public function response_contains_multiple_codes()
8384
]);
8485
}
8586

86-
/** @test */
87+
#[Test]
8788
public function response_contains_multiple_fields()
8889
{
8990
$response = $this->json('POST', '/test_with_multiple_fields', ["field_1" => "test"]);
@@ -100,7 +101,7 @@ public function response_contains_multiple_fields()
100101
]);
101102
}
102103

103-
/** @test */
104+
#[Test]
104105
public function response_contains_array_fields()
105106
{
106107
$response = $this->json('POST', '/test_with_array_fields', ["field_1" => [[]]]);
@@ -117,7 +118,7 @@ public function response_contains_array_fields()
117118
]);
118119
}
119120

120-
/** @test */
121+
#[Test]
121122
public function response_contains_errors_from_custom_validation_rules()
122123
{
123124
$response = $this->json('POST', '/test_with_custom_validation_rules', ["field_1" => 1]);
@@ -131,7 +132,7 @@ public function response_contains_errors_from_custom_validation_rules()
131132
]);
132133
}
133134

134-
/** @test */
135+
#[Test]
135136
public function response_contains_fallback_error_when_custom_rules_do_not_contain_the_code()
136137
{
137138
$response = $this->json('POST', '/test_with_custom_validation_rules_without_code', ["field_1" => 1]);
@@ -145,7 +146,7 @@ public function response_contains_fallback_error_when_custom_rules_do_not_contai
145146
]);
146147
}
147148

148-
/** @test */
149+
#[Test]
149150
public function response_returns_fallback_error_when_validation_rule_translation_do_not_exists()
150151
{
151152
$response = $this->json('POST', '/test_with_not_existing_validation_code', ["field_1" => 1]);
@@ -159,7 +160,7 @@ public function response_returns_fallback_error_when_validation_rule_translation
159160
]);
160161
}
161162

162-
/** @test */
163+
#[Test]
163164
public function response_returns_the_code_when_validation_rule_translation_extends_validator()
164165
{
165166
$response = $this->json('POST', '/test_with_existing_validation_code', ["field_1" => 1]);

tests/FactoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace Kdabrow\ValidationCodes\Tests;
44

55
use Kdabrow\ValidationCodes\Factory;
6+
use PHPUnit\Framework\Attributes\Test;
67

78
class FactoryTest extends TestCase
89
{
9-
/** @test */
10+
#[Test]
1011
public function app_resolved_instance_of_the_own_factory()
1112
{
1213
$this->assertInstanceOf(

tests/HandlerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
use Illuminate\Contracts\Debug\ExceptionHandler;
66
use Kdabrow\ValidationCodes\Handler;
7+
use PHPUnit\Framework\Attributes\Test;
78

89
class HandlerTest extends TestCase
910
{
10-
/** @test */
11+
#[Test]
1112
public function app_resolves_instance_of_our_handler()
1213
{
1314
$this->assertInstanceOf(

0 commit comments

Comments
 (0)