Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.2 Action file generator: Do not return when return type is never #6462

Merged
merged 1 commit into from May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Codeception/Lib/Generator/Actions.php
Expand Up @@ -116,7 +116,7 @@ protected function addMethod(\ReflectionMethod $refMethod)
->place('module', $module)
->place('method', $refMethod->name)
->place('return_type', $returnType)
->place('return', $returnType === ': void' ? '' : 'return ')
->place('return', ($returnType === ': void' || $returnType === ': never') ? '' : 'return ')
->place('params', $params);

if (0 === strpos($refMethod->name, 'see')) {
Expand Down
25 changes: 25 additions & 0 deletions tests/cli/BuildCest.php
Expand Up @@ -118,6 +118,8 @@ public function noReturnForVoidType(CliGuy $I, Scenario $scenario)
$I->seeInThisFile('use _generated\CliGuyActions');
$I->seeFileFound('CliGuyActions.php', 'tests/support/_generated');
$I->seeInThisFile('public function seeDirFound($dir): void');
$I->seeInThisFile('$this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion(\'seeDirFound\', func_get_args()));');
$I->dontSeeInThisFile('return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion(\'seeDirFound\', func_get_args()));');
}

public function generateNullableParametersOnPHP70(CliGuy $I, Scenario $scenario)
Expand Down Expand Up @@ -210,4 +212,27 @@ public function generateCorrectTypeWhenParentTypeIsUsed(CliGuy $I, Scenario $sce
$I->seeFileFound('CliGuyActions.php', 'tests/support/_generated');
$I->seeInThisFile('public function seeDirFound(\Codeception\Module $dir): \Codeception\Module');
}

public function noReturnForNeverType(CliGuy $I, Scenario $scenario)
{
if (PHP_VERSION_ID < 80100) {
$scenario->skip('Does not work in PHP < 8.1');
}

$I->wantToTest('no return keyword generated for never typehint');

$cliHelperContents = file_get_contents(codecept_root_dir('tests/support/CliHelper.php'));
$cliHelperContents = str_replace('public function seeDirFound($dir)', 'public function seeDirFound($dir): never', $cliHelperContents);
file_put_contents(codecept_root_dir('tests/support/CliHelper.php'), $cliHelperContents);

$I->runShellCommand('php codecept build');
$I->seeInShellOutput('generated successfully');
$I->seeInSupportDir('CliGuy.php');
$I->seeInThisFile('class CliGuy extends \Codeception\Actor');
$I->seeInThisFile('use _generated\CliGuyActions');
$I->seeFileFound('CliGuyActions.php', 'tests/support/_generated');
$I->seeInThisFile('public function seeDirFound($dir): never');
$I->seeInThisFile('$this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion(\'seeDirFound\', func_get_args()));');
$I->dontSeeInThisFile('return $this->getScenario()->runStep(new \Codeception\Step\ConditionalAssertion(\'seeDirFound\', func_get_args()));');
}
}