Skip to content

Commit d80c7b5

Browse files
committed
fix: ControllerTestTrait::withUri() updates Request instance
1 parent 9752815 commit d80c7b5

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

system/Test/ControllerTestTrait.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,13 @@ public function withLogger($logger)
278278
*/
279279
public function withUri(string $uri)
280280
{
281-
$this->uri = new URI($uri);
281+
$factory = Services::siteurifactory();
282+
$this->uri = $factory->createFromString($uri);
283+
Services::injectMock('uri', $this->uri);
284+
285+
// Update the Request instance, because Request has the SiteURI instance.
286+
$this->request = Services::incomingrequest(null, false);
287+
Services::injectMock('request', $this->request);
282288

283289
return $this;
284290
}

user_guide_src/source/changelogs/v4.4.0.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ Bugs Fixed
347347
(e.g., **foo-bar**) and one URI for underscores (e.g., **foo_bar**). This bug
348348
has been fixed. Now the URI for underscores (**foo_bar**) is not accessible.
349349
- **Output Buffering:** Bug fix with output buffering.
350+
- **ControllerTestTrait:** ``ControllerTestTrait::withUri()`` creates a new Request
351+
instance with the URI. Because the Request instance should have the URI instance.
352+
Also if the hostname in the URI string is invalid with ``Config\App``, the valid
353+
hostname will be set.
350354

351355
See the repo's
352356
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

user_guide_src/source/testing/controllers.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ representing a valid URI:
101101

102102
It is a good practice to always provide the URI during testing to avoid surprises.
103103

104+
.. note:: Since v4.4.0, this method creates a new Request instance with the URI.
105+
Because the Request instance should have the URI instance. Also if the hostname
106+
in the URI string is invalid with ``Config\App``, the valid hostname will be
107+
set.
108+
104109
withBody($body)
105110
---------------
106111

0 commit comments

Comments
 (0)