Skip to content

Commit

Permalink
Merge pull request #6462 from Codeception/4.2-support-never-type
Browse files Browse the repository at this point in the history
4.2 Action file generator: Do not return when return type is never
  • Loading branch information
Naktibalda committed May 24, 2022
2 parents c502768 + 813df98 commit 0d81f27
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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()));');
}
}

0 comments on commit 0d81f27

Please sign in to comment.