Skip to content

Commit

Permalink
Update to Laravel 11
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol Dąbrowski committed Jun 14, 2024
1 parent 7e76bd2 commit 7369853
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 41 deletions.
18 changes: 4 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,11 @@ This package enhances Laravel's validation error responses (status 422) by addin

## Installation
First install the package using Composer:
```shell
composer require kdabrow/validation-codes
```

Afterward, extend your `Exception\Handler` file with `Kdabrow\ValidationCodes\Handler`.
```php
<?php

namespace App\Exceptions;

class Handler extends \Kdabrow\ValidationCodes\Handler
{

}
```
| PHP | Laravel | Package |
|------|---------|----------------------------------------------------------|
| ^8.2 | 11 | ```composer require "kdabrow/validation-codes: ^2.0"``` |
| ^8.1 | 10 | ```composer require "kdabrow/validation-codes: ^1.0" ``` |

## How It Works

Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
],
"require": {
"php": ">=8.2.0",
"illuminate/support": "^10.0",
"illuminate/validation": "^10.0"
"illuminate/support": "^11.0"
},
"require-dev": {
"orchestra/testbench": "^8.0",
"orchestra/testbench": "^9.0",
"barryvdh/laravel-ide-helper": "^3.0"
},
"autoload": {
Expand Down
4 changes: 4 additions & 0 deletions src/Providers/ValidationCodesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Kdabrow\ValidationCodes\Providers;

use Illuminate\Container\Container;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\ServiceProvider;
use Kdabrow\ValidationCodes\Factory;
use Kdabrow\ValidationCodes\Handler;
use Kdabrow\ValidationCodes\Validator;

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

return $factory;
});

$this->app->singleton(ExceptionHandler::class, Handler::class);
}

public function boot()
Expand Down
8 changes: 4 additions & 4 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ protected function validateUsingCustomRule($attribute, $value, $rule)

$this->codes->add(
$attribute,
method_exists($ruleClass, 'getCode') ? $ruleClass::getCode($attribute) : $this->fallbackTranslation(), // TODO: change E0 to translation
); // TODO: test
method_exists($ruleClass, 'getCode') ? $ruleClass::getCode($attribute) : $this->fallbackTranslation(),
);
}
}
}
Expand All @@ -122,7 +122,7 @@ protected function findErrorCode($attribute, string $rule): string

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

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

return $this->getFromLocalArray(
$attribute, $lowerRule, $this->fallbackCodes
) ?: $this->fallbackTranslation(); // TODO: test
) ?: $this->fallbackTranslation();
}

/**
Expand Down
25 changes: 13 additions & 12 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
namespace Kdabrow\ValidationCodes\Tests;

use Illuminate\Testing\Fluent\AssertableJson;
use PHPUnit\Framework\Attributes\Test;

class ApiTest extends TestCase
{
/** @test */
#[Test]
public function response_returns_status_422_validation_error()
{
$response = $this->json('POST', '/test');

$response->assertStatus(422);
}

/** @test */
#[Test]
public function error_response_contains_validation_errors_key()
{
$response = $this->json('POST', '/test');
Expand All @@ -29,7 +30,7 @@ public function error_response_contains_validation_errors_key()
]);
}

/** @test */
#[Test]
public function error_response_contains_validation_codes_key()
{
$response = $this->json('POST', '/test');
Expand All @@ -39,7 +40,7 @@ public function error_response_contains_validation_codes_key()
});
}

/** @test */
#[Test]
public function error_response_codes_array_contains_field_with_validation_error_code()
{
$response = $this->json('POST', '/test');
Expand All @@ -53,7 +54,7 @@ public function error_response_codes_array_contains_field_with_validation_error_
]);
}

/** @test */
#[Test]
public function response_contains_only_codes_while_configuration_show_only_codes_is_true()
{
config()->set('validation_codes.show_only_codes', true);
Expand All @@ -69,7 +70,7 @@ public function response_contains_only_codes_while_configuration_show_only_codes
]);
}

/** @test */
#[Test]
public function response_contains_multiple_codes()
{
$response = $this->json('POST', '/test_with_multiple_errors', ["field_1" => "test"]);
Expand All @@ -83,7 +84,7 @@ public function response_contains_multiple_codes()
]);
}

/** @test */
#[Test]
public function response_contains_multiple_fields()
{
$response = $this->json('POST', '/test_with_multiple_fields', ["field_1" => "test"]);
Expand All @@ -100,7 +101,7 @@ public function response_contains_multiple_fields()
]);
}

/** @test */
#[Test]
public function response_contains_array_fields()
{
$response = $this->json('POST', '/test_with_array_fields', ["field_1" => [[]]]);
Expand All @@ -117,7 +118,7 @@ public function response_contains_array_fields()
]);
}

/** @test */
#[Test]
public function response_contains_errors_from_custom_validation_rules()
{
$response = $this->json('POST', '/test_with_custom_validation_rules', ["field_1" => 1]);
Expand All @@ -131,7 +132,7 @@ public function response_contains_errors_from_custom_validation_rules()
]);
}

/** @test */
#[Test]
public function response_contains_fallback_error_when_custom_rules_do_not_contain_the_code()
{
$response = $this->json('POST', '/test_with_custom_validation_rules_without_code', ["field_1" => 1]);
Expand All @@ -145,7 +146,7 @@ public function response_contains_fallback_error_when_custom_rules_do_not_contai
]);
}

/** @test */
#[Test]
public function response_returns_fallback_error_when_validation_rule_translation_do_not_exists()
{
$response = $this->json('POST', '/test_with_not_existing_validation_code', ["field_1" => 1]);
Expand All @@ -159,7 +160,7 @@ public function response_returns_fallback_error_when_validation_rule_translation
]);
}

/** @test */
#[Test]
public function response_returns_the_code_when_validation_rule_translation_extends_validator()
{
$response = $this->json('POST', '/test_with_existing_validation_code', ["field_1" => 1]);
Expand Down
3 changes: 2 additions & 1 deletion tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Kdabrow\ValidationCodes\Tests;

use Kdabrow\ValidationCodes\Factory;
use PHPUnit\Framework\Attributes\Test;

class FactoryTest extends TestCase
{
/** @test */
#[Test]
public function app_resolved_instance_of_the_own_factory()
{
$this->assertInstanceOf(
Expand Down
3 changes: 2 additions & 1 deletion tests/HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Illuminate\Contracts\Debug\ExceptionHandler;
use Kdabrow\ValidationCodes\Handler;
use PHPUnit\Framework\Attributes\Test;

class HandlerTest extends TestCase
{
/** @test */
#[Test]
public function app_resolves_instance_of_our_handler()
{
$this->assertInstanceOf(
Expand Down
5 changes: 0 additions & 5 deletions tests/Helpers/ValidationCodesTestProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

class ValidationCodesTestProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(ExceptionHandler::class, Handler::class);
}

public function boot(): void
{
$this->loadRoutesFrom(__DIR__.'/routes.php');
Expand Down
3 changes: 2 additions & 1 deletion tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace Kdabrow\ValidationCodes\Tests;

use Kdabrow\ValidationCodes\Validator;
use PHPUnit\Framework\Attributes\Test;

class ValidatorTest extends TestCase
{
/** @test */
#[Test]
public function app_resolved_instance_of_the_own_validator()
{
$this->assertInstanceOf(
Expand Down

0 comments on commit 7369853

Please sign in to comment.