Skip to content

Commit

Permalink
Drop PHP 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Jan 18, 2025
1 parent c5a3d95 commit d5e61eb
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 49 deletions.
6 changes: 2 additions & 4 deletions src/Attributes/GenericParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ protected function getEnumValues(): array
return $this->enum;
}

if (function_exists('enum_exists') && enum_exists($this->enum)
&& method_exists($this->enum, 'tryFrom')
) {
if (enum_exists($this->enum) && method_exists($this->enum, 'tryFrom')) {
return array_map(
// $case->value only exists on BackedEnums, not UnitEnums
// method_exists($enum, 'tryFrom') implies $enum instanceof BackedEnum
// method_exists($enum, 'tryFrom') implies the enum is a BackedEnum
// @phpstan-ignore-next-line
fn ($case) => $case->value,
$this->enum::cases()
Expand Down
4 changes: 2 additions & 2 deletions src/Extracting/ParsesValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ protected function parseRule($rule, array &$parameterData, bool $independentOnly
return true;
}

if (function_exists('enum_exists') && $rule instanceof \Illuminate\Validation\Rules\Enum) {
if ($rule instanceof \Illuminate\Validation\Rules\Enum) {
$reflection = new \ReflectionClass($rule);
$property = $reflection->getProperty('type');
$property->setAccessible(true);
$type = $property->getValue($rule);

if (enum_exists($type) && method_exists($type, 'tryFrom')) {
// $case->value only exists on BackedEnums, not UnitEnums
// method_exists($enum, 'tryFrom') implies $enum instanceof BackedEnum
// method_exists($enum, 'tryFrom') implies the enum is a BackedEnum
// @phpstan-ignore-next-line
$cases = array_map(fn ($case) => $case->value, $type::cases());
$parameterData['type'] = gettype($cases[0]);
Expand Down
3 changes: 1 addition & 2 deletions src/Extracting/Strategies/GetFromInlineValidatorBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ public function lookForInlineValidationRules(ClassMethod $methodAst): array
}
// Try to extract Enum rule
else if (
function_exists('enum_exists') &&
($enum = $this->extractEnumClassFromArrayItem($arrayItem)) &&
enum_exists($enum) && method_exists($enum, 'tryFrom')
) {
// $case->value only exists on BackedEnums, not UnitEnums
// method_exists($enum, 'tryFrom') implies $enum instanceof BackedEnum
// method_exists($enum, 'tryFrom') implies the enum is a BackedEnum
// @phpstan-ignore-next-line
$rulesList[] = 'in:' . implode(',', array_map(fn ($case) => $case->value, $enum::cases()));
}
Expand Down
5 changes: 0 additions & 5 deletions tests/Fixtures/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,19 +704,14 @@ public function withEnumRule(Request $request)
]);
}

/**
* Can only run on PHP 8.1
public function withInjectedEnumAndModel(Category $category, TestUser $user)
{
return null;
}
*/
}

/**
enum Category: string
{
case Fruits = 'fruits';
case People = 'people';
}
*/
18 changes: 9 additions & 9 deletions tests/Fixtures/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
],
"body": {
"mode": "raw",
"raw": "{\"user_id\":9,\"room_id\":\"consequatur\",\"forever\":false,\"another_one\":11613.31890586,\"yet_another_param\":{\"name\":\"consequatur\"},\"even_more_param\":[11613.31890586],\"book\":{\"name\":\"consequatur\",\"author_id\":17,\"pages_count\":17},\"ids\":[17],\"users\":[{\"first_name\":\"John\",\"last_name\":\"Doe\"}]}"
"raw": "{\"user_id\":9,\"room_id\":\"architecto\",\"forever\":false,\"another_one\":4326.41688,\"yet_another_param\":{\"name\":\"architecto\"},\"even_more_param\":[4326.41688],\"book\":{\"name\":\"architecto\",\"author_id\":16,\"pages_count\":16},\"ids\":[16],\"users\":[{\"first_name\":\"John\",\"last_name\":\"Doe\"}]}"
},
"description": "",
"auth": {
Expand All @@ -146,7 +146,7 @@
"query": [
{
"key": "location_id",
"value": "consequatur",
"value": "architecto",
"description": "The id of the location.",
"disabled": false
},
Expand All @@ -164,7 +164,7 @@
},
{
"key": "filters",
"value": "consequatur",
"value": "architecto",
"description": "The filters.",
"disabled": false
},
Expand All @@ -175,7 +175,7 @@
"disabled": false
}
],
"raw": "{{baseUrl}}/api/withQueryParameters?location_id=consequatur&user_id=me&page=4&filters=consequatur&url_encoded=%2B+%5B%5D%26%3D"
"raw": "{{baseUrl}}/api/withQueryParameters?location_id=architecto&user_id=me&page=4&filters=architecto&url_encoded=%2B+%5B%5D%26%3D"
},
"method": "GET",
"header": [
Expand Down Expand Up @@ -268,12 +268,12 @@
"query": [
{
"key": "something",
"value": "consequatur",
"value": "architecto",
"description": "",
"disabled": false
}
],
"raw": "{{baseUrl}}/api/echoesUrlParameters/:param/:param2/:param3/:param4?something=consequatur",
"raw": "{{baseUrl}}/api/echoesUrlParameters/:param/:param2/:param3/:param4?something=architecto",
"variable": [
{
"id": "param",
Expand All @@ -284,13 +284,13 @@
{
"id": "param2",
"key": "param2",
"value": "consequatur",
"value": "architecto",
"description": ""
},
{
"id": "param3",
"key": "param3",
"value": "consequatur",
"value": "architecto",
"description": ""
},
{
Expand Down Expand Up @@ -331,7 +331,7 @@
}
],
"code": 200,
"body": "{\"param\":\"4\",\"param2\":\"consequatur\",\"param3\":\"consequatur\",\"param4\":null}",
"body": "{\"param\":\"4\",\"param2\":\"architecto\",\"param3\":\"architecto\",\"param4\":null}",
"name": null
}
]
Expand Down
24 changes: 12 additions & 12 deletions tests/Fixtures/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ paths:
in: query
name: location_id
description: 'The id of the location.'
example: consequatur
example: architecto
required: true
schema:
type: string
description: 'The id of the location.'
example: consequatur
example: architecto
nullable: false
-
in: query
Expand Down Expand Up @@ -124,12 +124,12 @@ paths:
in: query
name: filters
description: 'The filters.'
example: consequatur
example: architecto
required: false
schema:
type: string
description: 'The filters.'
example: consequatur
example: architecto
nullable: false
-
in: query
Expand Down Expand Up @@ -193,12 +193,12 @@ paths:
in: query
name: something
description: ''
example: consequatur
example: architecto
required: false
schema:
type: string
description: ''
example: consequatur
example: architecto
nullable: false
-
in: header
Expand All @@ -216,13 +216,13 @@ paths:
type: object
example:
param: '4'
param2: consequatur
param3: consequatur
param2: architecto
param3: architecto
param4: null
properties:
param: { type: string, example: '4' }
param2: { type: string, example: consequatur }
param3: { type: string, example: consequatur }
param2: { type: string, example: architecto }
param3: { type: string, example: architecto }
param4: { type: string, example: null }
tags:
- Other😎
Expand All @@ -243,7 +243,7 @@ paths:
required: true
schema:
type: string
example: consequatur
example: architecto
-
in: path
name: param3
Expand All @@ -257,7 +257,7 @@ paths:
value: ''
present:
summary: 'When the value is present'
value: consequatur
value: architecto
-
in: path
name: param4
Expand Down
12 changes: 12 additions & 0 deletions tests/GenerateDocumentation/OutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ private function generate_with_paths($configName, $intermediateOutputDirectory =
/** @test */
public function generated_postman_collection_file_is_correct()
{
if (phpversion() < 8.3) {
// See https://github.com/FakerPHP/Faker/issues/694
$this->markTestSkipped('Faker seeding changed in PHP 8.3');
return;
}

RouteFacade::post('/api/withBodyParametersAsArray', [TestController::class, 'withBodyParametersAsArray']);
RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
RouteFacade::post('/api/withBodyParameters', [TestController::class, 'withBodyParameters']);
Expand Down Expand Up @@ -205,6 +211,12 @@ public function generated_postman_collection_file_is_correct()
/** @test */
public function generated_openapi_spec_file_is_correct()
{
if (phpversion() < 8.3) {
// See https://github.com/FakerPHP/Faker/issues/694
$this->markTestSkipped('Faker seeding changed in PHP 8.3');
return;
}

RouteFacade::post('/api/withBodyParametersAsArray', [TestController::class, 'withBodyParametersAsArray']);
RouteFacade::post('/api/withFormDataParams', [TestController::class, 'withFormDataParams']);
RouteFacade::get('/api/withResponseTag', [TestController::class, 'withResponseTag']);
Expand Down
4 changes: 0 additions & 4 deletions tests/Strategies/GetFromInlineValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,6 @@ public function respects_query_params_comment()
/** @test */
public function can_fetch_inline_enum_rules()
{
if (phpversion() < 8.1) {
$this->markTestSkipped('Enums are only supported in PHP 8.1 or later');
}

$endpoint = $this->endpoint(function (ExtractedEndpointData $e) {
$e->method = new \ReflectionMethod(TestController::class, 'withEnumRule');
});
Expand Down
9 changes: 4 additions & 5 deletions tests/Strategies/UrlParameters/GetFromLaravelAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,23 @@ class GetFromLaravelAPITest extends BaseLaravelTest
/** @test */
public function can_infer_type_from_model_binding()
{
$endpoint = $this->endpointForRoute("users/{id}", TestController::class, 'withInjectedModel');
// Can only run on PHP 8.1
// $endpoint = $this->endpointForRoute("categories/{category}/users/{id}/", TestController::class, 'withInjectedEnumAndModel');
// $endpoint = $this->endpointForRoute("users/{id}", TestController::class, 'withInjectedModel');
$endpoint = $this->endpointForRoute("categories/{category}/users/{id}/", TestController::class, 'withInjectedEnumAndModel');
$results = $this->fetch($endpoint);

$this->assertArraySubset([
"name" => "id",
"description" => "The ID of the user.",
"required" => true,
"type" => "integer",
], $results['id']);/*
], $results['id']);
$this->assertArraySubset([
"name" => "category",
"description" => "The category.",
"required" => true,
"type" => "string",
"example" => \Knuckles\Scribe\Tests\Fixtures\Category::cases()[0]->value,
], $results['category']);*/
], $results['category']);
$this->assertIsInt($results['id']['example']);
}

Expand Down
8 changes: 2 additions & 6 deletions tests/Unit/ValidationRuleParsingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function can_parse_supported_rules(array $ruleset, array $customInfo, arr
Schema::create('users', function ($table) {
$table->id();
});

$results = $this->strategy->parse($ruleset, $customInfo);

$parameterName = array_keys($ruleset)[0];
Expand All @@ -57,7 +57,7 @@ public function can_parse_supported_rules(array $ruleset, array $customInfo, arr

// Validate that the generated values actually pass validation (for rules where we can generate some data)
if (is_string($ruleset[$parameterName]) && str_contains($ruleset[$parameterName], "exists")) return;

$exampleData = [$parameterName => $results[$parameterName]['example']];
$validator = Validator::make($exampleData, $ruleset);
try {
Expand Down Expand Up @@ -552,10 +552,6 @@ public function can_parse_custom_rule_classes()
/** @test */
public function can_parse_enum_rules()
{
if (phpversion() < 8.1) {
$this->markTestSkipped('Enums are only supported in PHP 8.1 or later');
}

$results = $this->strategy->parse([
'enum' => [
'required',
Expand Down

0 comments on commit d5e61eb

Please sign in to comment.