Skip to content

Commit

Permalink
Add enum support (#33)
Browse files Browse the repository at this point in the history
* feat: add enum support

* test: add enum assertions

* Update src/HasParameters.php

---------

Co-authored-by: Tim MacDonald <[email protected]>
  • Loading branch information
bpotmalnik and timacdonald authored May 15, 2024
1 parent 6ee8d3b commit e311f34
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
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 (string) $value->value;
}

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';
}

0 comments on commit e311f34

Please sign in to comment.