Skip to content

Commit 01932d7

Browse files
authored
Merge pull request Codeception#6124 from Codeception/issue-6122
Action file generator improvements
2 parents f47547b + a97829f commit 01932d7

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/Codeception/Lib/Generator/Actions.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,18 @@ protected function getParamsString(\ReflectionMethod $refMethod)
155155
{
156156
$params = [];
157157
foreach ($refMethod->getParameters() as $param) {
158+
$type = '';
159+
if (PHP_VERSION_ID >= 70000) {
160+
$reflectionType = $param->getType();
161+
if ($reflectionType !== null) {
162+
$type = $this->stringifyType($reflectionType) . ' ';
163+
}
164+
}
158165
if ($param->isOptional()) {
159-
$params[] = '$' . $param->name . ' = ' . ReflectionHelper::getDefaultValue($param);
166+
$params[] = $type . '$' . $param->name . ' = ' . ReflectionHelper::getDefaultValue($param);
160167
} else {
161-
$params[] = '$' . $param->name;
162-
};
168+
$params[] = $type . '$' . $param->name;
169+
}
163170
}
164171
return implode(', ', $params);
165172
}
@@ -222,13 +229,27 @@ private function createReturnTypeHint(\ReflectionMethod $refMethod)
222229
return '';
223230
}
224231

232+
return ': ' . $this->stringifyType($returnType);
233+
}
234+
235+
/**
236+
* @param \ReflectionType $returnType
237+
* @return string
238+
*/
239+
private function stringifyType(\ReflectionType $returnType)
240+
{
241+
if ($returnType instanceof \ReflectionUnionType) {
242+
$types = $returnType->getTypes();
243+
return implode('|', $types);
244+
}
245+
225246
if (PHP_VERSION_ID < 70100) {
226247
$returnTypeString = (string)$returnType;
227248
} else {
228249
$returnTypeString = $returnType->getName();
229250
}
230251
return sprintf(
231-
': %s%s%s',
252+
'%s%s%s',
232253
$returnType->allowsNull() ? '?' : '',
233254
$returnType->isBuiltin() ? '' : '\\',
234255
$returnTypeString

tests/cli/BuildCest.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function usesTypehintsWherePossible(CliGuy $I, Scenario $scenario)
4141
$I->wantToTest('generate typehints with generated actions');
4242

4343
$cliHelperContents = file_get_contents(codecept_root_dir('tests/support/CliHelper.php'));
44-
$cliHelperContents = str_replace('public function grabFromOutput($regex)', 'public function grabFromOutput($regex): int', $cliHelperContents);
44+
$cliHelperContents = str_replace('public function grabFromOutput($regex)', 'public function grabFromOutput(string $regex): int', $cliHelperContents);
4545
file_put_contents(codecept_root_dir('tests/support/CliHelper.php'), $cliHelperContents);
4646

4747
$I->runShellCommand('php codecept build');
@@ -50,15 +50,34 @@ public function usesTypehintsWherePossible(CliGuy $I, Scenario $scenario)
5050
$I->seeInThisFile('class CliGuy extends \Codeception\Actor');
5151
$I->seeInThisFile('use _generated\CliGuyActions');
5252
$I->seeFileFound('CliGuyActions.php', 'tests/support/_generated');
53-
$I->openFile(codecept_root_dir('tests/support/CliHelper.php'));
54-
$I->seeInThisFile('public function grabFromOutput($regex): int');
55-
$I->seeInThisFile('return $match[1]');
53+
$I->seeInThisFile('public function grabFromOutput(string $regex): int');
54+
}
55+
56+
public function generatedUnionReturnTypeOnPhp8(CliGuy $I, Scenario $scenario)
57+
{
58+
if (PHP_MAJOR_VERSION < 8) {
59+
$scenario->skip('Does not work in PHP < 8');
60+
}
61+
62+
$I->wantToTest('generate action with union return type');
63+
64+
$cliHelperContents = file_get_contents(codecept_root_dir('tests/support/CliHelper.php'));
65+
$cliHelperContents = str_replace('public function grabFromOutput($regex)', 'public function grabFromOutput(array|string $param): int|string', $cliHelperContents);
66+
file_put_contents(codecept_root_dir('tests/support/CliHelper.php'), $cliHelperContents);
67+
68+
$I->runShellCommand('php codecept build');
69+
$I->seeInShellOutput('generated successfully');
70+
$I->seeInSupportDir('CliGuy.php');
71+
$I->seeInThisFile('class CliGuy extends \Codeception\Actor');
72+
$I->seeInThisFile('use _generated\CliGuyActions');
73+
$I->seeFileFound('CliGuyActions.php', 'tests/support/_generated');
74+
$I->seeInThisFile('public function grabFromOutput(array|string $param): string|int');
5675
}
5776

5877
public function noReturnForVoidType(CliGuy $I, Scenario $scenario)
5978
{
60-
if (PHP_MAJOR_VERSION < 7) {
61-
$scenario->skip('Does not work in PHP < 7');
79+
if (PHP_VERSION_ID < 70100) {
80+
$scenario->skip('Does not work in PHP < 7.1');
6281
}
6382

6483
$I->wantToTest('no return keyword generated for void typehint');
@@ -73,9 +92,6 @@ public function noReturnForVoidType(CliGuy $I, Scenario $scenario)
7392
$I->seeInThisFile('class CliGuy extends \Codeception\Actor');
7493
$I->seeInThisFile('use _generated\CliGuyActions');
7594
$I->seeFileFound('CliGuyActions.php', 'tests/support/_generated');
76-
$I->openFile(codecept_root_dir('tests/support/CliHelper.php'));
7795
$I->seeInThisFile('public function seeDirFound($dir): void');
78-
$I->dontSeeInThisFile('return $this->assertTrue(is_dir(');
79-
$I->seeInThisFile('$this->assertTrue(is_dir(');
8096
}
8197
}

0 commit comments

Comments
 (0)