Skip to content

Commit

Permalink
Merge pull request #8196 from kenjis/test-use-SiteURI
Browse files Browse the repository at this point in the history
test: update tests to use SiteURI
  • Loading branch information
kenjis authored Nov 14, 2023
2 parents a5ddb5e + 396dca9 commit db721a5
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 158 deletions.
15 changes: 8 additions & 7 deletions tests/system/API/ResponseTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use CodeIgniter\Format\FormatterInterface;
use CodeIgniter\Format\JSONFormatter;
use CodeIgniter\Format\XMLFormatter;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockIncomingRequest;
Expand All @@ -42,7 +42,7 @@ protected function setUp(): void
$this->formatter = new JSONFormatter();
}

protected function makeController(array $userConfig = [], string $uri = 'http://example.com', array $userHeaders = [])
protected function makeController(array $userConfig = [], string $routePath = '', array $userHeaders = [])
{
$config = new App();

Expand Down Expand Up @@ -73,7 +73,7 @@ protected function makeController(array $userConfig = [], string $uri = 'http://
Factories::injectMock('config', 'Cookie', $cookie);

if ($this->request === null) {
$this->request = new MockIncomingRequest($config, new URI($uri), null, new UserAgent());
$this->request = new MockIncomingRequest($config, new SiteURI($config, $routePath), null, new UserAgent());
$this->response = new MockResponse($config);
}

Expand Down Expand Up @@ -115,7 +115,7 @@ public function resetFormatter(): void
public function testNoFormatterJSON(): void
{
$this->formatter = null;
$controller = $this->makeController([], 'http://codeigniter.com', ['Accept' => 'application/json']);
$controller = $this->makeController([], '', ['Accept' => 'application/json']);

$this->invoke($controller, 'respondCreated', [['id' => 3], 'A Custom Reason']);

Expand All @@ -133,7 +133,7 @@ public function testNoFormatterJSON(): void
public function testNoFormatter(): void
{
$this->formatter = null;
$controller = $this->makeController([], 'http://codeigniter.com', ['Accept' => 'application/json']);
$controller = $this->makeController([], '', ['Accept' => 'application/json']);

$this->invoke($controller, 'respondCreated', ['A Custom Reason']);

Expand Down Expand Up @@ -484,8 +484,9 @@ private function tryValidContentType($mimeType, $contentType): void
$original = $_SERVER;
$_SERVER['CONTENT_TYPE'] = $mimeType;

$this->makeController([], 'http://codeigniter.com', ['Accept' => $mimeType]);
$this->makeController([], '', ['Accept' => $mimeType]);
$this->assertSame($mimeType, $this->request->getHeaderLine('Accept'), 'Request header...');

$this->response->setContentType($contentType);
$this->assertSame($contentType, $this->response->getHeaderLine('Content-Type'), 'Response header pre-response...');

Expand Down Expand Up @@ -554,7 +555,7 @@ public function testFormatByRequestNegotiateIfFormatIsNotJsonOrXML(): void
}
Factories::injectMock('config', 'Cookie', $cookie);

$request = new MockIncomingRequest($config, new URI($config->baseURL), null, new UserAgent());
$request = new MockIncomingRequest($config, new SiteURI($config), null, new UserAgent());
$response = new MockResponse($config);

$controller = new class ($request, $response) {
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Cache/ResponseCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use Config\App as AppConfig;
Expand Down Expand Up @@ -53,7 +53,7 @@ private function createIncomingRequest(

$appConfig ??= $this->appConfig;

$siteUri = new URI($appConfig->baseURL . $uri);
$siteUri = new SiteURI($appConfig, $uri);
if ($query !== []) {
$_GET = $_REQUEST = $query;
$siteUri->setQueryArray($query);
Expand Down
8 changes: 4 additions & 4 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Router\RouteCollection;
use CodeIgniter\Session\Handlers\FileHandler;
Expand Down Expand Up @@ -411,7 +411,7 @@ public function testOldInput(): void
$this->routes = $this->createRouteCollection();
Services::injectMock('routes', $this->routes);

$this->request = new MockIncomingRequest($this->config, new URI('http://example.com'), null, new UserAgent());
$this->request = new MockIncomingRequest($this->config, new SiteURI($this->config), null, new UserAgent());
Services::injectMock('request', $this->request);

// setup & ask for a redirect...
Expand Down Expand Up @@ -446,7 +446,7 @@ public function testOldInputSerializeData(): void
$this->routes = $this->createRouteCollection();
Services::injectMock('routes', $this->routes);

$this->request = new MockIncomingRequest($this->config, new URI('http://example.com'), null, new UserAgent());
$this->request = new MockIncomingRequest($this->config, new SiteURI($this->config), null, new UserAgent());
Services::injectMock('request', $this->request);

// setup & ask for a redirect...
Expand Down Expand Up @@ -481,7 +481,7 @@ public function testOldInputArray(): void
$this->routes = $this->createRouteCollection();
Services::injectMock('routes', $this->routes);

$this->request = new MockIncomingRequest($this->config, new URI('http://example.com'), null, new UserAgent());
$this->request = new MockIncomingRequest($this->config, new SiteURI($this->config), null, new UserAgent());
Services::injectMock('request', $this->request);

$locations = [
Expand Down
4 changes: 2 additions & 2 deletions tests/system/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Validation\Exceptions\ValidationException;
Expand Down Expand Up @@ -58,7 +58,7 @@ protected function setUp(): void
parent::setUp();

$this->config = new App();
$this->request = new IncomingRequest($this->config, new URI('https://somwhere.com'), null, new UserAgent());
$this->request = new IncomingRequest($this->config, new SiteURI($this->config), null, new UserAgent());
$this->response = new Response($this->config);
$this->logger = Services::logger();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Filters/InvalidCharsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Security\Exceptions\SecurityException;
use CodeIgniter\Test\CIUnitTestCase;
Expand Down Expand Up @@ -53,7 +53,7 @@ protected function tearDown(): void
private function createRequest(): IncomingRequest
{
$config = new MockAppConfig();
$uri = new URI();
$uri = new SiteURI($config);
$userAgent = new UserAgent();
$request = $this->getMockBuilder(IncomingRequest::class)
->setConstructorArgs([$config, $uri, null, $userAgent])
Expand Down
3 changes: 1 addition & 2 deletions tests/system/HTTP/IncomingRequestDetectingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ protected function setUp(): void
$_POST = $_GET = $_SERVER = $_REQUEST = $_ENV = $_COOKIE = $_SESSION = [];

// The URI object is not used in detectPath().
$origin = 'http://www.example.com/index.php/woot?code=good#pos';
$this->request = new IncomingRequest(new App(), new URI($origin), null, new UserAgent());
$this->request = new IncomingRequest(new App(), new SiteURI(new App(), 'woot?code=good#pos'), null, new UserAgent());
}

public function testPathDefault(): void
Expand Down
7 changes: 4 additions & 3 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function testSetValidLocales()
$config->defaultLocale = 'es';
$config->baseURL = 'http://example.com/';

$request = new IncomingRequest($config, new URI(), null, new UserAgent());
$request = new IncomingRequest($config, new SiteURI($config), null, new UserAgent());

$request->setValidLocales(['ja']);
$request->setLocale('ja');
Expand Down Expand Up @@ -901,7 +901,7 @@ public function testExtensionPHP($path, $detectPath): void

$_SERVER['REQUEST_URI'] = $path;
$_SERVER['SCRIPT_NAME'] = $path;
$request = new IncomingRequest($config, new URI($path), null, new UserAgent());
$request = new IncomingRequest($config, new SiteURI($config, $path), null, new UserAgent());
$this->assertSame($detectPath, $request->detectPath());
}

Expand All @@ -914,7 +914,8 @@ public function testGetPath(): void

public function testSetPath(): void
{
$request = new IncomingRequest(new App(), new URI(), null, new UserAgent());
$config = new App();
$request = new IncomingRequest($config, new SiteURI($config), null, new UserAgent());
$this->assertSame('', $request->getPath());

$request->setPath('foobar');
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Helpers/CookieHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use CodeIgniter\Cookie\Exceptions\CookieException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockResponse;
Expand Down Expand Up @@ -49,7 +49,7 @@ protected function setUp(): void

Services::injectMock('response', new MockResponse(new App()));
$this->response = Services::response();
$this->request = new IncomingRequest(new App(), new URI(), null, new UserAgent());
$this->request = new IncomingRequest(new App(), new SiteURI(new App()), null, new UserAgent());
Services::injectMock('request', $this->request);

helper('cookie');
Expand Down
7 changes: 4 additions & 3 deletions tests/system/Pager/PagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use CodeIgniter\Config\Factories;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Pager\Exceptions\PagerException;
Expand Down Expand Up @@ -53,7 +54,7 @@ private function createPager(string $requestUri): void

$request = new IncomingRequest(
$config,
new URI($config->baseURL . ltrim($requestUri, '/')),
new SiteURI($config, ltrim($requestUri, '/')),
'php://input',
new UserAgent()
);
Expand All @@ -70,7 +71,7 @@ public function testSetPathRemembersPath(): void

$details = $this->pager->getDetails();

$this->assertSame('foo/bar', $details['uri']->getPath());
$this->assertSame('foo/bar', $details['uri']->getRoutePath());
}

public function testGetDetailsRecognizesPageQueryVar(): void
Expand Down Expand Up @@ -474,7 +475,7 @@ public function testBasedURI(): void

$request = new IncomingRequest(
$config,
new URI(),
new SiteURI($config, 'x/y'),
'php://input',
new UserAgent()
);
Expand Down
6 changes: 3 additions & 3 deletions tests/system/RESTful/ResourceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use CodeIgniter\Format\XMLFormatter;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Model;
use CodeIgniter\Router\RouteCollection;
Expand Down Expand Up @@ -312,7 +312,7 @@ public function testJSONFormatOutput(): void
$resource = new MockResourceController();

$config = new App();
$uri = new URI();
$uri = new SiteURI($config);
$agent = new UserAgent();

$request = new IncomingRequest($config, $uri, '', $agent);
Expand Down Expand Up @@ -340,7 +340,7 @@ public function testXMLFormatOutput(): void
$resource = new MockResourceController();

$config = new App();
$uri = new URI();
$uri = new SiteURI($config);
$agent = new UserAgent();

$request = new IncomingRequest($config, $uri, '', $agent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use CodeIgniter\Config\Factories;
use CodeIgniter\Cookie\Cookie;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\URI;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockAppConfig;
Expand Down Expand Up @@ -74,7 +74,8 @@ public function testCSRFVerifySetNewCookie(): void
$_POST['foo'] = 'bar';
$_POST['csrf_test_name'] = $this->randomizedToken;

$request = new IncomingRequest(new MockAppConfig(), new URI('http://badurl.com'), null, new UserAgent());
$config = new MockAppConfig();
$request = new IncomingRequest($config, new SiteURI($config), null, new UserAgent());

$security = new Security($this->config);

Expand Down
Loading

0 comments on commit db721a5

Please sign in to comment.