Skip to content

Commit cefb90d

Browse files
mvrieljaapio
authored andcommitted
Add integration tests for Expressions, and fail
As part of checking whether everything works as expected I am adding integration tests, and find out that the pretty printer can be a bit greedy. This is exemplified in the ConstructorPromotionTest where I want to pass a reference to a local class constant, but the ExpressionPrinter picks up on the class reference and does not resolve it. This is a major issue, and more work is needed to a) parse the whole FQSEN and use that b) find out how to resolve references, such as self Another test is needed to see if it resolves relative references as well, as the Standard PrettyPrinter code gives me the idea it does not.
1 parent 3e599c4 commit cefb90d

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

tests/integration/FileDocblockTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
/**
1313
* Integration tests to check the correct working of processing a namespace into a project.
14+
*
15+
* @coversNothing
1416
*/
1517
#[CoversNothing]
1618
final class FileDocblockTest extends TestCase

tests/integration/PHP8/ConstructorPromotionTest.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function setUp() : void
4646
);
4747
}
4848

49-
public function testPropertiesAreCreated() : void
49+
public function testArgumentsAreReadCorrectly() : void
5050
{
5151
$file = $this->project->getFiles()[self::FILE];
5252
$class = $file->getClasses()['\\PHP8\\ConstructorPromotion'];
@@ -55,13 +55,45 @@ public function testPropertiesAreCreated() : void
5555
$constructor->addArgument(new Argument('name', new String_(), "'default name'"));
5656
$constructor->addArgument(new Argument('email', new Object_(new Fqsen('\\PHP8\\Email'))));
5757
$constructor->addArgument(new Argument('birth_date', new Object_(new Fqsen('\\' . DateTimeImmutable::class))));
58+
$constructor->addArgument(
59+
new Argument(
60+
'created_at',
61+
new Object_(new Fqsen('\\' . DateTimeImmutable::class)),
62+
new Expression(
63+
'new {{ PHPDOC6ffacd918e2f70478d2fd33dcb58c4d4 }}(\'now\')',
64+
[
65+
'{{ PHPDOC6ffacd918e2f70478d2fd33dcb58c4d4 }}' => new Fqsen('\\DateTimeImmutable')
66+
]
67+
)
68+
)
69+
);
70+
$constructor->addArgument(
71+
new Argument(
72+
'uses_constants',
73+
new Array_(),
74+
new Expression(
75+
'[{{ PHPDOC590f53e8699817c6fa498cc11a4cbe63 }}]',
76+
[
77+
'{{ PHPDOC590f53e8699817c6fa498cc11a4cbe63 }}' => new Fqsen('\PHP8\ConstructorPromotion::DEFAULT_VALUE')
78+
]
79+
)
80+
)
81+
);
5882

5983
self::assertEquals($constructor, $class->getMethods()['\PHP8\ConstructorPromotion::__construct()']);
84+
}
85+
86+
public function testPropertiesAreCreated() : void
87+
{
88+
$file = $this->project->getFiles()[self::FILE];
89+
$class = $file->getClasses()['\\PHP8\\ConstructorPromotion'];
90+
6091
self::assertEquals(
6192
[
6293
'\PHP8\ConstructorPromotion::$name' => $this->expectedNameProperty(),
6394
'\PHP8\ConstructorPromotion::$email' => $this->expectedEmailProperty(),
6495
'\PHP8\ConstructorPromotion::$birth_date' => $this->expectedBirthDateProperty(),
96+
'\PHP8\ConstructorPromotion::$created_at' => $this->expectedCreatedAtProperty(),
6597
],
6698
$class->getProperties()
6799
);
@@ -89,7 +121,7 @@ private function expectedConstructorMethod(): Method
89121
false,
90122
false,
91123
new Location(18, 264),
92-
new Location(29, 568)
124+
new Location(31, 709)
93125
);
94126
}
95127

@@ -141,4 +173,23 @@ private function expectedBirthDateProperty(): Property
141173
new Object_(new Fqsen('\\' . DateTimeImmutable::class))
142174
);
143175
}
176+
177+
private function expectedCreatedAtProperty(): Property
178+
{
179+
return new Property(
180+
new Fqsen('\PHP8\ConstructorPromotion::$created_at'),
181+
new Visibility(Visibility::PRIVATE_),
182+
null,
183+
new Expression(
184+
'new {{ PHPDOC6ffacd918e2f70478d2fd33dcb58c4d4 }}(\'now\')',
185+
[
186+
'{{ PHPDOC6ffacd918e2f70478d2fd33dcb58c4d4 }}' => new Fqsen('\\DateTimeImmutable')
187+
]
188+
),
189+
false,
190+
new Location(29),
191+
new Location(29),
192+
new Object_(new Fqsen('\\' . DateTimeImmutable::class))
193+
);
194+
}
144195
}

tests/integration/data/PHP8/ConstructorPromotion.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ public function __construct(
2626
public string $name = 'default name',
2727
protected Email $email,
2828
private DateTimeImmutable $birth_date,
29+
private DateTimeImmutable $created_at = new DateTimeImmutable('now'),
30+
private array $uses_constants = [self::DEFAULT_VALUE],
2931
) {}
3032
}

0 commit comments

Comments
 (0)