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

fix: Yii2 ErrorHandler can not process when throw Error (#106) #107

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/Codeception/Lib/Connector/Yii2.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\BrowserKit\History;
use Symfony\Component\BrowserKit\Response;
use Throwable;
use Yii;
use yii\base\ExitException;
use yii\base\Security;
Expand Down Expand Up @@ -345,16 +346,9 @@ public function doRequest(object $request): \Symfony\Component\BrowserKit\Respon
$response = $app->handleRequest($yiiRequest);
$app->trigger($app::EVENT_AFTER_REQUEST);
$response->send();
} catch (\Exception $e) {
if ($e instanceof UserException) {
// Don't discard output and pass exception handling to Yii to be able
// to expect error response codes in tests.
$app->errorHandler->discardExistingOutput = false;
$app->errorHandler->handleException($e);
} elseif (!$e instanceof ExitException) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was it necessary to remove this logic?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, see my comment in the issue itself. I am currently doubting whether the issue is valid at all.

// for exceptions not related to Http, we pass them to Codeception
throw $e;
}
} catch (Throwable $e) {
$app->errorHandler->discardExistingOutput = false;
$app->errorHandler->handleException($e);
$response = $app->response;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/cases/simple/controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ public function actionEnd()
\Yii::$app->end();
}

public function actionTypeError()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a return type to any function you touch!

{
$testClass = new class {
public ?int $intType = 0;
};

$testClass->intType = 'string';

return $testClass->intType;
}

/**
* @param Action $action
Expand Down
19 changes: 9 additions & 10 deletions tests/cases/simple/functional/SimpleCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,18 @@ public function testFormSubmit2(FunctionalTester $I)

public function testException(FunctionalTester $I)
{
$I->expectThrowable(new \Exception('This is not an HttpException'), function() use ($I) {
$I->amOnRoute('/site/exception');
});
$I->assertInstanceOf(Application::class, \Yii::$app);
$I->amOnRoute('/site/exception');
$I->seeResponseCodeIsServerError();
$content = $I->grabPageSource();
$I->assertStringStartsWith("<pre>Exception &apos;Exception&apos; with message &apos;This is not an HttpException&apos;", $content);
}
Comment on lines 36 to 42
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a return type to any function you touch!


public function testExceptionInBeforeRequest(FunctionalTester $I)
public function testTypeError(FunctionalTester $I)
{
$e = new \Exception('This is not an HttpException');
\Yii::$app->params['throw'] = $e;
$I->expectThrowable($e, function() use ($I) {
$I->amOnRoute('/site/exception');
});
$I->amOnRoute('/site/type-error');
$I->seeResponseCodeIsServerError();
$content = $I->grabPageSource();
$I->assertStringStartsWith('<pre>Exception &apos;TypeError&apos; with message &apos;Cannot assign string to property', $content);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to check the string contains Cannot assign string to property. Instead of the full HTML snippet.

}
Comment on lines +44 to 50
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a return type to any function you touch!


public function testExitException(FunctionalTester $I)
Expand Down
Loading