|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * PackageFactory.ComponentEngine - Universal View Components for PHP |
| 5 | + * Copyright (C) 2022 Contributors of PackageFactory.ComponentEngine |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Narrower; |
| 24 | + |
| 25 | + |
| 26 | +use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode; |
| 27 | +use PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Scope\Fixtures\DummyScope; |
| 28 | +use PackageFactory\ComponentEngine\TypeSystem\Narrower\ExpressionTypeNarrower; |
| 29 | +use PackageFactory\ComponentEngine\TypeSystem\Narrower\NarrowedTypes; |
| 30 | +use PackageFactory\ComponentEngine\TypeSystem\Narrower\TypeNarrowerContext; |
| 31 | +use PackageFactory\ComponentEngine\TypeSystem\Type\NullType\NullType; |
| 32 | +use PackageFactory\ComponentEngine\TypeSystem\Type\StringType\StringType; |
| 33 | +use PackageFactory\ComponentEngine\TypeSystem\Type\UnionType\UnionType; |
| 34 | +use PHPUnit\Framework\TestCase; |
| 35 | + |
| 36 | +final class ExpressionTypeNarrowerTest extends TestCase |
| 37 | +{ |
| 38 | + public function narrowedExpressionsExamples(): mixed |
| 39 | + { |
| 40 | + return [ |
| 41 | + 'nullableString' => [ |
| 42 | + 'nullableString', |
| 43 | + $variableIsString = NarrowedTypes::fromEntry('nullableString', StringType::get()) |
| 44 | + ], |
| 45 | + |
| 46 | + 'nullableString === null' => [ |
| 47 | + 'nullableString === null', |
| 48 | + $variableIsNull = NarrowedTypes::fromEntry('nullableString', NullType::get()) |
| 49 | + ], |
| 50 | + // Patience you must have my young Padawan. |
| 51 | + 'null === nullableString' => [ |
| 52 | + 'null === nullableString', $variableIsNull |
| 53 | + ], |
| 54 | + |
| 55 | + 'nullableString !== null' => [ |
| 56 | + 'nullableString !== null', $variableIsString |
| 57 | + ], |
| 58 | + 'null !== nullableString' => [ |
| 59 | + 'null !== nullableString', $variableIsString |
| 60 | + ], |
| 61 | + |
| 62 | + 'true === (nullableString === null)' => [ |
| 63 | + 'true === (nullableString === null)', $variableIsNull |
| 64 | + ], |
| 65 | + 'false !== (nullableString === null)' => [ |
| 66 | + 'false !== (nullableString === null)', $variableIsNull |
| 67 | + ], |
| 68 | + |
| 69 | + 'false === (nullableString === null)' => [ |
| 70 | + 'false === (nullableString === null)', $variableIsString |
| 71 | + ], |
| 72 | + 'true !== (nullableString === null)' => [ |
| 73 | + 'true !== (nullableString === null)', $variableIsString |
| 74 | + ], |
| 75 | + |
| 76 | + 'nullableString === variableOfTypeNull' => [ |
| 77 | + 'nullableString === variableOfTypeNull', $variableIsNull |
| 78 | + ], |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @dataProvider narrowedExpressionsExamples |
| 84 | + * @test |
| 85 | + */ |
| 86 | + public function narrowedExpressions(string $expressionAsString, NarrowedTypes $expectedTypes): void |
| 87 | + { |
| 88 | + $expressionTypeNarrower = new ExpressionTypeNarrower( |
| 89 | + scope: new DummyScope([ |
| 90 | + 'nullableString' => UnionType::of(StringType::get(), NullType::get()), |
| 91 | + 'variableOfTypeNull' => NullType::get() |
| 92 | + ]) |
| 93 | + ); |
| 94 | + |
| 95 | + $expressionNode = ExpressionNode::fromString($expressionAsString); |
| 96 | + |
| 97 | + $actualTypes = $expressionTypeNarrower->narrowTypesOfSymbolsIn($expressionNode, TypeNarrowerContext::TRUTHY); |
| 98 | + |
| 99 | + $this->assertEqualsCanonicalizing( |
| 100 | + $expectedTypes->toArray(), |
| 101 | + $actualTypes->toArray() |
| 102 | + ); |
| 103 | + } |
| 104 | +} |
0 commit comments