From c08fa4169bfa1a821b3471d57896dfdcb9af34b7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 3 Oct 2022 11:12:40 +0900 Subject: [PATCH 01/10] test: remove $request->uri --- tests/system/Helpers/FormHelperTest.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index a464fbb488d5..e5666b2b83cf 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -29,15 +29,18 @@ protected function setUp(): void parent::setUp(); helper('form'); + + $this->resetServices(); } public function testFormOpenBasic() { + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); $config = new App(); $config->baseURL = ''; $config->indexPage = 'index.php'; $request = Services::request($config); - $request->uri = new URI('http://example.com/'); Services::injectMock('request', $request); @@ -67,11 +70,12 @@ public function testFormOpenBasic() public function testFormOpenHasLocale() { + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); $config = new App(); $config->baseURL = ''; $config->indexPage = 'index.php'; $request = Services::request($config); - $request->uri = new URI('http://example.com/'); Services::injectMock('request', $request); $expected = <<<'EOH' @@ -89,11 +93,12 @@ public function testFormOpenHasLocale() public function testFormOpenWithoutAction() { + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); $config = new App(); $config->baseURL = ''; $config->indexPage = 'index.php'; $request = Services::request($config); - $request->uri = new URI('http://example.com/'); Services::injectMock('request', $request); @@ -122,11 +127,12 @@ public function testFormOpenWithoutAction() public function testFormOpenWithoutMethod() { + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); $config = new App(); $config->baseURL = ''; $config->indexPage = 'index.php'; $request = Services::request($config); - $request->uri = new URI('http://example.com/'); Services::injectMock('request', $request); @@ -155,11 +161,12 @@ public function testFormOpenWithoutMethod() public function testFormOpenWithHidden() { + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); $config = new App(); $config->baseURL = ''; $config->indexPage = 'index.php'; $request = Services::request($config); - $request->uri = new URI('http://example.com/'); Services::injectMock('request', $request); @@ -195,11 +202,12 @@ public function testFormOpenWithHidden() public function testFormOpenMultipart() { + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); $config = new App(); $config->baseURL = ''; $config->indexPage = 'index.php'; $request = Services::request($config); - $request->uri = new URI('http://example.com/'); Services::injectMock('request', $request); From 9dd7e25b15f8b4312b35f3d8f0830fe0a6abc551 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 3 Oct 2022 11:24:32 +0900 Subject: [PATCH 02/10] test: extract method --- tests/system/Helpers/FormHelperTest.php | 52 +++++++------------------ 1 file changed, 15 insertions(+), 37 deletions(-) diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index e5666b2b83cf..c795e0a611d7 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -11,6 +11,7 @@ namespace CodeIgniter\Helpers; +use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\URI; use CodeIgniter\Test\CIUnitTestCase; use Config\App; @@ -33,15 +34,21 @@ protected function setUp(): void $this->resetServices(); } - public function testFormOpenBasic() + private function getRequest(): IncomingRequest { $uri = new URI('http://example.com/'); Services::injectMock('uri', $uri); + $config = new App(); $config->baseURL = ''; $config->indexPage = 'index.php'; - $request = Services::request($config); + return Services::request($config); + } + + public function testFormOpenBasic() + { + $request = $this->getRequest(); Services::injectMock('request', $request); $before = (new Filters())->globals['before']; @@ -70,14 +77,9 @@ public function testFormOpenBasic() public function testFormOpenHasLocale() { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - $config = new App(); - $config->baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - + $request = $this->getRequest(); Services::injectMock('request', $request); + $expected = <<<'EOH'
@@ -93,13 +95,7 @@ public function testFormOpenHasLocale() public function testFormOpenWithoutAction() { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - $config = new App(); - $config->baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - + $request = $this->getRequest(); Services::injectMock('request', $request); $before = (new Filters())->globals['before']; @@ -127,13 +123,7 @@ public function testFormOpenWithoutAction() public function testFormOpenWithoutMethod() { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - $config = new App(); - $config->baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - + $request = $this->getRequest(); Services::injectMock('request', $request); $before = (new Filters())->globals['before']; @@ -161,13 +151,7 @@ public function testFormOpenWithoutMethod() public function testFormOpenWithHidden() { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - $config = new App(); - $config->baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - + $request = $this->getRequest(); Services::injectMock('request', $request); $before = (new Filters())->globals['before']; @@ -202,13 +186,7 @@ public function testFormOpenWithHidden() public function testFormOpenMultipart() { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - $config = new App(); - $config->baseURL = ''; - $config->indexPage = 'index.php'; - $request = Services::request($config); - + $request = $this->getRequest(); Services::injectMock('request', $request); $before = (new Filters())->globals['before']; From c42b1bfc2670d598c6431e217fc0176efcf4ace9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 3 Oct 2022 12:21:33 +0900 Subject: [PATCH 03/10] test: fix incorrect assertions --- tests/system/Helpers/URLHelper/CurrentUrlTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/system/Helpers/URLHelper/CurrentUrlTest.php b/tests/system/Helpers/URLHelper/CurrentUrlTest.php index 0f316a305988..f0bc53a18446 100644 --- a/tests/system/Helpers/URLHelper/CurrentUrlTest.php +++ b/tests/system/Helpers/URLHelper/CurrentUrlTest.php @@ -145,7 +145,7 @@ public function testUriStringAbsolute() Services::injectMock('request', $request); - $this->assertSame('/assets/image.jpg', uri_string()); + $this->assertSame('assets/image.jpg', uri_string()); } public function testUriStringRelative() @@ -172,7 +172,7 @@ public function testUriStringNoTrailingSlashAbsolute() Services::injectMock('request', $request); - $this->assertSame('/assets/image.jpg', uri_string()); + $this->assertSame('assets/image.jpg', uri_string()); } public function testUriStringNoTrailingSlashRelative() @@ -220,7 +220,7 @@ public function testUriStringSubfolderAbsolute() Services::injectMock('request', $request); - $this->assertSame('/subfolder/assets/image.jpg', uri_string()); + $this->assertSame('subfolder/assets/image.jpg', uri_string()); } public function testUriStringSubfolderRelative() From 1e3c8ee0000f3b210b406df51bbd5a8acde6541d Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 3 Oct 2022 12:22:09 +0900 Subject: [PATCH 04/10] test: remove $request->uri --- .../Helpers/URLHelper/CurrentUrlTest.php | 71 ++++++++++++------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/tests/system/Helpers/URLHelper/CurrentUrlTest.php b/tests/system/Helpers/URLHelper/CurrentUrlTest.php index f0bc53a18446..4795687387a1 100644 --- a/tests/system/Helpers/URLHelper/CurrentUrlTest.php +++ b/tests/system/Helpers/URLHelper/CurrentUrlTest.php @@ -140,9 +140,10 @@ public function testUriStringAbsolute() $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/assets/image.jpg'); + $uri = new URI('http://example.com/assets/image.jpg'); + Services::injectMock('uri', $uri); + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame('assets/image.jpg', uri_string()); @@ -153,9 +154,10 @@ public function testUriStringRelative() $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/assets/image.jpg'); + $uri = new URI('http://example.com/assets/image.jpg'); + Services::injectMock('uri', $uri); + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame('assets/image.jpg', uri_string(true)); @@ -167,9 +169,11 @@ public function testUriStringNoTrailingSlashAbsolute() $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; $this->config->baseURL = 'http://example.com'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/assets/image.jpg'); + $uri = new URI('http://example.com/assets/image.jpg'); + Services::injectMock('uri', $uri); + + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame('assets/image.jpg', uri_string()); @@ -181,9 +185,11 @@ public function testUriStringNoTrailingSlashRelative() $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; $this->config->baseURL = 'http://example.com'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/assets/image.jpg'); + $uri = new URI('http://example.com/assets/image.jpg'); + Services::injectMock('uri', $uri); + + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame('assets/image.jpg', uri_string(true)); @@ -191,9 +197,10 @@ public function testUriStringNoTrailingSlashRelative() public function testUriStringEmptyAbsolute() { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame('/', uri_string()); @@ -201,9 +208,10 @@ public function testUriStringEmptyAbsolute() public function testUriStringEmptyRelative() { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame('', uri_string(true)); @@ -215,9 +223,11 @@ public function testUriStringSubfolderAbsolute() $_SERVER['REQUEST_URI'] = '/subfolder/assets/image.jpg'; $this->config->baseURL = 'http://example.com/subfolder/'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/subfolder/assets/image.jpg'); + $uri = new URI('http://example.com/subfolder/assets/image.jpg'); + Services::injectMock('uri', $uri); + + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame('subfolder/assets/image.jpg', uri_string()); @@ -230,8 +240,11 @@ public function testUriStringSubfolderRelative() $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; $this->config->baseURL = 'http://example.com/subfolder/'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/subfolder/assets/image.jpg'); + + $uri = new URI('http://example.com/subfolder/assets/image.jpg'); + Services::injectMock('uri', $uri); + + $request = Services::request($this->config); Services::injectMock('request', $request); @@ -287,8 +300,10 @@ public function testUrlIs(string $currentPath, string $testPath, bool $expected) $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/' . $currentPath; - $request = Services::request(); - $request->uri = new URI('http://example.com/' . $currentPath); + $uri = new URI('http://example.com/' . $currentPath); + Services::injectMock('uri', $uri); + + $request = Services::request(); Services::injectMock('request', $request); $this->assertSame($expected, url_is($testPath)); @@ -299,12 +314,15 @@ public function testUrlIs(string $currentPath, string $testPath, bool $expected) */ public function testUrlIsNoIndex(string $currentPath, string $testPath, bool $expected) { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/' . $currentPath; + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/' . $currentPath; + $this->config->indexPage = ''; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/' . $currentPath); + $uri = new URI('http://example.com/' . $currentPath); + Services::injectMock('uri', $uri); + + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame($expected, url_is($testPath)); @@ -318,10 +336,13 @@ public function testUrlIsWithSubfolder(string $currentPath, string $testPath, bo $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/' . $currentPath; $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; - $this->config->baseURL = 'http://example.com/subfolder/'; - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/subfolder/' . $currentPath); + $this->config->baseURL = 'http://example.com/subfolder/'; + + $uri = new URI('http://example.com/subfolder/' . $currentPath); + Services::injectMock('uri', $uri); + + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame($expected, url_is($testPath)); From 85df3ceca808b475ab34841b3ab43a700381b3c6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 3 Oct 2022 12:13:14 +0900 Subject: [PATCH 05/10] test: remove $request->uri --- .../system/Helpers/URLHelper/MiscUrlTest.php | 60 ++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/tests/system/Helpers/URLHelper/MiscUrlTest.php b/tests/system/Helpers/URLHelper/MiscUrlTest.php index 0fcb630e6350..8b5aaae18cb4 100644 --- a/tests/system/Helpers/URLHelper/MiscUrlTest.php +++ b/tests/system/Helpers/URLHelper/MiscUrlTest.php @@ -55,11 +55,12 @@ public function testPreviousURLUsesSessionFirst() $_SERVER['HTTP_REFERER'] = $uri1; $_SESSION['_ci_previous_url'] = $uri2; + $uri = new URI('http://example.com/public'); + Services::injectMock('uri', $uri); + // Since we're on a CLI, we must provide our own URI $this->config->baseURL = 'http://example.com/public'; $request = Services::request($this->config); - $request->uri = new URI('http://example.com/public'); - Services::injectMock('request', $request); $this->assertSame($uri2, previous_url()); @@ -71,11 +72,12 @@ public function testPreviousURLUsesRefererIfNeeded() $_SERVER['HTTP_REFERER'] = $uri1; + $uri = new URI('http://example.com/public'); + Services::injectMock('uri', $uri); + // Since we're on a CLI, we must provide our own URI $this->config->baseURL = 'http://example.com/public'; $request = Services::request($this->config); - $request->uri = new URI('http://example.com/public'); - Services::injectMock('request', $request); $this->assertSame($uri1, previous_url()); @@ -85,9 +87,10 @@ public function testPreviousURLUsesRefererIfNeeded() public function testIndexPage() { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame('index.php', index_page()); @@ -95,10 +98,11 @@ public function testIndexPage() public function testIndexPageAlt() { + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); + $this->config->indexPage = 'banana.php'; $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - Services::injectMock('request', $request); $this->assertSame('banana.php', index_page($this->config)); @@ -159,10 +163,12 @@ public function anchorNormalPatterns() */ public function testAnchor($expected = '', $uri = '', $title = '', $attributes = '') { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); + $uriObj = new URI('http://example.com/'); + Services::injectMock('uri', $uriObj); + $request = Services::request($this->config); Services::injectMock('request', $request); + $this->assertSame($expected, anchor($uri, $title, $attributes, $this->config)); } @@ -225,11 +231,13 @@ public function anchorNoindexPatterns() */ public function testAnchorNoindex($expected = '', $uri = '', $title = '', $attributes = '') { + $uriObj = new URI('http://example.com/'); + Services::injectMock('uri', $uriObj); + $this->config->indexPage = ''; $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - Services::injectMock('request', $request); + $this->assertSame($expected, anchor($uri, $title, $attributes, $this->config)); } @@ -282,11 +290,13 @@ public function anchorSubpagePatterns() */ public function testAnchorTargetted($expected = '', $uri = '', $title = '', $attributes = '') { + $uriObj = new URI('http://example.com/'); + Services::injectMock('uri', $uriObj); + $this->config->indexPage = ''; $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); - Services::injectMock('request', $request); + $this->assertSame($expected, anchor($uri, $title, $attributes, $this->config)); } @@ -328,10 +338,12 @@ public function anchorExamplePatterns() */ public function testAnchorExamples($expected = '', $uri = '', $title = '', $attributes = '') { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); + $uriObj = new URI('http://example.com/'); + Services::injectMock('uri', $uriObj); + $request = Services::request($this->config); Services::injectMock('request', $request); + $this->assertSame($expected, anchor($uri, $title, $attributes, $this->config)); } @@ -387,10 +399,12 @@ public function anchorPopupPatterns() */ public function testAnchorPopup($expected = '', $uri = '', $title = '', $attributes = false) { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); + $uriObj = new URI('http://example.com/'); + Services::injectMock('uri', $uriObj); + $request = Services::request($this->config); Services::injectMock('request', $request); + $this->assertSame($expected, anchor_popup($uri, $title, $attributes, $this->config)); } @@ -427,9 +441,10 @@ public function mailtoPatterns() */ public function testMailto($expected = '', $email = '', $title = '', $attributes = '') { - $request = Services::request($this->config); - $request->uri = new URI('http://example.com/'); + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); + $request = Services::request($this->config); Services::injectMock('request', $request); $this->assertSame($expected, mailto($email, $title, $attributes)); @@ -468,9 +483,10 @@ public function safeMailtoPatterns() */ public function testSafeMailto($expected = '', $email = '', $title = '', $attributes = '') { - $request = Services::incomingrequest($this->config); - $request->uri = new URI('http://example.com/'); + $uri = new URI('http://example.com/'); + Services::injectMock('uri', $uri); + $request = Services::incomingrequest($this->config); Services::injectMock('request', $request); $this->assertSame($expected, safe_mailto($email, $title, $attributes)); From 5a5a1be4d6f913d35b2113db881f50d9e75b5da7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 3 Oct 2022 12:15:07 +0900 Subject: [PATCH 06/10] test: remove $request->uri --- tests/system/Helpers/URLHelper/SiteUrlTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/system/Helpers/URLHelper/SiteUrlTest.php b/tests/system/Helpers/URLHelper/SiteUrlTest.php index 5a6d9f65995f..1c4b5fc6a75e 100644 --- a/tests/system/Helpers/URLHelper/SiteUrlTest.php +++ b/tests/system/Helpers/URLHelper/SiteUrlTest.php @@ -279,10 +279,11 @@ public function testBaseURLService() $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/ci/v4/x/y'; + $uri = new URI('http://example.com/ci/v4/x/y'); + Services::injectMock('uri', $uri); + $this->config->baseURL = 'http://example.com/ci/v4/'; $request = Services::request($this->config); - $request->uri = new URI('http://example.com/ci/v4/x/y'); - Services::injectMock('request', $request); $this->assertSame('http://example.com/ci/v4/index.php/controller/method', site_url('controller/method', null, $this->config)); From 8567b70862571ec13ad7aa801372f053a2cb9de9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 3 Oct 2022 13:08:19 +0900 Subject: [PATCH 07/10] test: extract method --- .../Helpers/URLHelper/CurrentUrlTest.php | 83 +++++++------------ 1 file changed, 29 insertions(+), 54 deletions(-) diff --git a/tests/system/Helpers/URLHelper/CurrentUrlTest.php b/tests/system/Helpers/URLHelper/CurrentUrlTest.php index 4795687387a1..0182ac9fbad0 100644 --- a/tests/system/Helpers/URLHelper/CurrentUrlTest.php +++ b/tests/system/Helpers/URLHelper/CurrentUrlTest.php @@ -140,13 +140,19 @@ public function testUriStringAbsolute() $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; - $uri = new URI('http://example.com/assets/image.jpg'); + $uri = 'http://example.com/assets/image.jpg'; + $this->setService($uri); + + $this->assertSame('assets/image.jpg', uri_string()); + } + + private function setService(string $uri): void + { + $uri = new URI($uri); Services::injectMock('uri', $uri); $request = Services::request($this->config); Services::injectMock('request', $request); - - $this->assertSame('assets/image.jpg', uri_string()); } public function testUriStringRelative() @@ -154,11 +160,8 @@ public function testUriStringRelative() $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/assets/image.jpg'; - $uri = new URI('http://example.com/assets/image.jpg'); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uri = 'http://example.com/assets/image.jpg'; + $this->setService($uri); $this->assertSame('assets/image.jpg', uri_string(true)); } @@ -170,11 +173,8 @@ public function testUriStringNoTrailingSlashAbsolute() $this->config->baseURL = 'http://example.com'; - $uri = new URI('http://example.com/assets/image.jpg'); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uri = 'http://example.com/assets/image.jpg'; + $this->setService($uri); $this->assertSame('assets/image.jpg', uri_string()); } @@ -186,33 +186,24 @@ public function testUriStringNoTrailingSlashRelative() $this->config->baseURL = 'http://example.com'; - $uri = new URI('http://example.com/assets/image.jpg'); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uri = 'http://example.com/assets/image.jpg'; + $this->setService($uri); $this->assertSame('assets/image.jpg', uri_string(true)); } public function testUriStringEmptyAbsolute() { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uri = 'http://example.com/'; + $this->setService($uri); $this->assertSame('/', uri_string()); } public function testUriStringEmptyRelative() { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uri = 'http://example.com/'; + $this->setService($uri); $this->assertSame('', uri_string(true)); } @@ -224,11 +215,8 @@ public function testUriStringSubfolderAbsolute() $this->config->baseURL = 'http://example.com/subfolder/'; - $uri = new URI('http://example.com/subfolder/assets/image.jpg'); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uri = 'http://example.com/subfolder/assets/image.jpg'; + $this->setService($uri); $this->assertSame('subfolder/assets/image.jpg', uri_string()); } @@ -241,12 +229,8 @@ public function testUriStringSubfolderRelative() $this->config->baseURL = 'http://example.com/subfolder/'; - $uri = new URI('http://example.com/subfolder/assets/image.jpg'); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - - Services::injectMock('request', $request); + $uri = 'http://example.com/subfolder/assets/image.jpg'; + $this->setService($uri); $this->assertSame('assets/image.jpg', uri_string(true)); } @@ -300,11 +284,8 @@ public function testUrlIs(string $currentPath, string $testPath, bool $expected) $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/' . $currentPath; - $uri = new URI('http://example.com/' . $currentPath); - Services::injectMock('uri', $uri); - - $request = Services::request(); - Services::injectMock('request', $request); + $uri = 'http://example.com/' . $currentPath; + $this->setService($uri); $this->assertSame($expected, url_is($testPath)); } @@ -319,11 +300,8 @@ public function testUrlIsNoIndex(string $currentPath, string $testPath, bool $ex $this->config->indexPage = ''; - $uri = new URI('http://example.com/' . $currentPath); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uri = 'http://example.com/' . $currentPath; + $this->setService($uri); $this->assertSame($expected, url_is($testPath)); } @@ -339,11 +317,8 @@ public function testUrlIsWithSubfolder(string $currentPath, string $testPath, bo $this->config->baseURL = 'http://example.com/subfolder/'; - $uri = new URI('http://example.com/subfolder/' . $currentPath); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uri = 'http://example.com/subfolder/' . $currentPath; + $this->setService($uri); $this->assertSame($expected, url_is($testPath)); } From 200cd8d1e9df5072a91f3d4b04cf93078a340a4f Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 3 Oct 2022 13:20:31 +0900 Subject: [PATCH 08/10] test: extract method --- .../system/Helpers/URLHelper/MiscUrlTest.php | 92 ++++++++----------- 1 file changed, 36 insertions(+), 56 deletions(-) diff --git a/tests/system/Helpers/URLHelper/MiscUrlTest.php b/tests/system/Helpers/URLHelper/MiscUrlTest.php index 8b5aaae18cb4..b213f6413e97 100644 --- a/tests/system/Helpers/URLHelper/MiscUrlTest.php +++ b/tests/system/Helpers/URLHelper/MiscUrlTest.php @@ -55,15 +55,22 @@ public function testPreviousURLUsesSessionFirst() $_SERVER['HTTP_REFERER'] = $uri1; $_SESSION['_ci_previous_url'] = $uri2; - $uri = new URI('http://example.com/public'); + $this->config->baseURL = 'http://example.com/public'; + + $uri = 'http://example.com/public'; + $this->setRequest($uri); + + $this->assertSame($uri2, previous_url()); + } + + private function setRequest(string $uri): void + { + $uri = new URI($uri); Services::injectMock('uri', $uri); // Since we're on a CLI, we must provide our own URI - $this->config->baseURL = 'http://example.com/public'; - $request = Services::request($this->config); + $request = Services::request($this->config); Services::injectMock('request', $request); - - $this->assertSame($uri2, previous_url()); } public function testPreviousURLUsesRefererIfNeeded() @@ -72,13 +79,10 @@ public function testPreviousURLUsesRefererIfNeeded() $_SERVER['HTTP_REFERER'] = $uri1; - $uri = new URI('http://example.com/public'); - Services::injectMock('uri', $uri); - - // Since we're on a CLI, we must provide our own URI $this->config->baseURL = 'http://example.com/public'; - $request = Services::request($this->config); - Services::injectMock('request', $request); + + $uri = 'http://example.com/public'; + $this->setRequest($uri); $this->assertSame($uri1, previous_url()); } @@ -87,23 +91,18 @@ public function testPreviousURLUsesRefererIfNeeded() public function testIndexPage() { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uri = 'http://example.com/'; + $this->setRequest($uri); $this->assertSame('index.php', index_page()); } public function testIndexPageAlt() { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - $this->config->indexPage = 'banana.php'; - $request = Services::request($this->config); - Services::injectMock('request', $request); + + $uri = 'http://example.com/'; + $this->setRequest($uri); $this->assertSame('banana.php', index_page($this->config)); } @@ -163,11 +162,8 @@ public function anchorNormalPatterns() */ public function testAnchor($expected = '', $uri = '', $title = '', $attributes = '') { - $uriObj = new URI('http://example.com/'); - Services::injectMock('uri', $uriObj); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uriString = 'http://example.com/'; + $this->setRequest($uriString); $this->assertSame($expected, anchor($uri, $title, $attributes, $this->config)); } @@ -231,12 +227,10 @@ public function anchorNoindexPatterns() */ public function testAnchorNoindex($expected = '', $uri = '', $title = '', $attributes = '') { - $uriObj = new URI('http://example.com/'); - Services::injectMock('uri', $uriObj); - $this->config->indexPage = ''; - $request = Services::request($this->config); - Services::injectMock('request', $request); + + $uriString = 'http://example.com/'; + $this->setRequest($uriString); $this->assertSame($expected, anchor($uri, $title, $attributes, $this->config)); } @@ -290,12 +284,10 @@ public function anchorSubpagePatterns() */ public function testAnchorTargetted($expected = '', $uri = '', $title = '', $attributes = '') { - $uriObj = new URI('http://example.com/'); - Services::injectMock('uri', $uriObj); - $this->config->indexPage = ''; - $request = Services::request($this->config); - Services::injectMock('request', $request); + + $uriString = 'http://example.com/'; + $this->setRequest($uriString); $this->assertSame($expected, anchor($uri, $title, $attributes, $this->config)); } @@ -338,11 +330,8 @@ public function anchorExamplePatterns() */ public function testAnchorExamples($expected = '', $uri = '', $title = '', $attributes = '') { - $uriObj = new URI('http://example.com/'); - Services::injectMock('uri', $uriObj); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uriString = 'http://example.com/'; + $this->setRequest($uriString); $this->assertSame($expected, anchor($uri, $title, $attributes, $this->config)); } @@ -399,11 +388,8 @@ public function anchorPopupPatterns() */ public function testAnchorPopup($expected = '', $uri = '', $title = '', $attributes = false) { - $uriObj = new URI('http://example.com/'); - Services::injectMock('uri', $uriObj); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uriString = 'http://example.com/'; + $this->setRequest($uriString); $this->assertSame($expected, anchor_popup($uri, $title, $attributes, $this->config)); } @@ -441,11 +427,8 @@ public function mailtoPatterns() */ public function testMailto($expected = '', $email = '', $title = '', $attributes = '') { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - - $request = Services::request($this->config); - Services::injectMock('request', $request); + $uriString = 'http://example.com/'; + $this->setRequest($uriString); $this->assertSame($expected, mailto($email, $title, $attributes)); } @@ -483,11 +466,8 @@ public function safeMailtoPatterns() */ public function testSafeMailto($expected = '', $email = '', $title = '', $attributes = '') { - $uri = new URI('http://example.com/'); - Services::injectMock('uri', $uri); - - $request = Services::incomingrequest($this->config); - Services::injectMock('request', $request); + $uriString = 'http://example.com/'; + $this->setRequest($uriString); $this->assertSame($expected, safe_mailto($email, $title, $attributes)); } From fb8be3e58e9256f9016cc7b8fb4f35a16a288f54 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 3 Oct 2022 13:25:54 +0900 Subject: [PATCH 09/10] test: refactor method --- tests/system/Helpers/FormHelperTest.php | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index c795e0a611d7..08df8bb16600 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -11,7 +11,6 @@ namespace CodeIgniter\Helpers; -use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\URI; use CodeIgniter\Test\CIUnitTestCase; use Config\App; @@ -34,7 +33,7 @@ protected function setUp(): void $this->resetServices(); } - private function getRequest(): IncomingRequest + private function setRequest(): void { $uri = new URI('http://example.com/'); Services::injectMock('uri', $uri); @@ -43,12 +42,13 @@ private function getRequest(): IncomingRequest $config->baseURL = ''; $config->indexPage = 'index.php'; - return Services::request($config); + $request = Services::request($config); + Services::injectMock('request', $request); } public function testFormOpenBasic() { - $request = $this->getRequest(); + $request = $this->setRequest(); Services::injectMock('request', $request); $before = (new Filters())->globals['before']; @@ -77,8 +77,7 @@ public function testFormOpenBasic() public function testFormOpenHasLocale() { - $request = $this->getRequest(); - Services::injectMock('request', $request); + $this->setRequest(); $expected = <<<'EOH' @@ -95,8 +94,7 @@ public function testFormOpenHasLocale() public function testFormOpenWithoutAction() { - $request = $this->getRequest(); - Services::injectMock('request', $request); + $this->setRequest(); $before = (new Filters())->globals['before']; if (in_array('csrf', $before, true) || array_key_exists('csrf', $before)) { @@ -123,8 +121,7 @@ public function testFormOpenWithoutAction() public function testFormOpenWithoutMethod() { - $request = $this->getRequest(); - Services::injectMock('request', $request); + $this->setRequest(); $before = (new Filters())->globals['before']; if (in_array('csrf', $before, true) || array_key_exists('csrf', $before)) { @@ -151,8 +148,7 @@ public function testFormOpenWithoutMethod() public function testFormOpenWithHidden() { - $request = $this->getRequest(); - Services::injectMock('request', $request); + $this->setRequest(); $before = (new Filters())->globals['before']; if (in_array('csrf', $before, true) || array_key_exists('csrf', $before)) { @@ -186,8 +182,7 @@ public function testFormOpenWithHidden() public function testFormOpenMultipart() { - $request = $this->getRequest(); - Services::injectMock('request', $request); + $this->setRequest(); $before = (new Filters())->globals['before']; if (in_array('csrf', $before, true) || array_key_exists('csrf', $before)) { From 1dad2a9caa7ed2be2716bc867735148656200d48 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 3 Oct 2022 13:37:05 +0900 Subject: [PATCH 10/10] test: remove @param mixed --- tests/system/Helpers/ArrayHelperTest.php | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/tests/system/Helpers/ArrayHelperTest.php b/tests/system/Helpers/ArrayHelperTest.php index 50c6f94f4bfc..7e1f1af21d71 100644 --- a/tests/system/Helpers/ArrayHelperTest.php +++ b/tests/system/Helpers/ArrayHelperTest.php @@ -209,8 +209,8 @@ public function testArrayDotIgnoresLastWildcard() /** * @dataProvider deepSearchProvider * - * @param mixed $key - * @param mixed $expected + * @param int|string $key + * @param array|string|null $expected */ public function testArrayDeepSearch($key, $expected) { @@ -247,12 +247,8 @@ public function testArrayDeepSearchReturnNullEmptyArray() /** * @dataProvider sortByMultipleKeysProvider - * - * @param mixed $data - * @param mixed $sortColumns - * @param mixed $expected */ - public function testArraySortByMultipleKeysWithArray($data, $sortColumns, $expected) + public function testArraySortByMultipleKeysWithArray(array $data, array $sortColumns, array $expected) { $success = array_sort_by_multiple_keys($data, $sortColumns); @@ -262,12 +258,8 @@ public function testArraySortByMultipleKeysWithArray($data, $sortColumns, $expec /** * @dataProvider sortByMultipleKeysProvider - * - * @param mixed $data - * @param mixed $sortColumns - * @param mixed $expected */ - public function testArraySortByMultipleKeysWithObjects($data, $sortColumns, $expected) + public function testArraySortByMultipleKeysWithObjects(array $data, array $sortColumns, array $expected) { // Morph to objects foreach ($data as $index => $dataSet) { @@ -282,12 +274,8 @@ public function testArraySortByMultipleKeysWithObjects($data, $sortColumns, $exp /** * @dataProvider sortByMultipleKeysProvider - * - * @param mixed $data - * @param mixed $sortColumns - * @param mixed $expected */ - public function testArraySortByMultipleKeysFailsEmptyParameter($data, $sortColumns, $expected) + public function testArraySortByMultipleKeysFailsEmptyParameter(array $data, array $sortColumns, array $expected) { // Both filled $success = array_sort_by_multiple_keys($data, $sortColumns);