-
-
Notifications
You must be signed in to change notification settings - Fork 0
test: Add test for rendering exceptions in StatelessApplication with proper response validation.
#106
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
Conversation
…h proper response validation.
WalkthroughAdds a PHPUnit test that verifies template-based exception rendering receives the exception parameter and checks response details; also a whitespace-only insertion in ErrorHandler's HTML render branch and added PHP function imports in the test file. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #106 +/- ##
===========================================
Coverage 100.00% 100.00%
Complexity 320 320
===========================================
Files 12 12
Lines 810 810
===========================================
Hits 810 810 ☔ View full report in Codecov by Sentry. |
…ndling in `StatelessApplication` class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
tests/http/StatelessApplicationTest.php (1)
939-944: Reduce brittleness of exception heading assertion.This assert hardcodes the exact HTML and phrasing of the exception heading. Minor template or encoder changes (e.g., quotes, spacing, or prefix text) will break it. Prefer asserting the essentials: class and message presence.
Example refactor:
- self::assertStringContainsString( - '<pre>Exception (Exception) 'yii\base\Exception' with message 'Exception error message.'', - $responseBody, - "Response 'body' should contain exception class and message when 'exception' parameter is passed to " . - "'renderFile()' method in 'StatelessApplication'.", - ); + self::assertStringContainsString( + 'yii\base\Exception', + $responseBody, + "Response 'body' should contain exception class when 'exception' parameter is passed to 'renderFile()'.", + ); + self::assertStringContainsString( + 'Exception error message.', + $responseBody, + "Response 'body' should contain exception message when 'exception' parameter is passed to 'renderFile()'.", + );
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
tests/http/StatelessApplicationTest.php(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/http/StatelessApplicationTest.php (3)
tests/support/FactoryHelper.php (2)
FactoryHelper(46-268)createServerRequestCreator(154-161)tests/TestCase.php (1)
statelessApplication(124-187)src/http/StatelessApplication.php (1)
handle(269-295)
🪛 GitHub Actions: build
tests/http/StatelessApplicationTest.php
[error] 949-949: PHPUnit test failed: StatelessApplicationTest::testRenderExceptionPassesExceptionParameterToTemplateView. Assertion failed: Response body should contain stack trace information when an exception is passed to renderFile() in StatelessApplication. Command: vendor/bin/phpunit --colors=always --coverage-clover=coverage.xml --log-junit junit.xml.
…g and buffer management.
…th in response body.
… exception trace instead of file path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
src/http/ErrorHandler.php(1 hunks)tests/http/StatelessApplicationTest.php(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- src/http/ErrorHandler.php
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-03T16:24:09.241Z
Learnt from: terabytesoftw
PR: yii2-extensions/psr-bridge#53
File: src/http/ErrorHandler.php:258-272
Timestamp: 2025-08-03T16:24:09.241Z
Learning: In yii2-extensions/psr-bridge, the StatelessApplication creates a new Response instance for each request in the reset() method, then passes it to ErrorHandler::setResponse(). This means the template response is not shared across requests, so calling clear() on it in createErrorResponse() is safe and doesn't cause side effects.
Applied to files:
tests/http/StatelessApplicationTest.php
🧬 Code Graph Analysis (1)
tests/http/StatelessApplicationTest.php (4)
tests/http/ErrorHandlerTest.php (1)
RequiresPhpExtension(22-61)tests/support/FactoryHelper.php (2)
FactoryHelper(46-268)createServerRequestCreator(154-161)tests/TestCase.php (1)
statelessApplication(124-187)src/http/StatelessApplication.php (1)
handle(269-295)
🪛 GitHub Actions: build
tests/http/StatelessApplicationTest.php
[error] 958-958: Response 'body' should contain file path where exception occurred when 'exception' parameter is passed to 'renderFile()'.
[warning] 904-904: Risky test: StatelessApplicationTest::testRenderExceptionPassesExceptionParameterToTemplateView. Test code or tested code closed output buffers other than its own.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: phpunit / PHP 8.2-windows-latest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
♻️ Duplicate comments (1)
tests/http/StatelessApplicationTest.php (1)
906-965: Always restore output buffers and YII_ENV_TEST via try/finally.If any assertion fails, cleanup won’t run, leaving altered buffering levels and YII_ENV_TEST=false for later tests. Wrap the main body in try/finally so cleanup is guaranteed.
Apply this diff:
@\runkit_constant_redefine('YII_ENV_TEST', false); $initialBufferLevel = ob_get_level(); - $_SERVER = [ + try { + $_SERVER = [ 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => 'site/trigger-exception', - ]; + ]; - $request = FactoryHelper::createServerRequestCreator()->createFromGlobals(); + $request = FactoryHelper::createServerRequestCreator()->createFromGlobals(); - $app = $this->statelessApplication( - [ - 'components' => [ - 'errorHandler' => [ - 'errorAction' => null, - ], - ], - ], - ); + $app = $this->statelessApplication( + [ + 'components' => [ + 'errorHandler' => [ + 'errorAction' => null, + ], + ], + ], + ); - $response = $app->handle($request); + $response = $app->handle($request); - self::assertSame( + self::assertSame( 500, $response->getStatusCode(), "Response 'status code' should be '500' when exception occurs and template rendering is used in " . "'StatelessApplication'.", - ); - self::assertSame( + ); + self::assertSame( 'text/html; charset=UTF-8', $response->getHeaderLine('Content-Type'), "Response 'Content-Type' should be 'text/html; charset=UTF-8' for exception template rendering in " . "'StatelessApplication'.", - ); + ); - $responseBody = $response->getBody()->getContents(); + $responseBody = $response->getBody()->getContents(); - self::assertStringContainsString( + self::assertStringContainsString( 'yii\base\Exception', $responseBody, "Response 'body' should contain exception class when 'exception' parameter is passed to 'renderFile()'.", - ); - self::assertStringContainsString( + ); + self::assertStringContainsString( 'Exception error message.', $responseBody, "Response 'body' should contain exception message when 'exception' parameter is passed to 'renderFile()'.", - ); - self::assertStringContainsString( - '[internal function]: yii2\extensions\psrbridge\tests\support\stub\SiteController->actionTriggerException()', - $responseBody, - "Response 'body' should contain exception trace when 'exception' parameter is passed to 'renderFile()'.", - ); - - while (ob_get_level() < $initialBufferLevel) { - ob_start(); - } - - @\runkit_constant_redefine('YII_ENV_TEST', true); + ); + // See separate comment to switch this to a resilient regex assertion. + } finally { + while (ob_get_level() < $initialBufferLevel) { + ob_start(); + } + @\runkit_constant_redefine('YII_ENV_TEST', true); + }
🧹 Nitpick comments (1)
tests/http/StatelessApplicationTest.php (1)
954-958: Make the stack-trace assertion resilient to formatting differences.The exact trace line can vary across PHP/Yii versions and formatting (e.g., optional “[internal function]: ” prefix, HTML-escaped vs raw “->”). Prefer a tolerant regex to avoid future flakes.
Apply this diff:
- self::assertStringContainsString( - '[internal function]: yii2\extensions\psrbridge\tests\support\stub\SiteController->actionTriggerException()', - $responseBody, - "Response 'body' should contain exception trace when 'exception' parameter is passed to 'renderFile()'.", - ); + self::assertMatchesRegularExpression( + // Accept optional "[internal function]:", allow HTML-escaped or raw '->' + '~(?:\[internal function\]:\s*)?yii2\\\\extensions\\\\psrbridge\\\\tests\\\\support\\\\stub\\\\SiteController-(?:>|->)actionTriggerException\(\)~', + $responseBody, + "Response 'body' should reference SiteController::actionTriggerException() in the stack trace (robust to HTML escaping and optional '[internal function]').", + );
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
tests/http/StatelessApplicationTest.php(1 hunks)
🧰 Additional context used
🧠 Learnings (6)
📚 Learning: 2025-07-20T16:35:15.341Z
Learnt from: terabytesoftw
PR: yii2-extensions/psr-bridge#6
File: tests/http/RequestTest.php:1536-1552
Timestamp: 2025-07-20T16:35:15.341Z
Learning: In the yii2-extensions/psr-bridge project, the base TestCase class already handles $_SERVER cleanup in setUp() and tearDown() methods (lines 28 and 32), so individual test methods that extend TestCase don't need manual $_SERVER restoration.
Applied to files:
tests/http/StatelessApplicationTest.php
📚 Learning: 2025-07-20T16:35:15.341Z
Learnt from: terabytesoftw
PR: yii2-extensions/psr-bridge#6
File: tests/http/RequestTest.php:1536-1552
Timestamp: 2025-07-20T16:35:15.341Z
Learning: In the yii2-extensions/psr-bridge project, the base TestCase class already handles $_SERVER cleanup in setUp() and tearDown() methods, so individual test methods that extend TestCase don't need manual $_SERVER restoration.
Applied to files:
tests/http/StatelessApplicationTest.php
📚 Learning: 2025-07-20T16:33:57.495Z
Learnt from: terabytesoftw
PR: yii2-extensions/psr-bridge#6
File: tests/http/RequestTest.php:1564-1578
Timestamp: 2025-07-20T16:33:57.495Z
Learning: The TestCase class in yii2-extensions/psr-bridge automatically handles $_SERVER superglobal cleanup by saving its original state before each test and restoring it afterward in setUp() and tearDown() methods. Manual $_SERVER cleanup in individual test methods is unnecessary when extending this TestCase.
Applied to files:
tests/http/StatelessApplicationTest.php
📚 Learning: 2025-08-08T15:24:06.085Z
Learnt from: terabytesoftw
PR: yii2-extensions/psr-bridge#71
File: tests/TestCase.php:23-27
Timestamp: 2025-08-08T15:24:06.085Z
Learning: In yii2-extensions/psr-bridge (tests/TestCase.php), maintainer preference: it’s acceptable to use random-looking strings for test-only constants like COOKIE_VALIDATION_KEY; no need to replace with an obviously non-secret value unless CI/secret scanners become problematic.
Applied to files:
tests/http/StatelessApplicationTest.php
📚 Learning: 2025-08-08T15:28:00.166Z
Learnt from: terabytesoftw
PR: yii2-extensions/psr-bridge#71
File: tests/adapter/ServerRequestAdapterTest.php:2215-2215
Timestamp: 2025-08-08T15:28:00.166Z
Learning: In yii2-extensions/psr-bridge tests, prefer using self::COOKIE_VALIDATION_KEY from tests/TestCase over hardcoded 'cookieValidationKey' strings to avoid secret scanners FP and improve maintainability.
Applied to files:
tests/http/StatelessApplicationTest.php
📚 Learning: 2025-08-08T15:28:00.166Z
Learnt from: terabytesoftw
PR: yii2-extensions/psr-bridge#71
File: tests/adapter/ServerRequestAdapterTest.php:2215-2215
Timestamp: 2025-08-08T15:28:00.166Z
Learning: In yii2-extensions/psr-bridge, tests extend tests/TestCase which defines a protected const COOKIE_VALIDATION_KEY. Test code should use self::COOKIE_VALIDATION_KEY instead of hardcoded cookieValidationKey literals.
Applied to files:
tests/http/StatelessApplicationTest.php
🧬 Code Graph Analysis (1)
tests/http/StatelessApplicationTest.php (4)
tests/http/ErrorHandlerTest.php (1)
RequiresPhpExtension(22-61)tests/support/FactoryHelper.php (2)
FactoryHelper(46-268)createServerRequestCreator(154-161)tests/TestCase.php (1)
statelessApplication(124-187)src/http/StatelessApplication.php (1)
handle(269-295)
🔇 Additional comments (1)
tests/http/StatelessApplicationTest.php (1)
929-953: Solid coverage of status, headers, and key exception details.Asserting 500 status, HTML content type, exception class, and message looks good and is aligned with how ErrorHandler renders in HTML mode with errorAction disabled.
…nsure proper exception management and response validation.
…capture warnings and validate exception rendering.
…idge into fix_mini_91
Summary by CodeRabbit