Skip to content

Commit

Permalink
TASK: Add ExpressionTypeNarrowerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Apr 26, 2023
1 parent aaf6c49 commit 560c97d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 26 deletions.
6 changes: 6 additions & 0 deletions src/TypeSystem/Narrower/NarrowedTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@ public function getType(string $identifierName): ?TypeInterface
{
return $this->types[$identifierName] ?? null;
}

/** @return array<string,TypeInterface> */
public function toArray(): array
{
return $this->types;
}
}
104 changes: 104 additions & 0 deletions test/Unit/TypeSystem/Narrower/ExpressionTypeNarrowerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* PackageFactory.ComponentEngine - Universal View Components for PHP
* Copyright (C) 2022 Contributors of PackageFactory.ComponentEngine
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Narrower;


use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode;
use PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Scope\Fixtures\DummyScope;
use PackageFactory\ComponentEngine\TypeSystem\Narrower\ExpressionTypeNarrower;
use PackageFactory\ComponentEngine\TypeSystem\Narrower\NarrowedTypes;
use PackageFactory\ComponentEngine\TypeSystem\Narrower\TypeNarrowerContext;
use PackageFactory\ComponentEngine\TypeSystem\Type\NullType\NullType;
use PackageFactory\ComponentEngine\TypeSystem\Type\StringType\StringType;
use PackageFactory\ComponentEngine\TypeSystem\Type\UnionType\UnionType;
use PHPUnit\Framework\TestCase;

final class ExpressionTypeNarrowerTest extends TestCase
{
public function narrowedExpressionsExamples(): mixed
{
return [
'nullableString' => [
'nullableString',
$variableIsString = NarrowedTypes::fromEntry('nullableString', StringType::get())
],

'nullableString === null' => [
'nullableString === null',
$variableIsNull = NarrowedTypes::fromEntry('nullableString', NullType::get())
],
// Patience you must have my young Padawan.
'null === nullableString' => [
'null === nullableString', $variableIsNull
],

'nullableString !== null' => [
'nullableString !== null', $variableIsString
],
'null !== nullableString' => [
'null !== nullableString', $variableIsString
],

'true === (nullableString === null)' => [
'true === (nullableString === null)', $variableIsNull
],
'false !== (nullableString === null)' => [
'false !== (nullableString === null)', $variableIsNull
],

'false === (nullableString === null)' => [
'false === (nullableString === null)', $variableIsString
],
'true !== (nullableString === null)' => [
'true !== (nullableString === null)', $variableIsString
],

'nullableString === variableOfTypeNull' => [
'nullableString === variableOfTypeNull', $variableIsNull
],
];
}

/**
* @dataProvider narrowedExpressionsExamples
* @test
*/
public function narrowedExpressions(string $expressionAsString, NarrowedTypes $expectedTypes): void
{
$expressionTypeNarrower = new ExpressionTypeNarrower(
scope: new DummyScope([
'nullableString' => UnionType::of(StringType::get(), NullType::get()),
'variableOfTypeNull' => NullType::get()
])
);

$expressionNode = ExpressionNode::fromString($expressionAsString);

$actualTypes = $expressionTypeNarrower->narrowTypesOfSymbolsIn($expressionNode, TypeNarrowerContext::TRUTHY);

$this->assertEqualsCanonicalizing(
$expectedTypes->toArray(),
$actualTypes->toArray()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,6 @@ public function ternaryOperationExamples(): array
'null === nullableString ? "" : nullableString' => [
'null === nullableString ? "" : nullableString', StringType::get()
],

'nullableString !== null ? nullableString : ""' => [
'nullableString !== null ? nullableString : ""', StringType::get()
],
'null !== nullableString ? nullableString : ""' => [
'null !== nullableString ? nullableString : ""', StringType::get()
],

'true === (nullableString === null) ? "" : nullableString' => [
'true === (nullableString === null) ? "" : nullableString', StringType::get()
],
'false !== (nullableString === null) ? "" : nullableString' => [
'false !== (nullableString === null) ? "" : nullableString', StringType::get()
],

'false === (nullableString === null) ? nullableString : ""' => [
'false === (nullableString === null) ? nullableString : ""', StringType::get()
],
'true !== (nullableString === null) ? nullableString : ""' => [
'true !== (nullableString === null) ? nullableString : ""', StringType::get()
],

'nullableString === variableOfTypeNull ? "" : nullableString' => [
'nullableString === variableOfTypeNull ? "" : nullableString', StringType::get()
],
];
}

Expand All @@ -103,7 +78,6 @@ public function resolvesTernaryOperationToResultingType(string $ternaryExpressio
$scope = new DummyScope([
'variableOfTypeString' => StringType::get(),
'variableOfTypeNumber' => NumberType::get(),
'variableOfTypeNull' => NullType::get(),
'nullableString' => UnionType::of(StringType::get(), NullType::get())
]);
$ternaryOperationTypeResolver = new TernaryOperationTypeResolver(
Expand Down

0 comments on commit 560c97d

Please sign in to comment.