Skip to content

Commit 560c97d

Browse files
committed
TASK: Add ExpressionTypeNarrowerTest
1 parent aaf6c49 commit 560c97d

File tree

3 files changed

+110
-26
lines changed

3 files changed

+110
-26
lines changed

src/TypeSystem/Narrower/NarrowedTypes.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@ public function getType(string $identifierName): ?TypeInterface
5353
{
5454
return $this->types[$identifierName] ?? null;
5555
}
56+
57+
/** @return array<string,TypeInterface> */
58+
public function toArray(): array
59+
{
60+
return $this->types;
61+
}
5662
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

test/Unit/TypeSystem/Resolver/TernaryOperation/TernaryOperationTypeResolverTest.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,6 @@ public function ternaryOperationExamples(): array
6363
'null === nullableString ? "" : nullableString' => [
6464
'null === nullableString ? "" : nullableString', StringType::get()
6565
],
66-
67-
'nullableString !== null ? nullableString : ""' => [
68-
'nullableString !== null ? nullableString : ""', StringType::get()
69-
],
70-
'null !== nullableString ? nullableString : ""' => [
71-
'null !== nullableString ? nullableString : ""', StringType::get()
72-
],
73-
74-
'true === (nullableString === null) ? "" : nullableString' => [
75-
'true === (nullableString === null) ? "" : nullableString', StringType::get()
76-
],
77-
'false !== (nullableString === null) ? "" : nullableString' => [
78-
'false !== (nullableString === null) ? "" : nullableString', StringType::get()
79-
],
80-
81-
'false === (nullableString === null) ? nullableString : ""' => [
82-
'false === (nullableString === null) ? nullableString : ""', StringType::get()
83-
],
84-
'true !== (nullableString === null) ? nullableString : ""' => [
85-
'true !== (nullableString === null) ? nullableString : ""', StringType::get()
86-
],
87-
88-
'nullableString === variableOfTypeNull ? "" : nullableString' => [
89-
'nullableString === variableOfTypeNull ? "" : nullableString', StringType::get()
90-
],
9166
];
9267
}
9368

@@ -103,7 +78,6 @@ public function resolvesTernaryOperationToResultingType(string $ternaryExpressio
10378
$scope = new DummyScope([
10479
'variableOfTypeString' => StringType::get(),
10580
'variableOfTypeNumber' => NumberType::get(),
106-
'variableOfTypeNull' => NullType::get(),
10781
'nullableString' => UnionType::of(StringType::get(), NullType::get())
10882
]);
10983
$ternaryOperationTypeResolver = new TernaryOperationTypeResolver(

0 commit comments

Comments
 (0)