|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Constructo\Test\Core\Reflect\Resolver\Type; |
| 6 | + |
| 7 | +use Constructo\Core\Reflect\Resolver\Type\BuiltinNamedTypeHandler; |
| 8 | +use Constructo\Core\Reflect\Resolver\Type\Contract\NamedTypeResolution; |
| 9 | +use Constructo\Factory\DefaultSpecsFactory; |
| 10 | +use Constructo\Support\Metadata\Schema\Field; |
| 11 | +use Constructo\Support\Metadata\Schema\Field\Rules; |
| 12 | +use Constructo\Support\Metadata\Schema\Registry\Specs; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | +use ReflectionNamedType; |
| 15 | +use ReflectionParameter; |
| 16 | +use ReflectionUnionType; |
| 17 | + |
| 18 | +final class BuiltinNamedTypeHandlerTest extends TestCase |
| 19 | +{ |
| 20 | + private BuiltinNamedTypeHandler $handler; |
| 21 | + private Specs $specs; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $this->handler = new BuiltinNamedTypeHandler(); |
| 26 | + |
| 27 | + $specsData = [ |
| 28 | + 'string' => [], |
| 29 | + 'integer' => [], |
| 30 | + 'numeric' => [], |
| 31 | + 'boolean' => [], |
| 32 | + 'array' => [], |
| 33 | + ]; |
| 34 | + |
| 35 | + $specsFactory = new DefaultSpecsFactory($specsData); |
| 36 | + $this->specs = $specsFactory->make(); |
| 37 | + } |
| 38 | + |
| 39 | + public function testResolveBuiltinStringType(): void |
| 40 | + { |
| 41 | + $parameter = $this->createParameterWithType('string', true); |
| 42 | + $field = new Field($this->specs, new Rules(), 'test'); |
| 43 | + |
| 44 | + $this->handler->resolve($parameter, $field); |
| 45 | + |
| 46 | + $this->assertTrue($field->hasRule('string')); |
| 47 | + } |
| 48 | + |
| 49 | + public function testResolveBuiltinIntType(): void |
| 50 | + { |
| 51 | + $parameter = $this->createParameterWithType('int', true); |
| 52 | + $field = new Field($this->specs, new Rules(), 'test'); |
| 53 | + |
| 54 | + $this->handler->resolve($parameter, $field); |
| 55 | + |
| 56 | + $this->assertTrue($field->hasRule('integer')); |
| 57 | + } |
| 58 | + |
| 59 | + public function testResolveBuiltinFloatType(): void |
| 60 | + { |
| 61 | + $parameter = $this->createParameterWithType('float', true); |
| 62 | + $field = new Field($this->specs, new Rules(), 'test'); |
| 63 | + |
| 64 | + $this->handler->resolve($parameter, $field); |
| 65 | + |
| 66 | + $this->assertTrue($field->hasRule('numeric')); |
| 67 | + } |
| 68 | + |
| 69 | + public function testResolveBuiltinBoolType(): void |
| 70 | + { |
| 71 | + $parameter = $this->createParameterWithType('bool', true); |
| 72 | + $field = new Field($this->specs, new Rules(), 'test'); |
| 73 | + |
| 74 | + $this->handler->resolve($parameter, $field); |
| 75 | + |
| 76 | + $this->assertTrue($field->hasRule('boolean')); |
| 77 | + } |
| 78 | + |
| 79 | + public function testResolveBuiltinArrayType(): void |
| 80 | + { |
| 81 | + $parameter = $this->createParameterWithType('array', true); |
| 82 | + $field = new Field($this->specs, new Rules(), 'test'); |
| 83 | + |
| 84 | + $this->handler->resolve($parameter, $field); |
| 85 | + |
| 86 | + $this->assertTrue($field->hasRule('array')); |
| 87 | + } |
| 88 | + |
| 89 | + public function testDoesNotResolveNonBuiltinType(): void |
| 90 | + { |
| 91 | + $parameter = $this->createParameterWithType('stdClass', false); |
| 92 | + $field = new Field($this->specs, new Rules(), 'test'); |
| 93 | + |
| 94 | + $this->handler->resolve($parameter, $field); |
| 95 | + |
| 96 | + $this->assertFalse($field->hasRule('string')); |
| 97 | + $this->assertFalse($field->hasRule('array')); |
| 98 | + } |
| 99 | + |
| 100 | + public function testDoesNotResolveUnionType(): void |
| 101 | + { |
| 102 | + $parameter = $this->createParameterWithUnionType(); |
| 103 | + $field = new Field($this->specs, new Rules(), 'test'); |
| 104 | + |
| 105 | + $this->handler->resolve($parameter, $field); |
| 106 | + |
| 107 | + $this->assertFalse($field->hasRule('string')); |
| 108 | + $this->assertFalse($field->hasRule('integer')); |
| 109 | + $this->assertFalse($field->hasRule('array')); |
| 110 | + } |
| 111 | + |
| 112 | + private function createParameterWithType(string $typeName, bool $isBuiltin): ReflectionParameter |
| 113 | + { |
| 114 | + $parameter = $this->createMock(ReflectionParameter::class); |
| 115 | + $type = $this->createMock(ReflectionNamedType::class); |
| 116 | + |
| 117 | + $type->method('getName')->willReturn($typeName); |
| 118 | + $type->method('isBuiltin')->willReturn($isBuiltin); |
| 119 | + |
| 120 | + $parameter->method('getType')->willReturn($type); |
| 121 | + |
| 122 | + return $parameter; |
| 123 | + } |
| 124 | + |
| 125 | + private function createParameterWithUnionType(): ReflectionParameter |
| 126 | + { |
| 127 | + $parameter = $this->createMock(ReflectionParameter::class); |
| 128 | + $unionType = $this->createMock(ReflectionUnionType::class); |
| 129 | + |
| 130 | + $parameter->method('getType')->willReturn($unionType); |
| 131 | + |
| 132 | + return $parameter; |
| 133 | + } |
| 134 | +} |
0 commit comments