1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
<?php declare(strict_types=1);
/*
* This file is part of sebastian/type.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Type;
use function strtolower;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\Type\TestFixture\ChildClass;
use SebastianBergmann\Type\TestFixture\ParentClass;
use someNamespaceA\NamespacedClass;
#[CoversClass(ObjectType::class)]
#[CoversClass(Type::class)]
#[UsesClass(SimpleType::class)]
#[UsesClass(TypeName::class)]
#[Small]
final class ObjectTypeTest extends TestCase
{
private ObjectType $childClass;
private ObjectType $parentClass;
protected function setUp(): void
{
$this->childClass = new ObjectType(
TypeName::fromQualifiedName(ChildClass::class),
false,
);
$this->parentClass = new ObjectType(
TypeName::fromQualifiedName(ParentClass::class),
false,
);
}
public function testHasName(): void
{
$this->assertSame(ChildClass::class, $this->childClass->name());
}
public function testParentIsNotAssignableToChild(): void
{
$this->assertFalse($this->childClass->isAssignable($this->parentClass));
}
public function testChildIsAssignableToParent(): void
{
$this->assertTrue($this->parentClass->isAssignable($this->childClass));
}
public function testClassIsAssignableToSelf(): void
{
$this->assertTrue($this->parentClass->isAssignable($this->parentClass));
}
public function testSimpleTypeIsNotAssignableToClass(): void
{
$this->assertFalse($this->parentClass->isAssignable(new SimpleType('int', false)));
}
public function testClassFromOneNamespaceIsNotAssignableToClassInOtherNamespace(): void
{
$classFromNamespaceA = new ObjectType(
/** @phpstan-ignore class.notFound */
TypeName::fromQualifiedName(NamespacedClass::class),
false,
);
$classFromNamespaceB = new ObjectType(
/** @phpstan-ignore class.notFound */
TypeName::fromQualifiedName(\someNamespaceB\NamespacedClass::class),
false,
);
$this->assertFalse($classFromNamespaceA->isAssignable($classFromNamespaceB));
}
public function testClassIsAssignableToSelfCaseInsensitively(): void
{
$classLowercased = new ObjectType(
TypeName::fromQualifiedName(strtolower(ParentClass::class)),
false,
);
$this->assertTrue($this->parentClass->isAssignable($classLowercased));
}
public function testNullIsAssignableToNullableType(): void
{
$someClass = new ObjectType(
TypeName::fromQualifiedName(ParentClass::class),
true,
);
$this->assertTrue($someClass->isAssignable(Type::fromValue(null, true)));
}
public function testNullIsNotAssignableToNotNullableType(): void
{
$someClass = new ObjectType(
TypeName::fromQualifiedName(ParentClass::class),
false,
);
$this->assertFalse($someClass->isAssignable(Type::fromValue(null, true)));
}
public function testPreservesNullNotAllowed(): void
{
$someClass = new ObjectType(
TypeName::fromQualifiedName(ParentClass::class),
false,
);
$this->assertFalse($someClass->allowsNull());
}
public function testPreservesNullAllowed(): void
{
$someClass = new ObjectType(
TypeName::fromQualifiedName(ParentClass::class),
true,
);
$this->assertTrue($someClass->allowsNull());
}
public function testHasClassName(): void
{
$this->assertSame('SebastianBergmann\Type\TestFixture\ParentClass', $this->parentClass->className()->qualifiedName());
}
public function testCanBeQueriedForType(): void
{
/** @phpstan-ignore method.alreadyNarrowedType */
$this->assertTrue($this->childClass->isObject());
$this->assertFalse($this->childClass->isCallable());
$this->assertFalse($this->childClass->isFalse());
$this->assertFalse($this->childClass->isGenericObject());
$this->assertFalse($this->childClass->isIntersection());
$this->assertFalse($this->childClass->isIterable());
$this->assertFalse($this->childClass->isMixed());
$this->assertFalse($this->childClass->isNever());
$this->assertFalse($this->childClass->isNull());
$this->assertFalse($this->childClass->isSimple());
$this->assertFalse($this->childClass->isStatic());
$this->assertFalse($this->childClass->isTrue());
$this->assertFalse($this->childClass->isUnion());
$this->assertFalse($this->childClass->isUnknown());
$this->assertFalse($this->childClass->isVoid());
}
}
|