|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\TypeInfo\Tests\TypeResolver; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\TypeInfo\Exception\InvalidArgumentException; |
| 16 | +use Symfony\Component\TypeInfo\Exception\UnsupportedException; |
| 17 | +use Symfony\Component\TypeInfo\Tests\Fixtures\AbstractDummy; |
| 18 | +use Symfony\Component\TypeInfo\Tests\Fixtures\Dummy; |
| 19 | +use Symfony\Component\TypeInfo\Type; |
| 20 | +use Symfony\Component\TypeInfo\TypeContext\TypeContext; |
| 21 | +use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory; |
| 22 | +use Symfony\Component\TypeInfo\TypeResolver\StringTypeResolver; |
| 23 | + |
| 24 | +class StringTypeResolverTest extends TestCase |
| 25 | +{ |
| 26 | + private StringTypeResolver $resolver; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->resolver = new StringTypeResolver(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @dataProvider resolveDataProvider |
| 35 | + */ |
| 36 | + public function testResolve(Type $expectedType, string $string, TypeContext $typeContext = null) |
| 37 | + { |
| 38 | + $this->assertEquals($expectedType, $this->resolver->resolve($string, $typeContext)); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return iterable<array{0: Type, 1: string, 2?: TypeContext}> |
| 43 | + */ |
| 44 | + public function resolveDataProvider(): iterable |
| 45 | + { |
| 46 | + $typeContextFactory = new TypeContextFactory(); |
| 47 | + |
| 48 | + // callable |
| 49 | + yield [Type::callable(), 'callable(string, int): mixed']; |
| 50 | + |
| 51 | + // array |
| 52 | + yield [Type::list(Type::bool()), 'bool[]']; |
| 53 | + |
| 54 | + // array shape |
| 55 | + yield [Type::array(), 'array{0: true, 1: false}']; |
| 56 | + |
| 57 | + // object shape |
| 58 | + yield [Type::object(), 'object{foo: true, bar: false}']; |
| 59 | + |
| 60 | + // this |
| 61 | + yield [Type::object(Dummy::class), '$this', $typeContextFactory->create(Dummy::class, AbstractDummy::class)]; |
| 62 | + |
| 63 | + // const |
| 64 | + yield [Type::array(), 'array[1, 2, 3]']; |
| 65 | + yield [Type::false(), 'false']; |
| 66 | + yield [Type::float(), '1.23']; |
| 67 | + yield [Type::int(), '1']; |
| 68 | + yield [Type::null(), 'null']; |
| 69 | + yield [Type::string(), '"string"']; |
| 70 | + yield [Type::true(), 'true']; |
| 71 | + |
| 72 | + // identifiers |
| 73 | + yield [Type::bool(), 'bool']; |
| 74 | + yield [Type::bool(), 'boolean']; |
| 75 | + yield [Type::true(), 'true']; |
| 76 | + yield [Type::false(), 'false']; |
| 77 | + yield [Type::int(), 'int']; |
| 78 | + yield [Type::int(), 'integer']; |
| 79 | + yield [Type::int(), 'positive-int']; |
| 80 | + yield [Type::int(), 'negative-int']; |
| 81 | + yield [Type::int(), 'non-positive-int']; |
| 82 | + yield [Type::int(), 'non-negative-int']; |
| 83 | + yield [Type::int(), 'non-zero-int']; |
| 84 | + yield [Type::float(), 'float']; |
| 85 | + yield [Type::float(), 'double']; |
| 86 | + yield [Type::string(), 'string']; |
| 87 | + yield [Type::string(), 'class-string']; |
| 88 | + yield [Type::string(), 'callable-string']; |
| 89 | + yield [Type::string(), 'numeric-string']; |
| 90 | + yield [Type::string(), 'non-empty-string']; |
| 91 | + yield [Type::string(), 'non-falsy-string']; |
| 92 | + yield [Type::string(), 'truthy-string']; |
| 93 | + yield [Type::string(), 'literal-string']; |
| 94 | + yield [Type::resource(), 'resource']; |
| 95 | + yield [Type::object(), 'object']; |
| 96 | + yield [Type::callable(), 'callable']; |
| 97 | + yield [Type::array(), 'array']; |
| 98 | + yield [Type::array(), 'non-empty-array']; |
| 99 | + yield [Type::list(), 'list']; |
| 100 | + yield [Type::list(), 'non-empty-list']; |
| 101 | + yield [Type::iterable(), 'iterable']; |
| 102 | + yield [Type::mixed(), 'mixed']; |
| 103 | + yield [Type::null(), 'null']; |
| 104 | + yield [Type::union(Type::int(), Type::string()), 'array-key']; |
| 105 | + yield [Type::union(Type::int(), Type::float(), Type::string(), Type::bool()), 'scalar']; |
| 106 | + yield [Type::object(AbstractDummy::class), 'self', $typeContextFactory->create(Dummy::class, AbstractDummy::class)]; |
| 107 | + yield [Type::object(Dummy::class), 'static', $typeContextFactory->create(Dummy::class, AbstractDummy::class)]; |
| 108 | + yield [Type::object(AbstractDummy::class), 'parent', $typeContextFactory->create(Dummy::class)]; |
| 109 | + yield [Type::object(Dummy::class), 'Dummy', $typeContextFactory->create(Dummy::class)]; |
| 110 | + yield [Type::template('T'), 'T']; |
| 111 | + |
| 112 | + // nullable |
| 113 | + yield [Type::nullable(Type::int()), '?int']; |
| 114 | + |
| 115 | + // generic |
| 116 | + yield [Type::generic(Type::int(), Type::string(), Type::bool()), 'int<string, bool>']; |
| 117 | + yield [Type::generic(Type::int(), Type::generic(Type::string(), Type::bool())), 'int<string<bool>>']; |
| 118 | + |
| 119 | + // union |
| 120 | + yield [Type::union(Type::int(), Type::string()), 'int|string']; |
| 121 | + |
| 122 | + // intersection |
| 123 | + yield [Type::intersection(Type::int(), Type::string()), 'int&string']; |
| 124 | + |
| 125 | + // DNF |
| 126 | + yield [Type::union(Type::int(), Type::intersection(Type::string(), Type::bool())), 'int|(string&bool)']; |
| 127 | + } |
| 128 | + |
| 129 | + public function testCannotResolveNonStringType() |
| 130 | + { |
| 131 | + $this->expectException(UnsupportedException::class); |
| 132 | + $this->resolver->resolve(123); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @dataProvider unhandledTypesDataProvider |
| 137 | + */ |
| 138 | + public function testCannotResolveInvalidTypes(string $string) |
| 139 | + { |
| 140 | + $this->expectException(UnsupportedException::class); |
| 141 | + $this->resolver->resolve($string); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @return iterable<array{0: string}> |
| 146 | + */ |
| 147 | + public function unhandledTypesDataProvider(): iterable |
| 148 | + { |
| 149 | + yield ['void']; |
| 150 | + yield ['never']; |
| 151 | + yield ['never-return']; |
| 152 | + yield ['never-returns']; |
| 153 | + yield ['no-return']; |
| 154 | + } |
| 155 | + |
| 156 | + public function testCannotResolveThisWithoutTypeContext() |
| 157 | + { |
| 158 | + $this->expectException(InvalidArgumentException::class); |
| 159 | + $this->resolver->resolve('$this'); |
| 160 | + } |
| 161 | +} |
0 commit comments