Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add enum support #33

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/HasParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TiMacDonald\Middleware;

use BackedEnum;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use ReflectionMethod;
Expand Down Expand Up @@ -169,6 +170,10 @@ private static function castToString($value): string
return '0';
}

if ($value instanceof BackedEnum) {
return $value->value;
timacdonald marked this conversation as resolved.
Show resolved Hide resolved
}

return (string) $value;
}

Expand Down
62 changes: 62 additions & 0 deletions tests/HasParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,30 @@ public function testList(): void
$result = Basic::in(['laravel']);
$this->assertSame('Tests\\Middleware\\Basic:laravel', $result);

$result = Basic::in([Framework::Laravel]);
$this->assertSame('Tests\\Middleware\\Basic:laravel', $result);

$result = Basic::in(['laravel', 'vue']);
$this->assertSame('Tests\\Middleware\\Basic:laravel,vue', $result);

$result = Basic::in([Framework::Laravel, Framework::Vue]);
$this->assertSame('Tests\\Middleware\\Basic:laravel,vue', $result);

$result = Basic::in(['laravel', ' ', null, 'tailwind']);
$this->assertSame('Tests\\Middleware\\Basic:laravel, ,,tailwind', $result);

$result = Basic::in(new Collection(['laravel', 'vue']));
$this->assertSame('Tests\\Middleware\\Basic:laravel,vue', $result);

$result = Basic::in(new Collection([Framework::Laravel, Framework::Vue]));
$this->assertSame('Tests\\Middleware\\Basic:laravel,vue', $result);

$result = Basic::in([new Collection(['laravel', 'vue'])]);
$this->assertSame('Tests\\Middleware\\Basic:["laravel","vue"]', $result);

$result = Basic::in([new Collection([Framework::Laravel, Framework::Vue])]);
$this->assertSame('Tests\\Middleware\\Basic:["laravel","vue"]', $result);

$result = Basic::in([true, false]);
$this->assertSame('Tests\\Middleware\\Basic:1,0', $result);

Expand All @@ -67,11 +79,17 @@ public function testList(): void
$result = Variadic::in(['laravel', 'vue']);
$this->assertSame('Tests\\Middleware\\Variadic:laravel,vue', $result);

$result = Variadic::in([Framework::Laravel, Framework::Vue]);
$this->assertSame('Tests\\Middleware\\Variadic:laravel,vue', $result);

$result = RequiredOptionalVariadic::in(['laravel']);
$this->assertSame('Tests\\Middleware\\RequiredOptionalVariadic:laravel', $result);

$result = RequiredOptionalVariadic::in(['laravel', 'vue', 'tailwind', 'react']);
$this->assertSame('Tests\\Middleware\\RequiredOptionalVariadic:laravel,vue,tailwind,react', $result);

$result = RequiredOptionalVariadic::in([Framework::Laravel, Framework::Vue, Framework::Tailwind, Framework::React]);
$this->assertSame('Tests\\Middleware\\RequiredOptionalVariadic:laravel,vue,tailwind,react', $result);
}

public function testListDoesNotAcceptSubArray(): void
Expand Down Expand Up @@ -130,15 +148,24 @@ public function testMap(): void
$result = Required::with(['required' => 'laravel']);
$this->assertSame('Tests\\Middleware\\Required:laravel', $result);

$result = Required::with(['required' => Framework::Laravel]);
$this->assertSame('Tests\\Middleware\\Required:laravel', $result);

$result = Required::with(['required' => 1.2]);
$this->assertSame('Tests\\Middleware\\Required:1.2', $result);

$result = Required::with(new Collection(['required' => 'laravel']));
$this->assertSame('Tests\\Middleware\\Required:laravel', $result);

$result = Required::with(new Collection(['required' => Framework::Laravel]));
$this->assertSame('Tests\\Middleware\\Required:laravel', $result);

$result = Required::with(['required' => new Collection(['laravel', 'vue'])]);
$this->assertSame('Tests\\Middleware\\Required:["laravel","vue"]', $result);

$result = Required::with(['required' => new Collection([Framework::Laravel, Framework::Vue])]);
$this->assertSame('Tests\\Middleware\\Required:["laravel","vue"]', $result);

$result = Optional::with([]);
$this->assertSame('Tests\\Middleware\\Optional:default', $result);

Expand All @@ -157,12 +184,21 @@ public function testMap(): void
$result = Optional::with(['optional' => 'laravel']);
$this->assertSame('Tests\\Middleware\\Optional:laravel', $result);

