1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
<?php
namespace Illuminate\Tests\Support;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use function Illuminate\Support\enum_value;
include_once 'Enums.php';
class SupportEnumValueFunctionTest extends TestCase
{
#[DataProvider('scalarDataProvider')]
public function test_it_can_handle_enum_value($given, $expected)
{
$this->assertSame($expected, enum_value($given));
}
public function test_it_can_fallback_to_use_default_if_value_is_null()
{
$this->assertSame('laravel', enum_value(null, 'laravel'));
$this->assertSame('laravel', enum_value(null, fn () => 'laravel'));
}
public static function scalarDataProvider()
{
yield [TestEnum::A, 'A'];
yield [TestBackedEnum::A, 1];
yield [TestBackedEnum::B, 2];
yield [TestStringBackedEnum::A, 'A'];
yield [TestStringBackedEnum::B, 'B'];
yield [null, null];
yield [0, 0];
yield ['0', '0'];
yield [false, false];
yield [1, 1];
yield ['1', '1'];
yield [true, true];
yield [[], []];
yield ['', ''];
yield ['laravel', 'laravel'];
yield [true, true];
yield [1337, 1337];
yield [1.0, 1.0];
yield [$collect = collect(), $collect];
}
}
|