Skip to content

Commit d39c047

Browse files
committed
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 060297f commit d39c047

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
@@ -10,6 +10,8 @@
1010

1111
/**
1212
* Integration tests to check the correct working of processing a namespace into a project.
13+
*
14+
* @coversNothing
1315
*/
1416
final class FileDocblockTest extends TestCase
1517
{

tests/integration/PHP8/ConstructorPromotionTest.php

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

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

5882
self::assertEquals($constructor, $class->getMethods()['\PHP8\ConstructorPromotion::__construct()']);
83+
}
84+
85+
public function testPropertiesAreCreated() : void
86+
{
87+
$file = $this->project->getFiles()[self::FILE];
88+
$class = $file->getClasses()['\\PHP8\\ConstructorPromotion'];
89+
5990
self::assertEquals(
6091
[
6192
'\PHP8\ConstructorPromotion::$name' => $this->expectedNameProperty(),
6293
'\PHP8\ConstructorPromotion::$email' => $this->expectedEmailProperty(),
6394
'\PHP8\ConstructorPromotion::$birth_date' => $this->expectedBirthDateProperty(),
95+
'\PHP8\ConstructorPromotion::$created_at' => $this->expectedCreatedAtProperty(),
6496
],
6597
$class->getProperties()
6698
);
@@ -88,7 +120,7 @@ private function expectedConstructorMethod(): Method
88120
false,
89121
false,
90122
new Location(18, 264),
91-
new Location(29, 568)
123+
new Location(31, 709)
92124
);
93125
}
94126

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

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,
2727
protected string $email = 'test@example.com',
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)