|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Phpactor\Container\Tests\Unit; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Generator; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Phpactor\Container\Parameter; |
| 9 | +use RuntimeException; |
| 10 | + |
| 11 | +class ParameterTest extends TestCase |
| 12 | +{ |
| 13 | + public function testCast(): void |
| 14 | + { |
| 15 | + self::assertSame(12, (new Parameter(12))->value()); |
| 16 | + self::assertSame(12, (new Parameter(12))->int()); |
| 17 | + self::assertSame('12', (new Parameter('12'))->string()); |
| 18 | + self::assertSame(true, (new Parameter(true))->bool()); |
| 19 | + self::assertSame(12.2, (new Parameter(12.2))->float()); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @dataProvider provideCastException |
| 24 | + * @param Closure(Parameter): mixed $invoker |
| 25 | + */ |
| 26 | + public function testCastException(mixed $value, Closure $invoker, string $message): void |
| 27 | + { |
| 28 | + $this->expectException(RuntimeException::class); |
| 29 | + $this->expectExceptionMessage($message); |
| 30 | + $invoker( |
| 31 | + new Parameter($value) |
| 32 | + ); |
| 33 | + } |
| 34 | + /** |
| 35 | + * @return Generator<string,array{mixed,Closure(Parameter): mixed,string}> |
| 36 | + */ |
| 37 | + public function provideCastException(): Generator |
| 38 | + { |
| 39 | + yield 'not an int' => [ |
| 40 | + '12', |
| 41 | + fn (Parameter $p) => $p->int(), |
| 42 | + 'Value is not an int, it is "string"' |
| 43 | + ]; |
| 44 | + yield 'not an string' => [ |
| 45 | + 12, |
| 46 | + fn (Parameter $p) => $p->string(), |
| 47 | + 'Value is not a string, it is "integer"' |
| 48 | + ]; |
| 49 | + yield 'not a float' => [ |
| 50 | + '12', |
| 51 | + fn (Parameter $p) => $p->float(), |
| 52 | + 'Value is not a float, it is "string"' |
| 53 | + ]; |
| 54 | + yield 'not a bool' => [ |
| 55 | + '12', |
| 56 | + fn (Parameter $p) => $p->bool(), |
| 57 | + 'Value is not a bool, it is "string"' |
| 58 | + ]; |
| 59 | + } |
| 60 | +} |
0 commit comments