-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Bug: IncomingRequest does not respect constructor param URI #7643
Comments
I do not see anything supernatural. CodeIgniter4/system/HTTP/IncomingRequest.php Line 178 in 93006e3
CodeIgniter4/system/HTTP/IncomingRequest.php Lines 269 to 273 in 93006e3
In my opinion, the Request object should be created based on superglobal arrays without specifying a URI or UserAgent in the constructor. And as a consequence, the URI object must be created based on the Request object. But if we need to specify a different URI, then PSR-7 has a withURI() method. |
The behavior is unexpected. |
I have a feeling that you made a mistake with the quoted fragment.
Every time? But the Request object is created only once. |
No, we create many times when writing test code. |
Once I already showed an example of creating a Request object through a factory. class Request
{
protected function __construct(array $query = [], array $param = [], array $cookie = [], array $server = [])
{
// ...
$this->uri = URI::createFromRequest($this);
}
public static function createFromGlobal(): Request
{
return new self($_GET, $_POST, $_COOKIE, $_SERVER);
}
} And as an example for tests it can look like this. public function test()
{
$appConfig = new App();
$request = Request::createFromGlobal()
->withUri(URI::createFromString($appConfig->baseURL . 'foo/bar'));
$this->assertSame(
'http://example.com/foo/bar',
(string) $request->getUri()
);
} The difference is not too big. |
The test code is a bit better, but still is not good. The need to change the state with |
If we forget to reset the superglobals, then most of our current tests will fail. The constructor in my example is protected, but if you make it public or add a factory that does not pass any data, then there will be no dependence on superglobal arrays. |
On
Change the comment: --- a/tests/system/HTTP/IncomingRequestTest.php
+++ b/tests/system/HTTP/IncomingRequestTest.php
@@ -21,7 +21,7 @@ use InvalidArgumentException;
use TypeError;
/**
- * @backupGlobals enabled
+ * @ backupGlobals enabled
*
* @internal
* The test class resets the superglobals: CodeIgniter4/tests/system/HTTP/IncomingRequestTest.php Lines 34 to 41 in 93006e3
Frankly, I am not sure if the current test code is correct. |
This may be much clearer. Reset the superglobals: --- a/tests/system/HTTP/URITest.php
+++ b/tests/system/HTTP/URITest.php
@@ -26,6 +26,13 @@ use Config\App;
*/
final class URITest extends CIUnitTestCase
{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $_POST = $_GET = $_SERVER = $_REQUEST = $_ENV = $_COOKIE = $_SESSION = [];
+ }
+
public function testConstructorSetsAllParts()
{
$uri = new URI('http://username:password@hostname:9090/path?arg=value#anchor');
|
Maybe I misunderstood you about resetting superglobals, but I meant |
Probably nobody knows the exact global state or superglobal values when running CI4 tests. It is very difficult to say that the global state before a test runs is really correct for the test. |
To opt out of the |
We need to remove all |
Closed by #7282 |
The following test fails on
develop
and4.4
.The text was updated successfully, but these errors were encountered: