diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 536b0e922e9c..0dc8acbb00e7 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -822,7 +822,7 @@ public function spoofRequestMethod() // Only allows PUT, PATCH, DELETE if (in_array($method, [Method::PUT, Method::PATCH, Method::DELETE], true)) { - $this->request = $this->request->setMethod($method); + $this->request = $this->request->withMethod($method); } } diff --git a/system/Commands/Utilities/Routes/FilterCollector.php b/system/Commands/Utilities/Routes/FilterCollector.php index 98963bba7d74..a1f85db56add 100644 --- a/system/Commands/Utilities/Routes/FilterCollector.php +++ b/system/Commands/Utilities/Routes/FilterCollector.php @@ -51,8 +51,7 @@ public function get(string $method, string $uri): array return ['before' => [], 'after' => []]; } - $request = service('incomingrequest', null, false); - $request->setMethod($method); + $request = single_service('incomingrequest', null)->withMethod($method); $router = $this->createRouter($request); $filters = $this->createFilters($request); @@ -77,8 +76,7 @@ public function getClasses(string $method, string $uri): array return ['before' => [], 'after' => []]; } - $request = service('incomingrequest', null, false); - $request->setMethod($method); + $request = single_service('incomingrequest', null)->withMethod($method); $router = $this->createRouter($request); $filters = $this->createFilters($request); @@ -95,8 +93,7 @@ public function getClasses(string $method, string $uri): array */ public function getRequiredFilters(): array { - $request = service('incomingrequest', null, false); - $request->setMethod(Method::GET); + $request = single_service('incomingrequest', null)->withMethod(Method::GET); $router = $this->createRouter($request); $filters = $this->createFilters($request); @@ -113,8 +110,7 @@ public function getRequiredFilters(): array */ public function getRequiredFilterClasses(): array { - $request = service('incomingrequest', null, false); - $request->setMethod(Method::GET); + $request = single_service('incomingrequest', null)->withMethod(Method::GET); $router = $this->createRouter($request); $filters = $this->createFilters($request); diff --git a/system/HTTP/CURLRequest.php b/system/HTTP/CURLRequest.php index 2f34c5d393fb..0e48ddd66ee6 100644 --- a/system/HTTP/CURLRequest.php +++ b/system/HTTP/CURLRequest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\HTTP; +use Closure; use CodeIgniter\Exceptions\InvalidArgumentException; use CodeIgniter\HTTP\Exceptions\HTTPException; use Config\App; @@ -313,7 +314,11 @@ public function setJSON($data) protected function parseOptions(array $options) { if (array_key_exists('baseURI', $options)) { - $this->baseURI = $this->baseURI->setURI($options['baseURI']); + $this->baseURI = Closure::bind( + static fn (URI $uri): URI => $uri->setUri($options['baseURI']), + null, + URI::class, + )($this->baseURI); unset($options['baseURI']); } diff --git a/system/HTTP/Message.php b/system/HTTP/Message.php index 5a490e09f780..bb319bf82bc1 100644 --- a/system/HTTP/Message.php +++ b/system/HTTP/Message.php @@ -60,39 +60,6 @@ public function getBody() return $this->body; } - /** - * Returns an array containing all headers. - * - * @return array An array of the request headers - * - * @deprecated Use Message::headers() to make room for PSR-7 - * - * @TODO Incompatible return value with PSR-7 - * - * @codeCoverageIgnore - */ - public function getHeaders(): array - { - return $this->headers(); - } - - /** - * Returns a single header object. If multiple headers with the same - * name exist, then will return an array of header objects. - * - * @return array|Header|null - * - * @deprecated Use Message::header() to make room for PSR-7 - * - * @TODO Incompatible return value with PSR-7 - * - * @codeCoverageIgnore - */ - public function getHeader(string $name) - { - return $this->header($name); - } - /** * Determines whether a header exists. */ diff --git a/system/HTTP/OutgoingRequest.php b/system/HTTP/OutgoingRequest.php index eba5babdf6bb..2f3eea45c798 100644 --- a/system/HTTP/OutgoingRequest.php +++ b/system/HTTP/OutgoingRequest.php @@ -77,20 +77,6 @@ public function getMethod(): string return $this->method; } - /** - * Sets the request method. Used when spoofing the request. - * - * @return $this - * - * @deprecated Use withMethod() instead for immutability - */ - public function setMethod(string $method) - { - $this->method = $method; - - return $this; - } - /** * Returns an instance with the specified method. * @@ -100,7 +86,8 @@ public function setMethod(string $method) */ public function withMethod($method) { - $request = clone $this; + $request = clone $this; + $request->method = $method; return $request; diff --git a/system/HTTP/Request.php b/system/HTTP/Request.php index 125646b6c7c7..2d22663e82e6 100644 --- a/system/HTTP/Request.php +++ b/system/HTTP/Request.php @@ -42,22 +42,6 @@ public function __construct($config = null) } } - /** - * Sets the request method. Used when spoofing the request. - * - * @return $this - * - * @deprecated 4.0.5 Use withMethod() instead for immutability - * - * @codeCoverageIgnore - */ - public function setMethod(string $method) - { - $this->method = $method; - - return $this; - } - /** * Returns an instance with the specified method. * diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php index 6adfe3794935..e322c0c6b46c 100644 --- a/system/HTTP/RequestTrait.php +++ b/system/HTTP/RequestTrait.php @@ -206,23 +206,6 @@ public function getServer($index = null, $filter = null, $flags = null) return $this->fetchGlobal('server', $index, $filter, $flags); } - /** - * Fetch an item from the $_ENV array. - * - * @param array|string|null $index Index for item to be fetched from $_ENV - * @param int|null $filter A filter name to be applied - * @param array|int|null $flags - * - * @return mixed - * - * @deprecated 4.4.4 This method does not work from the beginning. Use `env()`. - */ - public function getEnv($index = null, $filter = null, $flags = null) - { - // @phpstan-ignore-next-line - return $this->fetchGlobal('env', $index, $filter, $flags); - } - /** * Allows manually setting the value of PHP global, like $_GET, $_POST, etc. * diff --git a/system/HTTP/SiteURI.php b/system/HTTP/SiteURI.php index d6653b10f537..087c15659624 100644 --- a/system/HTTP/SiteURI.php +++ b/system/HTTP/SiteURI.php @@ -13,7 +13,6 @@ namespace CodeIgniter\HTTP; -use CodeIgniter\Exceptions\BadMethodCallException; use CodeIgniter\Exceptions\ConfigException; use CodeIgniter\HTTP\Exceptions\HTTPException; use Config\App; @@ -43,37 +42,6 @@ class SiteURI extends URI */ private readonly string $indexPage; - /** - * List of URI segments in baseURL and indexPage. - * - * If the URI is "http://localhost:8888/ci431/public/index.php/test?a=b", - * and the baseURL is "http://localhost:8888/ci431/public/", then: - * $baseSegments = [ - * 0 => 'ci431', - * 1 => 'public', - * 2 => 'index.php', - * ]; - */ - private array $baseSegments; - - /** - * List of URI segments after indexPage. - * - * The word "URI Segments" originally means only the URI path part relative - * to the baseURL. - * - * If the URI is "http://localhost:8888/ci431/public/index.php/test?a=b", - * and the baseURL is "http://localhost:8888/ci431/public/", then: - * $segments = [ - * 0 => 'test', - * ]; - * - * @var array - * - * @deprecated This property will be private. - */ - protected $segments; - /** * URI path relative to baseURL. * @@ -147,16 +115,14 @@ private function determineBaseURL( $uri = new URI($baseURL); - // Update scheme if ($scheme !== null && $scheme !== '') { - $uri->setScheme($scheme); + $uri = $uri->withScheme($scheme); } elseif ($configApp->forceGlobalSecureRequests) { - $uri->setScheme('https'); + $uri = $uri->withScheme('https'); } - // Update host if ($host !== null) { - $uri->setHost($host); + $uri = $uri->setHost($host); } return $uri; @@ -211,34 +177,12 @@ private function normalizeBaseURL(App $configApp): string private function setBasePath(): void { $this->basePathWithoutIndexPage = $this->baseURL->getPath(); - - $this->baseSegments = $this->convertToSegments($this->basePathWithoutIndexPage); - - if ($this->indexPage !== '') { - $this->baseSegments[] = $this->indexPage; - } - } - - /** - * @deprecated - */ - public function setBaseURL(string $baseURL): void - { - throw new BadMethodCallException('Cannot use this method.'); - } - - /** - * @deprecated - */ - public function setURI(?string $uri = null) - { - throw new BadMethodCallException('Cannot use this method.'); } /** * Returns the baseURL. * - * @interal + * @internal */ public function getBaseURL(): string { @@ -307,39 +251,18 @@ private function convertToSegments(string $path): array return ($tempPath === '') ? [] : explode('/', $tempPath); } - /** - * Sets the path portion of the URI based on segments. - * - * @return $this - * - * @deprecated This method will be private. - */ - public function refreshPath() - { - $allSegments = array_merge($this->baseSegments, $this->segments); - $this->path = '/' . $this->filterPath(implode('/', $allSegments)); - - if ($this->routePath === '/' && $this->path !== '/') { - $this->path .= '/'; - } - - $this->routePath = $this->filterPath(implode('/', $this->segments)); - - return $this; - } - /** * Saves our parts from a parse_url() call. * * @param array{ - * host?: string, - * user?: string, - * path?: string, - * query?: string, - * fragment?: string, - * scheme?: string, - * port?: int, - * pass?: string, + * host?: string, + * user?: string, + * path?: string, + * query?: string, + * fragment?: string, + * scheme?: string, + * port?: int, + * pass?: string, * } $parts */ protected function applyParts(array $parts): void @@ -364,11 +287,7 @@ protected function applyParts(array $parts): void $this->fragment = $parts['fragment']; } - if (isset($parts['scheme'])) { - $this->setScheme(rtrim($parts['scheme'], ':/')); - } else { - $this->setScheme('http'); - } + $this->scheme = $this->withScheme(rtrim($parts['scheme'] ?? 'http', ':/'))->getScheme(); if (isset($parts['port'])) { // Valid port numbers are enforced by earlier parse_url or setPort() diff --git a/system/HTTP/URI.php b/system/HTTP/URI.php index 6b4aeca8e56f..826562689f8b 100644 --- a/system/HTTP/URI.php +++ b/system/HTTP/URI.php @@ -13,7 +13,6 @@ namespace CodeIgniter\HTTP; -use CodeIgniter\Exceptions\BadMethodCallException; use CodeIgniter\Exceptions\InvalidArgumentException; use CodeIgniter\HTTP\Exceptions\HTTPException; use Config\App; @@ -37,22 +36,6 @@ class URI implements Stringable */ public const CHAR_UNRESERVED = 'a-zA-Z0-9_\-\.~'; - /** - * Current URI string - * - * @var string - * - * @deprecated 4.4.0 Not used. - */ - protected $uriString; - - /** - * The Current baseURL. - * - * @deprecated 4.4.0 Use SiteURI instead. - */ - private ?string $baseURL = null; - /** * List of URI segments. * @@ -251,33 +234,22 @@ public static function removeDotSegments(string $path): string } /** - * Constructor. - * - * @param string|null $uri The URI to parse. - * * @throws HTTPException * - * @TODO null for param $uri should be removed. - * See https://www.php-fig.org/psr/psr-17/#26-urifactoryinterface + * @see https://www.php-fig.org/psr/psr-17/#26-urifactoryinterface */ public function __construct(?string $uri = null) { - $this->setURI($uri); - } - - /** - * If $silent == true, then will not throw exceptions and will - * attempt to continue gracefully. - * - * @deprecated 4.4.0 Method not in PSR-7 - * - * @return URI - */ - public function setSilent(bool $silent = true) - { - $this->silent = $silent; + if ($uri === null) { + // @todo v4.8.0: Remove this deprecation warning and make $uri a string. + @trigger_error(sprintf( + 'Since v4.8.0, passing null to the %s constructor is deprecated. Please pass a string instead.', + static::class, + )); + $uri = ''; + } - return $this; + $this->setUri($uri); } /** @@ -286,7 +258,7 @@ public function setSilent(bool $silent = true) * * Note: Method not in PSR-7 * - * @return URI + * @return $this */ public function useRawQueryString(bool $raw = true) { @@ -298,15 +270,11 @@ public function useRawQueryString(bool $raw = true) /** * Sets and overwrites any current URI information. * - * @return URI - * * @throws HTTPException - * - * @deprecated 4.4.0 This method will be private. */ - public function setURI(?string $uri = null) + private function setUri(string $uri): self { - if ($uri === null) { + if ($uri === '') { return $this; } @@ -336,9 +304,9 @@ public function setURI(?string $uri = null) * The trailing ":" character is not part of the scheme and MUST NOT be * added. * - * @see https://tools.ietf.org/html/rfc3986#section-3.1 - * * @return string The URI scheme. + * + * @see https://tools.ietf.org/html/rfc3986#section-3.1 */ public function getScheme(): string { @@ -639,8 +607,9 @@ public function __toString(): string $path = $this->getPath(); $scheme = $this->getScheme(); - // If the hosts matches then assume this should be relative to baseURL - [$scheme, $path] = $this->changeSchemeAndPath($scheme, $path); + if ($scheme === 'http' && config(App::class)->forceGlobalSecureRequests) { + $scheme = 'https'; + } return static::createURIString( $scheme, @@ -651,41 +620,6 @@ public function __toString(): string ); } - /** - * Change the path (and scheme) assuming URIs with the same host as baseURL - * should be relative to the project's configuration. - * - * @return array{string, string} - * - * @deprecated This method will be deleted. - */ - private function changeSchemeAndPath(string $scheme, string $path): array - { - // Check if this is an internal URI - $config = config(App::class); - $baseUri = new self($config->baseURL); - - if ( - str_starts_with($this->getScheme(), 'http') - && $this->getHost() === $baseUri->getHost() - ) { - // Check for additional segments - $basePath = trim($baseUri->getPath(), '/') . '/'; - $trimPath = ltrim($path, '/'); - - if ($basePath !== '/' && ! str_starts_with($trimPath, $basePath)) { - $path = $basePath . $trimPath; - } - - // Check for forced HTTPS - if ($config->forceGlobalSecureRequests) { - $scheme = 'https'; - } - } - - return [$scheme, $path]; - } - /** * Parses the given string and saves the appropriate authority pieces. * @@ -711,26 +645,6 @@ public function setAuthority(string $str) return $this; } - /** - * Sets the scheme for this URI. - * - * Because of the large number of valid schemes we cannot limit this - * to only http or https. - * - * @see https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml - * - * @return $this - * - * @deprecated 4.4.0 Use `withScheme()` instead. - */ - public function setScheme(string $str) - { - $str = strtolower($str); - $this->scheme = preg_replace('#:(//)?$#', '', $str); - - return $this; - } - /** * Return an instance with the specified scheme. * @@ -835,42 +749,12 @@ public function setPath(string $path) return $this; } - /** - * Sets the current baseURL. - * - * @interal - * - * @deprecated Use SiteURI instead. - */ - public function setBaseURL(string $baseURL): void - { - $this->baseURL = $baseURL; - } - - /** - * Returns the current baseURL. - * - * @interal - * - * @deprecated Use SiteURI instead. - */ - public function getBaseURL(): string - { - if ($this->baseURL === null) { - throw new BadMethodCallException('The $baseURL is not set.'); - } - - return $this->baseURL; - } - /** * Sets the path portion of the URI based on segments. * * @return $this - * - * @deprecated This method will be private. */ - public function refreshPath() + private function refreshPath(): self { $this->path = $this->filterPath(implode('/', $this->segments)); @@ -1077,11 +961,7 @@ protected function applyParts(array $parts) $this->fragment = $parts['fragment']; } - if (isset($parts['scheme'])) { - $this->setScheme(rtrim($parts['scheme'], ':/')); - } else { - $this->setScheme('http'); - } + $this->scheme = $this->withScheme(rtrim($parts['scheme'] ?? 'http', ':/'))->getScheme(); if (isset($parts['port'])) { // Valid port numbers are enforced by earlier parse_url or setPort() @@ -1114,10 +994,10 @@ public function resolveRelativeURI(string $uri) * algorithm since it's already done by this line! */ $relative = new self(); - $relative->setURI($uri); + $relative->setUri($uri); if ($relative->getScheme() === $this->getScheme()) { - $relative->setScheme(''); + $relative = $relative->withScheme(''); } $transformed = clone $relative; @@ -1150,11 +1030,7 @@ public function resolveRelativeURI(string $uri) $transformed->setAuthority($this->getAuthority()); } - $transformed->setScheme($this->getScheme()); - - $transformed->setFragment($relative->getFragment()); - - return $transformed; + return $transformed->withScheme($this->getScheme())->setFragment($relative->getFragment()); } /** diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index e1c38edf3594..c89bb7ebdd88 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -427,7 +427,7 @@ protected function calculateCurrentPage(string $group) if (array_key_exists($group, $this->segment)) { try { $this->groups[$group]['currentPage'] = (int) $this->groups[$group]['currentUri'] - ->setSilent(false)->getSegment($this->segment[$group]); + ->getSegment($this->segment[$group]); } catch (HTTPException) { $this->groups[$group]['currentPage'] = 1; } diff --git a/system/Test/FeatureTestTrait.php b/system/Test/FeatureTestTrait.php index 618a2bcdb485..7c4660e5f814 100644 --- a/system/Test/FeatureTestTrait.php +++ b/system/Test/FeatureTestTrait.php @@ -338,10 +338,9 @@ protected function setupRequest(string $method, ?string $path = null): IncomingR Services::injectMock('uri', $uri); - $request = service('incomingrequest', $config, false); - - $request->setMethod($method); - $request->setProtocolVersion('1.1'); + $request = single_service('incomingrequest', $config) + ->withMethod($method) + ->setProtocolVersion('1.1'); if ($config->forceGlobalSecureRequests) { $_SERVER['HTTPS'] = 'test'; diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 509cf69643d2..bc2fae27dfd6 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -174,16 +174,6 @@ public function testCanGrabServerVars(): void $this->assertNull($this->request->getServer('TESTY')); } - public function testCanGrabEnvVars(): void - { - $server = $this->getPrivateProperty($this->request, 'globals'); - $server['env']['TEST'] = 5; - $this->setPrivateProperty($this->request, 'globals', $server); - - $this->assertSame('5', $this->request->getEnv('TEST')); - $this->assertNull($this->request->getEnv('TESTY')); - } - public function testCanGrabCookieVars(): void { service('superglobals')->setCookie('TEST', '5'); @@ -850,7 +840,7 @@ public function testGetFile(): void public function testSpoofing(): void { - $this->request->setMethod('WINK'); + $this->request = $this->request->withMethod('WINK'); $this->assertSame('WINK', $this->request->getMethod()); } diff --git a/tests/system/HTTP/SiteURITest.php b/tests/system/HTTP/SiteURITest.php index 1208edaf05b9..ca40c8e4d2ad 100644 --- a/tests/system/HTTP/SiteURITest.php +++ b/tests/system/HTTP/SiteURITest.php @@ -13,7 +13,6 @@ namespace CodeIgniter\HTTP; -use CodeIgniter\Exceptions\BadMethodCallException; use CodeIgniter\Exceptions\ConfigException; use CodeIgniter\HTTP\Exceptions\HTTPException; use CodeIgniter\Test\CIUnitTestCase; @@ -358,23 +357,6 @@ public static function provideSetPath(): iterable ]; } - public function testSetSegment(): void - { - $config = new App(); - - $uri = new SiteURI($config); - $uri->setPath('test/method'); - - $uri->setSegment(1, 'one'); - - $this->assertSame('http://example.com/index.php/one/method', (string) $uri); - $this->assertSame('one/method', $uri->getRoutePath()); - $this->assertSame('/index.php/one/method', $uri->getPath()); - $this->assertSame(['one', 'method'], $uri->getSegments()); - $this->assertSame('one', $uri->getSegment(1)); - $this->assertSame(2, $uri->getTotalSegments()); - } - public function testSetSegmentOutOfRange(): void { $this->expectException(HTTPException::class); @@ -386,17 +368,6 @@ public function testSetSegmentOutOfRange(): void $uri->setSegment(4, 'four'); } - public function testSetSegmentSilentOutOfRange(): void - { - $config = new App(); - $uri = new SiteURI($config); - $uri->setPath('one/method'); - $uri->setSilent(); - - $uri->setSegment(4, 'four'); - $this->assertSame(['one', 'method'], $uri->getSegments()); - } - public function testSetSegmentZero(): void { $this->expectException(HTTPException::class); @@ -408,24 +379,6 @@ public function testSetSegmentZero(): void $uri->setSegment(0, 'four'); } - public function testSetSegmentSubfolder(): void - { - $config = new App(); - $config->baseURL = 'http://example.com/ci4/'; - - $uri = new SiteURI($config); - $uri->setPath('test/method'); - - $uri->setSegment(1, 'one'); - - $this->assertSame('http://example.com/ci4/index.php/one/method', (string) $uri); - $this->assertSame('one/method', $uri->getRoutePath()); - $this->assertSame('/ci4/index.php/one/method', $uri->getPath()); - $this->assertSame(['one', 'method'], $uri->getSegments()); - $this->assertSame('one', $uri->getSegment(1)); - $this->assertSame(2, $uri->getTotalSegments()); - } - public function testGetRoutePath(): void { $config = new App(); @@ -472,26 +425,6 @@ public function testGetTotalSegments(): void $this->assertSame(0, $uri->getTotalSegments()); } - public function testSetURI(): void - { - $this->expectException(BadMethodCallException::class); - - $config = new App(); - $uri = new SiteURI($config); - - $uri->setURI('http://another.site.example.jp/'); - } - - public function testSetBaseURI(): void - { - $this->expectException(BadMethodCallException::class); - - $config = new App(); - $uri = new SiteURI($config); - - $uri->setBaseURL('http://another.site.example.jp/'); - } - public function testGetBaseURL(): void { $config = new App(); diff --git a/tests/system/HTTP/URITest.php b/tests/system/HTTP/URITest.php index c449d13af65c..1974e2545b20 100644 --- a/tests/system/HTTP/URITest.php +++ b/tests/system/HTTP/URITest.php @@ -68,14 +68,6 @@ public function testSegmentOutOfRange(): void $uri->getSegment(5); } - public function testSegmentOutOfRangeWithSilent(): void - { - $url = 'http://abc.com/a123/b/c'; - $uri = new URI($url); - - $this->assertSame('', $uri->setSilent()->getSegment(22)); - } - public function testSegmentOutOfRangeWithDefaultValue(): void { $this->expectException(HTTPException::class); @@ -85,40 +77,6 @@ public function testSegmentOutOfRangeWithDefaultValue(): void $uri->getSegment(22, 'something'); } - public function testSegmentOutOfRangeWithSilentAndDefaultValue(): void - { - $url = 'http://abc.com/a123/b/c'; - $uri = new URI($url); - - $this->assertSame('something', $uri->setSilent()->getSegment(22, 'something')); - } - - public function testSegmentsWithDefaultValueAndSilent(): void - { - $uri = new URI('http://hostname/path/to'); - $uri->setSilent(); - - $this->assertSame(['path', 'to'], $uri->getSegments()); - $this->assertSame('path', $uri->getSegment(1)); - $this->assertSame('to', $uri->getSegment(2, 'different')); - $this->assertSame('script', $uri->getSegment(3, 'script')); - $this->assertSame('', $uri->getSegment(3)); - - $this->assertSame(2, $uri->getTotalSegments()); - } - - public function testSegmentOutOfRangeWithDefaultValuesAndSilent(): void - { - $uri = new URI('http://hostname/path/to/script'); - $uri->setSilent(); - - $this->assertSame('', $uri->getSegment(22)); - $this->assertSame('something', $uri->getSegment(33, 'something')); - - $this->assertSame(3, $uri->getTotalSegments()); - $this->assertSame(['path', 'to', 'script'], $uri->getSegments()); - } - public function testCanCastAsString(): void { $url = 'http://username:password@hostname:9090/path?arg=value#anchor'; @@ -220,27 +178,6 @@ public function testMissingScheme(): void $this->assertSame($url, (string) $uri); } - public function testSchemeSub(): void - { - $url = 'example.com'; - $uri = new URI('http://' . $url); - $uri->setScheme('x'); - - $this->assertSame('x://' . $url, (string) $uri); - } - - public function testSetSchemeSetsValue(): void - { - $url = 'http://example.com/path'; - $uri = new URI($url); - - $uri->setScheme('https'); - - $this->assertSame('https', $uri->getScheme()); - $expected = 'https://example.com/path'; - $this->assertSame($expected, (string) $uri); - } - public function testWithScheme(): void { $url = 'example.com'; @@ -347,16 +284,6 @@ public function testSetPortInvalidValues(): void $uri->setPort(70000); } - public function testSetPortInvalidValuesSilent(): void - { - $url = 'http://example.com/path'; - $uri = new URI($url); - - $uri->setSilent()->setPort(70000); - - $this->assertNull($uri->getPort()); - } - public function testSetPortTooSmall(): void { $url = 'http://example.com/path'; @@ -383,9 +310,7 @@ public function testCatchesBadPort(): void { $this->expectException(HTTPException::class); - $url = 'http://username:password@hostname:90909/path?arg=value#anchor'; - $uri = new URI(); - $uri->setURI($url); + new URI('http://username:password@hostname:90909/path?arg=value#anchor'); } public function testSetPathSetsValue(): void @@ -593,16 +518,6 @@ public function testSetQueryThrowsErrorWhenFragmentPresent(): void $uri->setQuery('?key=value#fragment'); } - public function testSetQueryThrowsErrorWhenFragmentPresentSilent(): void - { - $url = 'http://example.com/path'; - $uri = new URI($url); - - $uri->setSilent()->setQuery('?key=value#fragment'); - - $this->assertSame('', $uri->getQuery()); - } - /** * @param string $url * @param string $expected @@ -1034,17 +949,6 @@ public function testSetBadSegment(): void $uri->setSegment(6, 'banana'); } - public function testSetBadSegmentSilent(): void - { - $base = 'http://example.com/foo/bar/baz'; - $uri = new URI($base); - $segments = $uri->getSegments(); - - $uri->setSilent()->setSegment(6, 'banana'); - - $this->assertSame($segments, $uri->getSegments()); - } - // Exploratory testing, investigating https://github.com/codeigniter4/CodeIgniter4/issues/2016 public function testBasedNoIndex(): void @@ -1174,25 +1078,14 @@ public function testEmptyURIPath(): void $this->assertSame(0, $uri->getTotalSegments()); } - public function testSetURI(): void + public function testInvalidURI(): void { $url = ':'; - $uri = new URI(); $this->expectException(HTTPException::class); $this->expectExceptionMessage(lang('HTTP.cannotParseURI', [$url])); - $uri->setURI($url); - } - - public function testSetURISilent(): void - { - $url = ':'; - $uri = new URI(); - - $uri->setSilent()->setURI($url); - - $this->assertTrue(true); + new URI($url); } public function testCreateURIStringNoArguments(): void diff --git a/tests/system/Router/Attributes/FilterTest.php b/tests/system/Router/Attributes/FilterTest.php index 33ee3ca89d19..dbdc225b7dd1 100644 --- a/tests/system/Router/Attributes/FilterTest.php +++ b/tests/system/Router/Attributes/FilterTest.php @@ -210,11 +210,16 @@ private function createMockRequest(string $method, string $path, string $query = $request = $this->getMockBuilder(IncomingRequest::class) ->setConstructorArgs([$config, $uri, null, $userAgent]) - ->onlyMethods(['isCLI']) + ->onlyMethods(['isCLI', 'getMethod', 'withMethod']) ->getMock(); $request->method('isCLI')->willReturn(false); - $request->setMethod($method); + $request->method('withMethod')->willReturnCallback(static function ($newMethod) use ($request) { + $newRequest = clone $request; + $newRequest->method('getMethod')->willReturn($newMethod); - return $request; + return $newRequest; + }); + + return $request->withMethod($method); } } diff --git a/tests/system/Router/Attributes/RestrictTest.php b/tests/system/Router/Attributes/RestrictTest.php index 556884d51c77..ba5290f79925 100644 --- a/tests/system/Router/Attributes/RestrictTest.php +++ b/tests/system/Router/Attributes/RestrictTest.php @@ -454,11 +454,16 @@ private function createMockRequest(string $method, string $path, string $host = $request = $this->getMockBuilder(IncomingRequest::class) ->setConstructorArgs([$config, $uri, null, $userAgent]) - ->onlyMethods(['isCLI']) + ->onlyMethods(['isCLI', 'getMethod', 'withMethod']) ->getMock(); $request->method('isCLI')->willReturn(false); - $request->setMethod($method); + $request->method('withMethod')->willReturnCallback(static function ($newMethod) use ($request) { + $newRequest = clone $request; + $newRequest->method('getMethod')->willReturn($newMethod); - return $request; + return $newRequest; + }); + + return $request->withMethod($method); } } diff --git a/tests/system/Router/RouteCollectionTest.php b/tests/system/Router/RouteCollectionTest.php index 6346697d206a..ce971b231026 100644 --- a/tests/system/Router/RouteCollectionTest.php +++ b/tests/system/Router/RouteCollectionTest.php @@ -153,7 +153,7 @@ public function testAddIgnoresDefaultNamespaceWhenExists(): void public function testAddWorksWithCurrentHTTPMethods(): void { - service('request')->setMethod(Method::GET); + Services::injectMock('request', service('request')->withMethod(Method::GET)); $routes = $this->getCollector(); @@ -185,7 +185,7 @@ public function testAddWithLeadingSlash(): void public function testMatchIgnoresInvalidHTTPMethods(): void { - service('request')->setMethod(Method::GET); + Services::injectMock('request', service('request')->withMethod(Method::GET)); $routes = $this->getCollector(); @@ -198,7 +198,7 @@ public function testMatchIgnoresInvalidHTTPMethods(): void public function testAddWorksWithArrayOFHTTPMethods(): void { - service('request')->setMethod(Method::POST); + Services::injectMock('request', service('request')->withMethod(Method::POST)); $routes = $this->getCollector(); @@ -719,7 +719,7 @@ public function testPresenterScaffoldsCorrectly(): void public function testResourcesWithCustomController(): void { - service('request')->setMethod(Method::GET); + Services::injectMock('request', service('request')->withMethod(Method::GET)); $routes = $this->getCollector(); $routes->resource('photos', ['controller' => '