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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
<?php declare(strict_types=1);
namespace PhpParser;
use PhpParser\Builder\Class_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
use PhpParser\Node\Expr;
class BuilderHelpersTest extends \PHPUnit\Framework\TestCase {
public function testNormalizeNode(): void {
$builder = new Class_('SomeClass');
$this->assertEquals($builder->getNode(), BuilderHelpers::normalizeNode($builder));
$attribute = new Node\Attribute(new Node\Name('Test'));
$this->assertSame($attribute, BuilderHelpers::normalizeNode($attribute));
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected node or builder object');
BuilderHelpers::normalizeNode('test');
}
public function testNormalizeStmt(): void {
$stmt = new Node\Stmt\Class_('Class');
$this->assertSame($stmt, BuilderHelpers::normalizeStmt($stmt));
$expr = new Expr\Variable('fn');
$normalizedExpr = BuilderHelpers::normalizeStmt($expr);
$this->assertEquals(new Stmt\Expression($expr), $normalizedExpr);
$this->assertSame($expr, $normalizedExpr->expr);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected statement or expression node');
BuilderHelpers::normalizeStmt(new Node\Attribute(new Node\Name('Test')));
}
public function testNormalizeStmtInvalidType(): void {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected node or builder object');
BuilderHelpers::normalizeStmt('test');
}
public function testNormalizeIdentifier(): void {
$identifier = new Node\Identifier('fn');
$this->assertSame($identifier, BuilderHelpers::normalizeIdentifier($identifier));
$this->assertEquals($identifier, BuilderHelpers::normalizeIdentifier('fn'));
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected string or instance of Node\Identifier');
BuilderHelpers::normalizeIdentifier(1);
}
public function testNormalizeIdentifierOrExpr(): void {
$identifier = new Node\Identifier('fn');
$this->assertSame($identifier, BuilderHelpers::normalizeIdentifierOrExpr($identifier));
$expr = new Expr\Variable('fn');
$this->assertSame($expr, BuilderHelpers::normalizeIdentifierOrExpr($expr));
$this->assertEquals($identifier, BuilderHelpers::normalizeIdentifierOrExpr('fn'));
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected string or instance of Node\Identifier');
BuilderHelpers::normalizeIdentifierOrExpr(1);
}
public function testNormalizeName(): void {
$name = new Node\Name('test');
$this->assertSame($name, BuilderHelpers::normalizeName($name));
$this->assertEquals(
new Node\Name\FullyQualified(['Namespace', 'Test']),
BuilderHelpers::normalizeName('\\Namespace\\Test')
);
$this->assertEquals(
new Node\Name\Relative(['Test']),
BuilderHelpers::normalizeName('namespace\\Test')
);
$this->assertEquals($name, BuilderHelpers::normalizeName('test'));
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Name cannot be empty');
BuilderHelpers::normalizeName('');
}
public function testNormalizeNameInvalidType(): void {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Name must be a string or an instance of Node\Name');
BuilderHelpers::normalizeName(1);
}
public function testNormalizeNameOrExpr(): void {
$expr = new Expr\Variable('fn');
$this->assertSame($expr, BuilderHelpers::normalizeNameOrExpr($expr));
$name = new Node\Name('test');
$this->assertSame($name, BuilderHelpers::normalizeNameOrExpr($name));
$this->assertEquals(
new Node\Name\FullyQualified(['Namespace', 'Test']),
BuilderHelpers::normalizeNameOrExpr('\\Namespace\\Test')
);
$this->assertEquals(
new Node\Name\Relative(['Test']),
BuilderHelpers::normalizeNameOrExpr('namespace\\Test')
);
$this->assertEquals($name, BuilderHelpers::normalizeNameOrExpr('test'));
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Name cannot be empty');
BuilderHelpers::normalizeNameOrExpr('');
}
public function testNormalizeNameOrExpInvalidType(): void {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Name must be a string or an instance of Node\Name or Node\Expr');
BuilderHelpers::normalizeNameOrExpr(1);
}
public function testNormalizeType(): void {
$this->assertEquals(new Node\Identifier('array'), BuilderHelpers::normalizeType('array'));
$this->assertEquals(new Node\Identifier('callable'), BuilderHelpers::normalizeType('callable'));
$this->assertEquals(new Node\Identifier('string'), BuilderHelpers::normalizeType('string'));
$this->assertEquals(new Node\Identifier('int'), BuilderHelpers::normalizeType('int'));
$this->assertEquals(new Node\Identifier('float'), BuilderHelpers::normalizeType('float'));
$this->assertEquals(new Node\Identifier('bool'), BuilderHelpers::normalizeType('bool'));
$this->assertEquals(new Node\Identifier('iterable'), BuilderHelpers::normalizeType('iterable'));
$this->assertEquals(new Node\Identifier('void'), BuilderHelpers::normalizeType('void'));
$this->assertEquals(new Node\Identifier('object'), BuilderHelpers::normalizeType('object'));
$this->assertEquals(new Node\Identifier('null'), BuilderHelpers::normalizeType('null'));
$this->assertEquals(new Node\Identifier('false'), BuilderHelpers::normalizeType('false'));
$this->assertEquals(new Node\Identifier('mixed'), BuilderHelpers::normalizeType('mixed'));
$this->assertEquals(new Node\Identifier('never'), BuilderHelpers::normalizeType('never'));
$this->assertEquals(new Node\Identifier('true'), BuilderHelpers::normalizeType('true'));
$intIdentifier = new Node\Identifier('int');
$this->assertSame($intIdentifier, BuilderHelpers::normalizeType($intIdentifier));
$intName = new Node\Name('int');
$this->assertSame($intName, BuilderHelpers::normalizeType($intName));
$intNullable = new Node\NullableType(new Identifier('int'));
$this->assertSame($intNullable, BuilderHelpers::normalizeType($intNullable));
$unionType = new Node\UnionType([new Node\Identifier('int'), new Node\Identifier('string')]);
$this->assertSame($unionType, BuilderHelpers::normalizeType($unionType));
$intersectionType = new Node\IntersectionType([new Node\Name('A'), new Node\Name('B')]);
$this->assertSame($intersectionType, BuilderHelpers::normalizeType($intersectionType));
$expectedNullable = new Node\NullableType($intIdentifier);
$nullable = BuilderHelpers::normalizeType('?int');
$this->assertEquals($expectedNullable, $nullable);
$this->assertEquals($intIdentifier, $nullable->type);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Type must be a string, or an instance of Name, Identifier or ComplexType');
BuilderHelpers::normalizeType(1);
}
public function testNormalizeTypeNullableVoid(): void {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('void type cannot be nullable');
BuilderHelpers::normalizeType('?void');
}
public function testNormalizeTypeNullableMixed(): void {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('mixed type cannot be nullable');
BuilderHelpers::normalizeType('?mixed');
}
public function testNormalizeTypeNullableNever(): void {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('never type cannot be nullable');
BuilderHelpers::normalizeType('?never');
}
public function testNormalizeValue(): void {
$expression = new Scalar\Int_(1);
$this->assertSame($expression, BuilderHelpers::normalizeValue($expression));
$this->assertEquals(new Expr\ConstFetch(new Node\Name('null')), BuilderHelpers::normalizeValue(null));
$this->assertEquals(new Expr\ConstFetch(new Node\Name('true')), BuilderHelpers::normalizeValue(true));
$this->assertEquals(new Expr\ConstFetch(new Node\Name('false')), BuilderHelpers::normalizeValue(false));
$this->assertEquals(new Scalar\Int_(2), BuilderHelpers::normalizeValue(2));
$this->assertEquals(new Scalar\Float_(2.5), BuilderHelpers::normalizeValue(2.5));
$this->assertEquals(new Scalar\String_('text'), BuilderHelpers::normalizeValue('text'));
$this->assertEquals(
new Expr\Array_([
new Node\ArrayItem(new Scalar\Int_(0)),
new Node\ArrayItem(new Scalar\Int_(1), new Scalar\String_('test')),
]),
BuilderHelpers::normalizeValue([
0,
'test' => 1,
])
);
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Invalid value');
BuilderHelpers::normalizeValue(new \stdClass());
}
public function testNormalizeDocComment(): void {
$docComment = new Comment\Doc('Some doc comment');
$this->assertSame($docComment, BuilderHelpers::normalizeDocComment($docComment));
$this->assertEquals($docComment, BuilderHelpers::normalizeDocComment('Some doc comment'));
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
BuilderHelpers::normalizeDocComment(1);
}
public function testNormalizeAttribute(): void {
$attribute = new Node\Attribute(new Node\Name('Test'));
$attributeGroup = new Node\AttributeGroup([$attribute]);
$this->assertEquals($attributeGroup, BuilderHelpers::normalizeAttribute($attribute));
$this->assertSame($attributeGroup, BuilderHelpers::normalizeAttribute($attributeGroup));
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Attribute must be an instance of PhpParser\Node\Attribute or PhpParser\Node\AttributeGroup');
BuilderHelpers::normalizeAttribute('test');
}
public function testNormalizeValueEnum() {
if (\PHP_VERSION_ID <= 80100) {
$this->markTestSkipped('Enums are supported since PHP 8.1');
}
include __DIR__ . '/../fixtures/Suit.php';
$this->assertEquals(new Expr\ClassConstFetch(new FullyQualified(\Suit::class), new Identifier('Hearts')), BuilderHelpers::normalizeValue(\Suit::Hearts));
}
}
|