-
-
Notifications
You must be signed in to change notification settings - Fork 1
test(http): Extract routing from ApplicationTest to ApplicationRoutingTest class.
#141
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
…tingTest` class.
WalkthroughAdds a new PHPUnit test class Changes
Sequence Diagram(s)No sequence diagrams provided — changes are a test-suite reorganization without control-flow or runtime behavior modifications. Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 💡 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). (2)
✨ 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 #141 +/- ##
===========================================
Coverage 100.00% 100.00%
Complexity 318 318
===========================================
Files 12 12
Lines 808 808
===========================================
Hits 808 808 ☔ View full report in Codecov by Sentry. |
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 (4)
tests/http/stateless/ApplicationRoutingTest.php (4)
28-29: Normalize REQUEST_URI and path assertions to start with “/”PSR-7 URIs conventionally use absolute paths. Normalizing REQUEST_URI to start with “/” makes the tests more portable across different ServerRequestCreator implementations and avoids brittle assumptions when switching creators or server environments. If you adopt this, update the path assertion accordingly.
Apply this diff:
- 'REQUEST_URI' => 'site/post', + 'REQUEST_URI' => '/site/post', @@ - 'REQUEST_URI' => 'site/get', + 'REQUEST_URI' => '/site/get', @@ - 'REQUEST_URI' => 'site/query/foo?q=1', + 'REQUEST_URI' => '/site/query/foo?q=1', @@ - 'REQUEST_URI' => 'site/update/123', + 'REQUEST_URI' => '/site/update/123', @@ - 'site/update/123', + '/site/update/123',Also applies to: 68-69, 103-104, 134-135, 159-160
45-52: Avoid heredoc for exact-JSON string comparisons (trailing newline risk)Heredoc adds a trailing newline and can introduce indentation surprises. Using a plain string removes this risk and keeps the assertion strict.
Apply this diff:
- self::assertSame( - <<<JSON - {"foo":"bar","a":{"b":"c"}} - JSON, - $response->getBody()->getContents(), + self::assertSame( + '{"foo":"bar","a":{"b":"c"}}', + $response->getBody()->getContents(), "Response body should match expected JSON string '{\"foo\":\"bar\",\"a\":{\"b\":\"c\"}}' for " . "'site/post' route.", ); @@ - self::assertSame( - <<<JSON - {"foo":"bar","a":{"b":"c"}} - JSON, - $response->getBody()->getContents(), + self::assertSame( + '{"foo":"bar","a":{"b":"c"}}', + $response->getBody()->getContents(), "Response body should match expected JSON string '{\"foo\":\"bar\",\"a\":{\"b\":\"c\"}}' for " . "'site/get' route.", );Optional: if you want robustness to key ordering/whitespace changes, use PHPUnit’s JSON comparator instead of exact string equality:
- self::assertSame('{"foo":"bar","a":{"b":"c"}}', $response->getBody()->getContents(), ...); + self::assertJsonStringEqualsJsonString('{"foo":"bar","a":{"b":"c"}}', (string) $response->getBody(), ...);Also applies to: 85-92
100-104: Redundant source of query paramsYou set q=1 in both $_GET and the REQUEST_URI. One source is enough; prefer the URI for clarity in these routing tests.
Apply this diff:
- $_GET = ['q' => '1']; - $_SERVER = [ + $_SERVER = [ 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => 'site/query/foo?q=1', + 'REQUEST_URI' => '/site/query/foo?q=1', ];
20-35: Reduce global-state coupling: build requests explicitly instead of using superglobalsThese tests currently rely on mutating superglobals and createFromGlobals(). That’s fine, but it’s slightly brittle and impedes parallelization. Constructing ServerRequest instances directly yields cleaner, side-effect-free tests and mirrors how the app would be used in production.
Apply this diff to create explicit requests (pattern shown for all four tests):
@@ - $_POST = [ - 'foo' => 'bar', - 'a' => [ - 'b' => 'c', - ], - ]; - $_SERVER = [ - 'REQUEST_METHOD' => 'POST', - 'REQUEST_URI' => 'site/post', - ]; - $app = $this->statelessApplication(); - - $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + $request = FactoryHelper::createRequest( + method: 'POST', + uri: '/site/post', + headers: ['Content-Type' => 'application/x-www-form-urlencoded'], + parsedBody: [ + 'foo' => 'bar', + 'a' => ['b' => 'c'], + ], + ); + $response = $app->handle($request); @@ - $_GET = [ - 'foo' => 'bar', - 'a' => [ - 'b' => 'c', - ], - ]; - $_SERVER = [ - 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => 'site/get', - ]; - $app = $this->statelessApplication(); - - $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + $request = FactoryHelper::createRequest( + method: 'GET', + uri: '/site/get?foo=bar&a[b]=c', + ); + $response = $app->handle($request); @@ - $_GET = ['q' => '1']; - $_SERVER = [ - 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => 'site/query/foo?q=1', - ]; - $app = $this->statelessApplication(); - - $response = $app->handle(FactoryHelper::createServerRequestCreator()->createFromGlobals()); + $request = FactoryHelper::createRequest( + method: 'GET', + uri: '/site/query/foo?q=1', + ); + $response = $app->handle($request); @@ - $_SERVER = [ - 'REQUEST_METHOD' => 'GET', - 'REQUEST_URI' => 'site/update/123', - ]; - - $request = FactoryHelper::createServerRequestCreator()->createFromGlobals(); - $app = $this->statelessApplication(); - + $request = FactoryHelper::createRequest( + method: 'GET', + uri: '/site/update/123', + ); $response = $app->handle($request);If you keep createFromGlobals(), consider marking this test group as non-parallel in your phpunit.xml to avoid cross-test interference. See the follow-up below.
Also applies to: 60-74, 100-109, 132-142
📜 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)
tests/http/stateless/ApplicationRoutingTest.php(1 hunks)tests/http/stateless/ApplicationTest.php(0 hunks)
💤 Files with no reviewable changes (1)
- tests/http/stateless/ApplicationTest.php
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-06T22:52:05.608Z
Learnt from: terabytesoftw
PR: yii2-extensions/psr-bridge#64
File: tests/http/StatelessApplicationTest.php:1939-1967
Timestamp: 2025-08-06T22:52:05.608Z
Learning: In yii2-extensions/psr-bridge tests, when testing specific component methods like Request::resolve(), it's necessary to call $app->handle($request) first to initialize all application components before testing the method in isolation. This ensures proper component lifecycle initialization.
Applied to files:
tests/http/stateless/ApplicationRoutingTest.php
🧬 Code graph analysis (1)
tests/http/stateless/ApplicationRoutingTest.php (3)
tests/support/FactoryHelper.php (2)
FactoryHelper(46-268)createServerRequestCreator(154-161)tests/TestCase.php (2)
TestCase(23-227)statelessApplication(134-197)src/http/StatelessApplication.php (1)
handle(269-295)
⏰ 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). (2)
- GitHub Check: mutation / PHP 8.3-ubuntu-latest
- GitHub Check: mutation / PHP 8.3-ubuntu-latest
🔇 Additional comments (1)
tests/http/stateless/ApplicationRoutingTest.php (1)
12-14: Good extraction and focused scopeCreating a dedicated ApplicationRoutingTest and grouping it under http clarifies intent and keeps routing concerns isolated from broader application tests. The tests also correctly go through $app->handle($request), which aligns with our prior learning about initializing components before exercising request-dependent logic.
Summary by CodeRabbit