Bumped for php 8.2 + fix unit test and phpcs

This commit is contained in:
Jérémy Munsch 2023-06-02 12:12:37 +02:00
parent 8b25893b99
commit 606c380e97
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC
11 changed files with 8275 additions and 60 deletions

View file

@ -2,6 +2,8 @@
All notable changes to `Enum` will be documented in this file.
## **Version 3.0.0**
- Changed use of php8.2 and more only For laravel 10 usage
## **Version 2.1.3**

View file

@ -16,14 +16,15 @@
"Enum"
],
"require": {
"php": ">=7.3"
"php": ">=8.2"
},
"require-dev": {
"phpunit/phpunit": "~9",
"phpunit/phpunit": "^9",
"squizlabs/php_codesniffer": "^3",
"orchestra/testbench": "^6",
"orchestra/testbench": "^8",
"slevomat/coding-standard": "^7",
"phpunit/php-code-coverage": "^9"
"phpunit/php-code-coverage": "^9",
"nunomaduro/collision": "^6.4"
},
"autoload": {
"psr-4": {
@ -46,7 +47,7 @@
"post-autoload-dump": [
"@php ./vendor/bin/testbench package:discover --ansi"
],
"test": "vendor/bin/phpunit"
"test": "vendor/bin/testbench package:test"
},
"config": {
"allow-plugins": {

8200
composer.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage cacheDirectory=".phpunit.cache/code-coverage" processUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
@ -20,6 +32,7 @@
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>

View file

@ -338,7 +338,7 @@ abstract class BaseEnum implements \JsonSerializable
* @return void
* @throws \TypeError If anything else than string or int is used.
*/
private static function assertValidValue($value): void
private static function assertValidValue(mixed $value): void
{
if (!(is_string($value) || is_int($value))) {
$enumClass = static::class;

View file

@ -51,7 +51,7 @@ abstract class BaseEnumRoutable extends BaseEnum implements UrlRoutable
* @return \Kwaadpepper\Enum\BaseEnum|null
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed
*/
public function resolveRouteBinding($value, $field = null)
public function resolveRouteBinding(mixed $value, $field = null)
{
// phpcs:enable
// Try to cast numeric value first.
@ -83,6 +83,7 @@ abstract class BaseEnumRoutable extends BaseEnum implements UrlRoutable
* @throws \Kwaadpepper\Enum\Exceptions\NotImplementedException ResolveChildRouteBinding not implemented,
* should not be used.
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
*/
public function resolveChildRouteBinding($childType, $value, $field)
{

View file

@ -34,6 +34,7 @@ abstract class BaseEnumRule implements Rule
* @param mixed $value
* @return boolean
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
*/
abstract public function passes($attribute, $value): bool;
// phpcs:enable

View file

@ -16,6 +16,7 @@ class EnumIsValidRule extends BaseEnumRule
* @param string $attribute
* @param mixed $value
* @return boolean
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
*/

View file

@ -40,9 +40,12 @@ trait CastsEnums
* @return $this
* @throws \TypeError If cast fails.
* @throws \BadMethodCallException If cast fails.
* @phpcs:disable Squiz.Commenting.FunctionComment.ScalarTypeHintMissing
* @phpcs:disable Squiz.Commenting.FunctionComment.TypeHintMissing
*/
public function setAttribute(string $key, $value)
public function setAttribute($key, $value)
{
// phpcs:enable
try {
if ($enum = $this->castToEnum($key, $value) and $enum instanceof BaseEnum) {
$this->attributes[$key] = $enum->value;
@ -88,7 +91,7 @@ trait CastsEnums
* @throws TypeError — If anything else than string or int is used.
* @throws BadMethodCallException — If a matching definition cannot be found.
*/
protected function castToEnum(string $key, $value)
protected function castToEnum(string $key, mixed $value)
{
if (
$value === null or

View file

@ -19,6 +19,33 @@ use Orchestra\Testbench\TestCase;
class BaseEnumRoutableTest extends TestCase
{
/**
* Setup custom phpunit catch for deprecated error cathing.
*
* @return void
*/
public function setUp(): void
{
set_error_handler(
static function ($errno, $errstr) {
throw new \Exception($errstr, $errno);
},
E_ALL
);
parent::setUp();
}
/**
* Restore phpunit handler
*
* @return void
*/
public function tearDown(): void
{
restore_error_handler();
parent::tearDown();
}
/**
* Defines aliases
*
@ -51,7 +78,8 @@ class BaseEnumRoutableTest extends TestCase
{
// phpcs:enable
return [
'Kwaadpepper\Enum\EnumServiceProvider',
\Illuminate\Log\LogServiceProvider::class,
\Kwaadpepper\Enum\EnumServiceProvider::class,
];
}
@ -123,29 +151,28 @@ class BaseEnumRoutableTest extends TestCase
// phpcs:enable
$router->get('/journals/{journal}', function (Journal $journal) {
return response()->json([$journal->day]);
})->middleware('bindings')->name('journal.view');
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class)->name('journal.view');
$router->get('/reports/{report}', function (Report $report) {
return response()->json([$report->day]);
})->middleware('bindings');
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);
$router->get('/alarms/{alarm}', function (Alarm $alarm) {
return response()->json([$alarm->day]);
})->middleware('bindings');
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);
$router->get('/days/{day}', function (Days $day) {
return response()->json([$day]);
})->middleware('bindings');
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);
$router->get('/day-validation', function (PassDayRequest $request) {
return response()->json([Days::make((int)$request->day)]);
})->middleware('bindings');
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);
$router->get('/civilities/{civility}', function (ContactFormCivility $civility) {
return response()->json([$civility]);
})->middleware('bindings');
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);
$router->get('/force/{force}', function (ForceStringsFromInteger $force) {
return response()->json([$force]);
})->middleware('bindings');
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);
$router->get('/notroutable/{notroutable}', function (NotRoutableEnum $notroutable) {
dd($notroutable);
return response()->json([$notroutable]);
})->middleware('bindings');
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);
}
/**

View file

@ -2,8 +2,6 @@
namespace Kwaadpepper\Enum\Tests;
use BadMethodCallException;
use Exception;
use Kwaadpepper\Enum\Exceptions\DuplicateLabelsException;
use Kwaadpepper\Enum\Exceptions\DuplicateValuesException;
use Kwaadpepper\Enum\Exceptions\UnknownEnumProperty;
@ -13,9 +11,6 @@ use Kwaadpepper\Enum\Tests\Enums\DuplicatedValues;
use Kwaadpepper\Enum\Tests\Enums\ForceStringsFromInteger;
use Kwaadpepper\Enum\Tests\Models\Report;
use Orchestra\Testbench\TestCase;
use ReflectionClass;
use stdClass;
use TypeError;
class BaseEnumTest extends TestCase
{
@ -43,7 +38,7 @@ class BaseEnumTest extends TestCase
$exception = null;
try {
$mon->propthatdontexists;
} catch (Exception $e) {
} catch (\Exception $e) {
$exception = $e;
} finally {
$this->assertEquals(
@ -104,11 +99,9 @@ class BaseEnumTest extends TestCase
*/
public function testCannotAssignAnEnumValue()
{
$this->setUp();
$this->expectException(\Error::class);
$enum = Days::mon();
$enum->value = 'anything';
$this->tearDown();
}
/**
@ -118,11 +111,9 @@ class BaseEnumTest extends TestCase
*/
public function testCannotAssignAnEnumLabel()
{
$this->setUp();
$this->expectException(\Error::class);
$enum = Days::mon();
$enum->label = 'anything';
$this->tearDown();
}
/**
@ -133,7 +124,7 @@ class BaseEnumTest extends TestCase
public function testHackingEnumValueCouldConfuseCache()
{
$enum = Days::mon();
$refClass = new ReflectionClass($enum);
$refClass = new \ReflectionClass($enum);
$property = $refClass->getProperty('value');
$property->setAccessible(true);
$property->setValue($enum, 'anything');
@ -164,7 +155,7 @@ class BaseEnumTest extends TestCase
*/
public function testInvalidEnumSet()
{
$this->expectException(BadMethodCallException::class);
$this->expectException(\BadMethodCallException::class);
$report = new Report();
$report->day = Days::mon()->label;
}
@ -176,7 +167,7 @@ class BaseEnumTest extends TestCase
*/
public function testInvalidEnumSetWithFloat()
{
$this->expectException(TypeError::class);
$this->expectException(\TypeError::class);
$report = new Report();
$report->day = 0.1;
}
@ -188,9 +179,9 @@ class BaseEnumTest extends TestCase
*/
public function testInvalidEnumSetNotAuthorizedType()
{
$this->expectException(TypeError::class);
$this->expectException(\TypeError::class);
$report = new Report();
$report->day = new stdClass();
$report->day = new \stdClass();
}
/**
@ -226,29 +217,4 @@ class BaseEnumTest extends TestCase
$this->expectException(DuplicateLabelsException::class);
DuplicatedLabels::one();
}
/**
* Setup custom phpunit catch for deprecated error cathing.
*
* @return void
*/
public function setUp(): void
{
set_error_handler(
static function ($errno, $errstr) {
throw new \Exception($errstr, $errno);
},
E_ALL
);
}
/**
* Restore phpunit handler
*
* @return void
*/
public function tearDown(): void
{
restore_error_handler();
}
}