$result = Optional::with(['optional' => Framework::Laravel]);
$this->assertSame('Tests\\Middleware\\Optional:laravel', $result);

$result = Optional::with(new Collection(['optional' => 'laravel']));
$this->assertSame('Tests\\Middleware\\Optional:laravel', $result);

$result = Optional::with(new Collection(['optional' => Framework::Laravel]));
$this->assertSame('Tests\\Middleware\\Optional:laravel', $result);

$result = Optional::with(['optional' => new Collection(['laravel', 'vue'])]);
$this->assertSame('Tests\\Middleware\\Optional:["laravel","vue"]', $result);

$result = Optional::with(['optional' => new Collection([Framework::Laravel, Framework::Vue])]);
$this->assertSame('Tests\\Middleware\\Optional:["laravel","vue"]', $result);

$result = Optional::with(['optional' => true]);
$this->assertSame('Tests\\Middleware\\Optional:1', $result);

Expand All @@ -181,18 +217,30 @@ public function testMap(): void
$result = Variadic::with(['variadic' => 'laravel']);
$this->assertSame('Tests\\Middleware\\Variadic:laravel', $result);

$result = Variadic::with(['variadic' => Framework::Laravel]);
$this->assertSame('Tests\\Middleware\\Variadic:laravel', $result);

$result = Variadic::with(['variadic' => ['laravel', 'vue']]);
$this->assertSame('Tests\\Middleware\\Variadic:laravel,vue', $result);

$result = Variadic::with(['variadic' => [Framework::Laravel, Framework::Vue]]);
$this->assertSame('Tests\\Middleware\\Variadic:laravel,vue', $result);

$result = Variadic::with(['variadic' => ['laravel', ' ', null, 'vue']]);
$this->assertSame('Tests\\Middleware\\Variadic:laravel, ,,vue', $result);

$result = Variadic::with(['variadic' => new Collection(['laravel', 'vue'])]);
$this->assertSame('Tests\\Middleware\\Variadic:laravel,vue', $result);

$result = Variadic::with(['variadic' => new Collection([Framework::Laravel, Framework::Vue])]);
$this->assertSame('Tests\\Middleware\\Variadic:laravel,vue', $result);

$result = Variadic::with(['variadic' => [new Collection(['laravel', 'vue'])]]);
$this->assertSame('Tests\\Middleware\\Variadic:["laravel","vue"]', $result);

$result = Variadic::with(['variadic' => [new Collection([Framework::Laravel, Framework::Vue])]]);
$this->assertSame('Tests\\Middleware\\Variadic:["laravel","vue"]', $result);

$result = Variadic::with(['variadic' => true]);
$this->assertSame('Tests\\Middleware\\Variadic:1', $result);

Expand All @@ -211,6 +259,9 @@ public function testMap(): void
$result = RequiredOptionalVariadic::with(['required' => 'laravel']);
$this->assertSame('Tests\\Middleware\\RequiredOptionalVariadic:laravel,default', $result);

$result = RequiredOptionalVariadic::with(['required' => Framework::Laravel]);
$this->assertSame('Tests\\Middleware\\RequiredOptionalVariadic:laravel,default', $result);

$result = RequiredOptionalVariadic::with(['required' => 'laravel', 'optional' => 'vue']);
$this->assertSame('Tests\\Middleware\\RequiredOptionalVariadic:laravel,vue', $result);

Expand All @@ -220,6 +271,9 @@ public function testMap(): void
$result = RequiredOptionalVariadic::with(['required' => 'laravel', 'optional' => 'vue', 'variadic' => ['tailwind', 'react']]);
$this->assertSame('Tests\\Middleware\\RequiredOptionalVariadic:laravel,vue,tailwind,react', $result);

$result = RequiredOptionalVariadic::with(['required' => Framework::Laravel, 'optional' => Framework::Vue, 'variadic' => [Framework::Tailwind, Framework::React]]);
$this->assertSame('Tests\\Middleware\\RequiredOptionalVariadic:laravel,vue,tailwind,react', $result);

$result = RequiredOptionalVariadic::with(['required' => 'laravel', 'optional' => 'vue', 'variadic' => []]);
$this->assertSame('Tests\\Middleware\\RequiredOptionalVariadic:laravel,vue', $result);
}
Expand Down Expand Up @@ -363,3 +417,11 @@ public function testPassingOriginalAndAliasThrows(): void
]);
}
}

enum Framework: string
{
case Laravel = 'laravel';
case Vue = 'vue';
case Tailwind = 'tailwind';
case React = 'react';
}