diff --git a/tests/system/CommonSingleServiceTest.php b/tests/system/CommonSingleServiceTest.php index 2a27c9b8486f..f3e3bb57b40b 100644 --- a/tests/system/CommonSingleServiceTest.php +++ b/tests/system/CommonSingleServiceTest.php @@ -75,29 +75,6 @@ public function testSingleServiceWithAtLeastOneParamSupplied(string $service): v } } - public function testSingleServiceWithAllParamsSupplied(): void - { - $cache1 = single_service('cache', null, true); - $cache2 = single_service('cache', null, true); - - assert($cache1 !== null); - assert($cache2 !== null); - - // Assert that even passing true as last param this will - // not create a shared instance. - $this->assertInstanceOf($cache1::class, $cache2); - $this->assertNotSame($cache1, $cache2); - } - - public function testSingleServiceWithGibberishGiven(): void - { - $this->assertNull(single_service('foo')); // @phpstan-ignore codeigniter.unknownServiceMethod - $this->assertNull(single_service('bar')); // @phpstan-ignore codeigniter.unknownServiceMethod - $this->assertNull(single_service('baz')); // @phpstan-ignore codeigniter.unknownServiceMethod - $this->assertNull(single_service('caches')); // @phpstan-ignore codeigniter.unknownServiceMethod - $this->assertNull(single_service('timers')); // @phpstan-ignore codeigniter.unknownServiceMethod - } - /** * @return iterable */ @@ -137,4 +114,27 @@ public static function provideServiceNames(): iterable yield from $services; } + + public function testSingleServiceWithAllParamsSupplied(): void + { + $cache1 = single_service('cache', null, true); + $cache2 = single_service('cache', null, true); + + assert($cache1 !== null); + assert($cache2 !== null); + + // Assert that even passing true as last param this will + // not create a shared instance. + $this->assertInstanceOf($cache1::class, $cache2); + $this->assertNotSame($cache1, $cache2); + } + + public function testSingleServiceWithGibberishGiven(): void + { + $this->assertNull(single_service('foo')); // @phpstan-ignore codeigniter.unknownServiceMethod + $this->assertNull(single_service('bar')); // @phpstan-ignore codeigniter.unknownServiceMethod + $this->assertNull(single_service('baz')); // @phpstan-ignore codeigniter.unknownServiceMethod + $this->assertNull(single_service('caches')); // @phpstan-ignore codeigniter.unknownServiceMethod + $this->assertNull(single_service('timers')); // @phpstan-ignore codeigniter.unknownServiceMethod + } } diff --git a/tests/system/Config/MimesTest.php b/tests/system/Config/MimesTest.php index bcf5923f5b9d..0b22a45e5735 100644 --- a/tests/system/Config/MimesTest.php +++ b/tests/system/Config/MimesTest.php @@ -24,6 +24,12 @@ #[Group('Others')] final class MimesTest extends CIUnitTestCase { + #[DataProvider('provideGuessExtensionFromType')] + public function testGuessExtensionFromType(?string $expected, string $mime): void + { + $this->assertSame($expected, Mimes::guessExtensionFromType($mime)); + } + public static function provideGuessExtensionFromType(): iterable { return [ @@ -50,10 +56,10 @@ public static function provideGuessExtensionFromType(): iterable ]; } - #[DataProvider('provideGuessExtensionFromType')] - public function testGuessExtensionFromType(?string $expected, string $mime): void + #[DataProvider('provideGuessTypeFromExtension')] + public function testGuessTypeFromExtension(?string $expected, string $ext): void { - $this->assertSame($expected, Mimes::guessExtensionFromType($mime)); + $this->assertSame($expected, Mimes::guessTypeFromExtension($ext)); } public static function provideGuessTypeFromExtension(): iterable @@ -81,10 +87,4 @@ public static function provideGuessTypeFromExtension(): iterable ], ]; } - - #[DataProvider('provideGuessTypeFromExtension')] - public function testGuessTypeFromExtension(?string $expected, string $ext): void - { - $this->assertSame($expected, Mimes::guessTypeFromExtension($ext)); - } } diff --git a/tests/system/Config/ServicesTest.php b/tests/system/Config/ServicesTest.php index 162d95705933..250474cbc0ea 100644 --- a/tests/system/Config/ServicesTest.php +++ b/tests/system/Config/ServicesTest.php @@ -263,7 +263,7 @@ public function testNewSessionWithNullConfig(): void $this->assertInstanceOf(Session::class, $actual); } - #[DataProvider('provideNewSessionInvalid')] + #[DataProvider('provideNewSessionWithInvalidHandler')] #[PreserveGlobalState(false)] #[RunInSeparateProcess] public function testNewSessionWithInvalidHandler(string $driver): void @@ -277,6 +277,18 @@ public function testNewSessionWithInvalidHandler(string $driver): void Services::session($config, false); } + /** + * @return iterable + */ + public static function provideNewSessionWithInvalidHandler(): iterable + { + yield 'just a string' => ['file']; + + yield 'inexistent class' => ['Foo']; + + yield 'other class' => [self::class]; + } + #[PreserveGlobalState(false)] #[RunInSeparateProcess] public function testNewSessionWithInvalidDatabaseHandler(): void @@ -296,18 +308,6 @@ public function testNewSessionWithInvalidDatabaseHandler(): void Services::session($config, false); } - /** - * @return iterable - */ - public static function provideNewSessionInvalid(): iterable - { - yield 'just a string' => ['file']; - - yield 'inexistent class' => ['Foo']; - - yield 'other class' => [self::class]; - } - #[PreserveGlobalState(false)] #[RunInSeparateProcess] public function testCallStatic(): void diff --git a/tests/system/DataConverter/DataConverterTest.php b/tests/system/DataConverter/DataConverterTest.php index 0e6e85a488c9..10a35e6909fc 100644 --- a/tests/system/DataConverter/DataConverterTest.php +++ b/tests/system/DataConverter/DataConverterTest.php @@ -50,16 +50,6 @@ public function testConvertDataFromDB(array $types, array $dbData, array $expect $this->assertSame($expected, $data); } - #[DataProvider('provideConvertDataToDB')] - public function testConvertDataToDB(array $types, array $phpData, array $expected): void - { - $converter = $this->createDataConverter($types); - - $data = $converter->toDataSource($phpData); - - $this->assertSame($expected, $data); - } - public static function provideConvertDataFromDB(): iterable { yield from [ @@ -206,6 +196,16 @@ public static function provideConvertDataFromDB(): iterable ]; } + #[DataProvider('provideConvertDataToDB')] + public function testConvertDataToDB(array $types, array $phpData, array $expected): void + { + $converter = $this->createDataConverter($types); + + $data = $converter->toDataSource($phpData); + + $this->assertSame($expected, $data); + } + public static function provideConvertDataToDB(): iterable { yield from [ diff --git a/tests/system/Database/BaseQueryTest.php b/tests/system/Database/BaseQueryTest.php index 3c48e2526652..c5265b85dfa7 100644 --- a/tests/system/Database/BaseQueryTest.php +++ b/tests/system/Database/BaseQueryTest.php @@ -107,6 +107,19 @@ public function testSwapPrefix(): void $this->assertSame($newSQL, $query->getQuery()); } + /** + * @param mixed $expected + * @param mixed $sql + */ + #[DataProvider('provideIsWriteType')] + public function testIsWriteType($expected, $sql): void + { + $query = new Query($this->db); + + $query->setQuery($sql); + $this->assertSame($expected, $query->isWriteType()); + } + public static function provideIsWriteType(): iterable { return [ @@ -185,19 +198,6 @@ public static function provideIsWriteType(): iterable ]; } - /** - * @param mixed $expected - * @param mixed $sql - */ - #[DataProvider('provideIsWriteType')] - public function testIsWriteType($expected, $sql): void - { - $query = new Query($this->db); - - $query->setQuery($sql); - $this->assertSame($expected, $query->isWriteType()); - } - public function testSingleBindingOutsideOfArray(): void { $query = new Query($this->db); @@ -579,6 +579,19 @@ public function testSwapPrefixAfterGetQuery(): void $this->assertSame($expected, $query->getQuery()); } + /** + * @param mixed $expected + * @param mixed $sql + */ + #[DataProvider('provideHighlightQueryKeywords')] + public function testHighlightQueryKeywords($expected, $sql): void + { + $query = new Query($this->db); + $query->setQuery($sql); + + $this->assertSame($expected, $query->debugToolbarDisplay()); + } + public static function provideHighlightQueryKeywords(): iterable { return [ @@ -596,17 +609,4 @@ public static function provideHighlightQueryKeywords(): iterable ], ]; } - - /** - * @param mixed $expected - * @param mixed $sql - */ - #[DataProvider('provideHighlightQueryKeywords')] - public function testHighlightQueryKeywords($expected, $sql): void - { - $query = new Query($this->db); - $query->setQuery($sql); - - $this->assertSame($expected, $query->debugToolbarDisplay()); - } } diff --git a/tests/system/Database/Builder/WhenTest.php b/tests/system/Database/Builder/WhenTest.php index 87f9b3ff233d..2fc4966accb8 100644 --- a/tests/system/Database/Builder/WhenTest.php +++ b/tests/system/Database/Builder/WhenTest.php @@ -206,7 +206,7 @@ public function testWhenNotRunsDefaultCallbackBasedOnCondition(mixed $condition, /** * @return array */ - public static function provideConditionValues(): array + public static function provideConditionValues(): iterable { return [ 'false' => [false, true], // [condition, expectedDefaultCallbackRuns] diff --git a/tests/system/Database/Builder/WhereTest.php b/tests/system/Database/Builder/WhereTest.php index 57980f28ee8c..2d0c2a3e1233 100644 --- a/tests/system/Database/Builder/WhereTest.php +++ b/tests/system/Database/Builder/WhereTest.php @@ -395,14 +395,6 @@ public function testWhereInSubQuery(): void $this->assertSame($expectedSQL, str_replace("\n", ' ', $builder->getCompiledSelect())); } - public static function provideWhereInvalidKeyThrowInvalidArgumentException(): iterable - { - return [ - 'null' => [null], - 'empty string' => [''], - ]; - } - /** * @param mixed $key */ @@ -415,12 +407,11 @@ public function testWhereInvalidKeyThrowInvalidArgumentException($key): void $builder->whereIn($key, ['Politician', 'Accountant']); } - public static function provideWhereInEmptyValuesThrowInvalidArgumentException(): iterable + public static function provideWhereInvalidKeyThrowInvalidArgumentException(): iterable { return [ - 'null' => [null], - 'not array' => ['not array'], - 'not instanceof \Closure' => [new stdClass()], + 'null' => [null], + 'empty string' => [''], ]; } @@ -436,6 +427,15 @@ public function testWhereInEmptyValuesThrowInvalidArgumentException($values): vo $builder->whereIn('name', $values); } + public static function provideWhereInEmptyValuesThrowInvalidArgumentException(): iterable + { + return [ + 'null' => [null], + 'not array' => ['not array'], + 'not instanceof \Closure' => [new stdClass()], + ]; + } + public function testWhereNotIn(): void { $builder = $this->db->table('jobs'); diff --git a/tests/system/Database/Live/LikeTest.php b/tests/system/Database/Live/LikeTest.php index 9591eef72ea8..c03dcd8d096c 100644 --- a/tests/system/Database/Live/LikeTest.php +++ b/tests/system/Database/Live/LikeTest.php @@ -76,7 +76,7 @@ public function testLikeCaseInsensitive(): void $this->assertSame('Developer', $job->name); } - #[DataProvider('provideMultibyteCharacters')] + #[DataProvider('provideLikeCaseInsensitiveWithMultibyteCharacter')] public function testLikeCaseInsensitiveWithMultibyteCharacter(string $match, string $result): void { $wai = $this->db->table('without_auto_increment')->like('value', $match, 'both', null, true)->get(); @@ -88,7 +88,7 @@ public function testLikeCaseInsensitiveWithMultibyteCharacter(string $match, str /** * @return iterable */ - public static function provideMultibyteCharacters(): iterable + public static function provideLikeCaseInsensitiveWithMultibyteCharacter(): iterable { yield from [ 'polish' => ['ŁĄ', 'multibyte characters pl'], diff --git a/tests/system/Email/EmailTest.php b/tests/system/Email/EmailTest.php index 63e82a0a72ba..8d865b58ddeb 100644 --- a/tests/system/Email/EmailTest.php +++ b/tests/system/Email/EmailTest.php @@ -39,14 +39,6 @@ public function testEmailValidation(): void $this->assertStringContainsString('Invalid email address: "invalid"', $email->printDebugger()); } - public static function provideEmailSendWithClearance(): iterable - { - return [ - 'autoclear' => [true], - 'not autoclear' => [false], - ]; - } - /** * @param bool $autoClear */ @@ -64,6 +56,14 @@ public function testEmailSendWithClearance($autoClear): void } } + public static function provideEmailSendWithClearance(): iterable + { + return [ + 'autoclear' => [true], + 'not autoclear' => [false], + ]; + } + public function testEmailSendStoresArchive(): void { $email = $this->createMockEmail(); diff --git a/tests/system/Files/FileTest.php b/tests/system/Files/FileTest.php index c74c6b63ffeb..8975ba0a6fd2 100644 --- a/tests/system/Files/FileTest.php +++ b/tests/system/Files/FileTest.php @@ -139,6 +139,27 @@ public function testGetSizeMetric(FileSizeUnit $unit): void $this->assertSame($size, $file->getSizeByMetricUnit($unit)); } + /** + * @return array> + */ + public static function provideGetSizeData(): iterable + { + return [ + 'returns KB binary' => [ + FileSizeUnit::KB, + ], + 'returns MB binary' => [ + FileSizeUnit::MB, + ], + 'returns GB binary' => [ + FileSizeUnit::GB, + ], + 'returns TB binary' => [ + FileSizeUnit::TB, + ], + ]; + } + public function testGetSizeMetricBytes(): void { $file = new File(SYSTEMPATH . 'Common.php'); @@ -168,25 +189,4 @@ public function testGetDestination(): void unlink(SYSTEMPATH . 'Common_Copy.php'); unlink(SYSTEMPATH . 'Common_Copy_5.php'); } - - /** - * @return array> - */ - public static function provideGetSizeData(): array - { - return [ - 'returns KB binary' => [ - FileSizeUnit::KB, - ], - 'returns MB binary' => [ - FileSizeUnit::MB, - ], - 'returns GB binary' => [ - FileSizeUnit::GB, - ], - 'returns TB binary' => [ - FileSizeUnit::TB, - ], - ]; - } } diff --git a/tests/system/Filters/FiltersTest.php b/tests/system/Filters/FiltersTest.php index a6ef8abede3e..b2daf0ec2b07 100644 --- a/tests/system/Filters/FiltersTest.php +++ b/tests/system/Filters/FiltersTest.php @@ -210,24 +210,6 @@ public function testProcessMethodProcessGlobals(): void $this->assertSame($expected, $filters->initialize()->getFilters()); } - public static function provideProcessMethodProcessGlobalsWithExcept(): iterable - { - return [ - [ - ['admin/*'], - ], - [ - ['admin/*', 'foo/*'], - ], - [ - ['*'], - ], - [ - 'admin/*', - ], - ]; - } - /** * @param array|string $except */ @@ -265,6 +247,24 @@ public function testProcessMethodProcessGlobalsWithExcept($except): void $this->assertSame($expected, $filters->initialize($uri)->getFilters()); } + public static function provideProcessMethodProcessGlobalsWithExcept(): iterable + { + return [ + [ + ['admin/*'], + ], + [ + ['admin/*', 'foo/*'], + ], + [ + ['*'], + ], + [ + 'admin/*', + ], + ]; + } + public function testProcessMethodProcessesFiltersBefore(): void { $_SERVER['REQUEST_METHOD'] = 'GET'; diff --git a/tests/system/HTTP/HeaderTest.php b/tests/system/HTTP/HeaderTest.php index 3dab6faa3784..99ee23708780 100644 --- a/tests/system/HTTP/HeaderTest.php +++ b/tests/system/HTTP/HeaderTest.php @@ -240,7 +240,7 @@ public function testHeaderToStringShowsEntireHeader(): void /** * @param string $name */ - #[DataProvider('invalidNamesProvider')] + #[DataProvider('provideInvalidHeaderNames')] public function testInvalidHeaderNames($name): void { $this->expectException(InvalidArgumentException::class); @@ -251,7 +251,7 @@ public function testInvalidHeaderNames($name): void /** * @return list> */ - public static function invalidNamesProvider(): array + public static function provideInvalidHeaderNames(): iterable { return [ ["Content-Type\r\n\r\n"], @@ -277,7 +277,7 @@ public static function invalidNamesProvider(): array /** * @param array|string>|string|null $value */ - #[DataProvider('invalidValuesProvider')] + #[DataProvider('provideInvalidHeaderValues')] public function testInvalidHeaderValues($value): void { $this->expectException(InvalidArgumentException::class); @@ -288,7 +288,7 @@ public function testInvalidHeaderValues($value): void /** * @return list|string>> */ - public static function invalidValuesProvider(): array + public static function provideInvalidHeaderValues(): iterable { return [ ["Header\n Value"], diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 337fc24384f9..1240d0885b23 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -579,6 +579,24 @@ public function testCanGrabGetRawInput(): void $this->assertSame($expected, $request->getRawInput()); } + /** + * @param string $rawstring + * @param mixed $var + * @param mixed $expected + * @param mixed $filter + * @param mixed $flag + */ + #[DataProvider('provideCanGrabGetRawInputVar')] + public function testCanGrabGetRawInputVar($rawstring, $var, $expected, $filter, $flag): void + { + $config = new App(); + $config->baseURL = 'http://example.com/'; + + $request = $this->createRequest($config, $rawstring); + + $this->assertSame($expected, $request->getRawInputVar($var, $filter, $flag)); + } + public static function provideCanGrabGetRawInputVar(): iterable { return [ @@ -658,24 +676,6 @@ public static function provideCanGrabGetRawInputVar(): iterable ]; } - /** - * @param string $rawstring - * @param mixed $var - * @param mixed $expected - * @param mixed $filter - * @param mixed $flag - */ - #[DataProvider('provideCanGrabGetRawInputVar')] - public function testCanGrabGetRawInputVar($rawstring, $var, $expected, $filter, $flag): void - { - $config = new App(); - $config->baseURL = 'http://example.com/'; - - $request = $this->createRequest($config, $rawstring); - - $this->assertSame($expected, $request->getRawInputVar($var, $filter, $flag)); - } - #[DataProvider('provideIsHTTPMethods')] public function testIsHTTPMethodLowerCase(string $value): void { @@ -902,20 +902,6 @@ public function testGetPostIndexNotExists(): void $this->assertNull($this->request->getGetPost('gc')); } - public static function provideExtensionPHP(): iterable - { - return [ - 'not /index.php' => [ - '/test.php', - '/', - ], - '/index.php' => [ - '/index.php', - '/', - ], - ]; - } - /** * @param mixed $path * @param mixed $detectPath @@ -932,6 +918,20 @@ public function testExtensionPHP($path, $detectPath): void $this->assertSame($detectPath, $request->detectPath()); } + public static function provideExtensionPHP(): iterable + { + return [ + 'not /index.php' => [ + '/test.php', + '/', + ], + '/index.php' => [ + '/index.php', + '/', + ], + ]; + } + public function testGetPath(): void { $request = $this->createRequest(null, null, 'fruits/banana'); diff --git a/tests/system/HTTP/MessageTest.php b/tests/system/HTTP/MessageTest.php index 5e3343416b12..28483674fee9 100644 --- a/tests/system/HTTP/MessageTest.php +++ b/tests/system/HTTP/MessageTest.php @@ -189,25 +189,6 @@ public function testSetHeaderArrayValues(): void $this->assertSame('json, html, xml', $this->message->getHeaderLine('Accept')); } - public static function provideArrayHeaderValue(): iterable - { - return [ - 'existing for next not append' => [ - [ - 'json', - 'html', - 'xml', - ], - ], - 'existing for next append' => [ - [ - 'json', - 'html', - ], - ], - ]; - } - /** * @param array $arrayHeaderValue */ @@ -232,6 +213,25 @@ public function testSetHeaderWithExistingArrayValuesAppendArrayValue($arrayHeade $this->assertSame('json, html, xml', $this->message->getHeaderLine('Accept')); } + public static function provideArrayHeaderValue(): iterable + { + return [ + 'existing for next not append' => [ + [ + 'json', + 'html', + 'xml', + ], + ], + 'existing for next append' => [ + [ + 'json', + 'html', + ], + ], + ]; + } + public function testSetHeaderWithExistingArrayValuesAppendNullValue(): void { $this->message->setHeader('Accept', ['json', 'html', 'xml']); diff --git a/tests/system/HTTP/SiteURITest.php b/tests/system/HTTP/SiteURITest.php index 26d0017d7c34..1208edaf05b9 100644 --- a/tests/system/HTTP/SiteURITest.php +++ b/tests/system/HTTP/SiteURITest.php @@ -66,6 +66,149 @@ public static function provideConstructor(): iterable return array_merge(self::provideSetPath(), self::provideRelativePathWithQueryOrFragment()); } + public static function provideRelativePathWithQueryOrFragment(): iterable + { + return [ + 'one/two?foo=1&bar=2' => [ + 'http://example.com/', // $baseURL + 'index.php', // $indexPage + 'one/two?foo=1&bar=2', // $relativePath + 'http://example.com/index.php/one/two?foo=1&bar=2', // $expectedURI + 'one/two', // $expectedRoutePath + '/index.php/one/two', // $expectedPath + 'foo=1&bar=2', // $expectedQuery + '', // $expectedFragment + ['one', 'two'], // $expectedSegments + 2, // $expectedTotalSegments + ], + 'one/two#sec1' => [ + 'http://example.com/', + 'index.php', + 'one/two#sec1', + 'http://example.com/index.php/one/two#sec1', + 'one/two', + '/index.php/one/two', + '', + 'sec1', + ['one', 'two'], + 2, + ], + 'one/two?foo=1&bar=2#sec1' => [ + 'http://example.com/', + 'index.php', + 'one/two?foo=1&bar=2#sec1', + 'http://example.com/index.php/one/two?foo=1&bar=2#sec1', + 'one/two', + '/index.php/one/two', + 'foo=1&bar=2', + 'sec1', + ['one', 'two'], + 2, + ], + 'Subfolder: one/two?foo=1&bar=2' => [ + 'http://example.com/ci4/', + 'index.php', + 'one/two?foo=1&bar=2', + 'http://example.com/ci4/index.php/one/two?foo=1&bar=2', + 'one/two', + '/ci4/index.php/one/two', + 'foo=1&bar=2', + '', + ['one', 'two'], + 2, + ], + ]; + } + + public function testConstructorHost(): void + { + $config = new App(); + $config->allowedHostnames = ['sub.example.com']; + + $uri = new SiteURI($config, '', 'sub.example.com'); + + $this->assertInstanceOf(SiteURI::class, $uri); + $this->assertSame('http://sub.example.com/index.php', (string) $uri); + $this->assertSame('', $uri->getRoutePath()); + $this->assertSame('/index.php', $uri->getPath()); + $this->assertSame('http://sub.example.com/', $uri->getBaseURL()); + } + + public function testConstructorScheme(): void + { + $config = new App(); + + $uri = new SiteURI($config, '', null, 'https'); + + $this->assertInstanceOf(SiteURI::class, $uri); + $this->assertSame('https://example.com/index.php', (string) $uri); + $this->assertSame('https://example.com/', $uri->getBaseURL()); + } + + public function testConstructorEmptyScheme(): void + { + $config = new App(); + + $uri = new SiteURI($config, '', null, ''); + + $this->assertInstanceOf(SiteURI::class, $uri); + $this->assertSame('http://example.com/index.php', (string) $uri); + $this->assertSame('http://example.com/', $uri->getBaseURL()); + } + + public function testConstructorForceGlobalSecureRequests(): void + { + $config = new App(); + $config->forceGlobalSecureRequests = true; + + $uri = new SiteURI($config); + + $this->assertSame('https://example.com/index.php', (string) $uri); + $this->assertSame('https://example.com/', $uri->getBaseURL()); + } + + public function testConstructorInvalidBaseURL(): void + { + $this->expectException(ConfigException::class); + + $config = new App(); + $config->baseURL = 'invalid'; + + new SiteURI($config); + } + + #[DataProvider('provideSetPath')] + public function testSetPath( + string $baseURL, + string $indexPage, + string $relativePath, + string $expectedURI, + string $expectedRoutePath, + string $expectedPath, + string $expectedQuery, + string $expectedFragment, + array $expectedSegments, + int $expectedTotalSegments, + ): void { + $config = new App(); + $config->indexPage = $indexPage; + $config->baseURL = $baseURL; + + $uri = new SiteURI($config); + + $uri->setPath($relativePath); + + $this->assertSame($expectedURI, (string) $uri); + $this->assertSame($expectedRoutePath, $uri->getRoutePath()); + $this->assertSame($expectedPath, $uri->getPath()); + $this->assertSame($expectedQuery, $uri->getQuery()); + $this->assertSame($expectedFragment, $uri->getFragment()); + $this->assertSame($baseURL, $uri->getBaseURL()); + + $this->assertSame($expectedSegments, $uri->getSegments()); + $this->assertSame($expectedTotalSegments, $uri->getTotalSegments()); + } + public static function provideSetPath(): iterable { return [ @@ -215,149 +358,6 @@ public static function provideSetPath(): iterable ]; } - public static function provideRelativePathWithQueryOrFragment(): iterable - { - return [ - 'one/two?foo=1&bar=2' => [ - 'http://example.com/', // $baseURL - 'index.php', // $indexPage - 'one/two?foo=1&bar=2', // $relativePath - 'http://example.com/index.php/one/two?foo=1&bar=2', // $expectedURI - 'one/two', // $expectedRoutePath - '/index.php/one/two', // $expectedPath - 'foo=1&bar=2', // $expectedQuery - '', // $expectedFragment - ['one', 'two'], // $expectedSegments - 2, // $expectedTotalSegments - ], - 'one/two#sec1' => [ - 'http://example.com/', - 'index.php', - 'one/two#sec1', - 'http://example.com/index.php/one/two#sec1', - 'one/two', - '/index.php/one/two', - '', - 'sec1', - ['one', 'two'], - 2, - ], - 'one/two?foo=1&bar=2#sec1' => [ - 'http://example.com/', - 'index.php', - 'one/two?foo=1&bar=2#sec1', - 'http://example.com/index.php/one/two?foo=1&bar=2#sec1', - 'one/two', - '/index.php/one/two', - 'foo=1&bar=2', - 'sec1', - ['one', 'two'], - 2, - ], - 'Subfolder: one/two?foo=1&bar=2' => [ - 'http://example.com/ci4/', - 'index.php', - 'one/two?foo=1&bar=2', - 'http://example.com/ci4/index.php/one/two?foo=1&bar=2', - 'one/two', - '/ci4/index.php/one/two', - 'foo=1&bar=2', - '', - ['one', 'two'], - 2, - ], - ]; - } - - public function testConstructorHost(): void - { - $config = new App(); - $config->allowedHostnames = ['sub.example.com']; - - $uri = new SiteURI($config, '', 'sub.example.com'); - - $this->assertInstanceOf(SiteURI::class, $uri); - $this->assertSame('http://sub.example.com/index.php', (string) $uri); - $this->assertSame('', $uri->getRoutePath()); - $this->assertSame('/index.php', $uri->getPath()); - $this->assertSame('http://sub.example.com/', $uri->getBaseURL()); - } - - public function testConstructorScheme(): void - { - $config = new App(); - - $uri = new SiteURI($config, '', null, 'https'); - - $this->assertInstanceOf(SiteURI::class, $uri); - $this->assertSame('https://example.com/index.php', (string) $uri); - $this->assertSame('https://example.com/', $uri->getBaseURL()); - } - - public function testConstructorEmptyScheme(): void - { - $config = new App(); - - $uri = new SiteURI($config, '', null, ''); - - $this->assertInstanceOf(SiteURI::class, $uri); - $this->assertSame('http://example.com/index.php', (string) $uri); - $this->assertSame('http://example.com/', $uri->getBaseURL()); - } - - public function testConstructorForceGlobalSecureRequests(): void - { - $config = new App(); - $config->forceGlobalSecureRequests = true; - - $uri = new SiteURI($config); - - $this->assertSame('https://example.com/index.php', (string) $uri); - $this->assertSame('https://example.com/', $uri->getBaseURL()); - } - - public function testConstructorInvalidBaseURL(): void - { - $this->expectException(ConfigException::class); - - $config = new App(); - $config->baseURL = 'invalid'; - - new SiteURI($config); - } - - #[DataProvider('provideSetPath')] - public function testSetPath( - string $baseURL, - string $indexPage, - string $relativePath, - string $expectedURI, - string $expectedRoutePath, - string $expectedPath, - string $expectedQuery, - string $expectedFragment, - array $expectedSegments, - int $expectedTotalSegments, - ): void { - $config = new App(); - $config->indexPage = $indexPage; - $config->baseURL = $baseURL; - - $uri = new SiteURI($config); - - $uri->setPath($relativePath); - - $this->assertSame($expectedURI, (string) $uri); - $this->assertSame($expectedRoutePath, $uri->getRoutePath()); - $this->assertSame($expectedPath, $uri->getPath()); - $this->assertSame($expectedQuery, $uri->getQuery()); - $this->assertSame($expectedFragment, $uri->getFragment()); - $this->assertSame($baseURL, $uri->getBaseURL()); - - $this->assertSame($expectedSegments, $uri->getSegments()); - $this->assertSame($expectedTotalSegments, $uri->getTotalSegments()); - } - public function testSetSegment(): void { $config = new App(); diff --git a/tests/system/HTTP/URITest.php b/tests/system/HTTP/URITest.php index 3564ae6c658e..3199d9c1cb25 100644 --- a/tests/system/HTTP/URITest.php +++ b/tests/system/HTTP/URITest.php @@ -468,6 +468,19 @@ public static function provideSetPath(): iterable ]; } + /** + * @param string $path + * @param string $expected + */ + #[DataProvider('providePathGetsFiltered')] + public function testPathGetsFiltered($path, $expected): void + { + $uri = new URI(); + $uri->setPath($path); + + $this->assertSame($expected, $uri->getPath()); + } + public static function providePathGetsFiltered(): iterable { return [ @@ -510,19 +523,6 @@ public static function providePathGetsFiltered(): iterable ]; } - /** - * @param string $path - * @param string $expected - */ - #[DataProvider('providePathGetsFiltered')] - public function testPathGetsFiltered($path, $expected): void - { - $uri = new URI(); - $uri->setPath($path); - - $this->assertSame($expected, $uri->getPath()); - } - public function testSetFragmentSetsValue(): void { $url = 'http://example.com/path'; @@ -603,6 +603,18 @@ public function testSetQueryThrowsErrorWhenFragmentPresentSilent(): void $this->assertSame('', $uri->getQuery()); } + /** + * @param string $url + * @param string $expected + */ + #[DataProvider('provideAuthorityReturnsExceptedValues')] + public function testAuthorityReturnsExceptedValues($url, $expected): void + { + $uri = new URI($url); + + $this->assertSame($expected, $uri->getAuthority()); + } + public static function provideAuthorityReturnsExceptedValues(): iterable { return [ @@ -642,15 +654,17 @@ public static function provideAuthorityReturnsExceptedValues(): iterable } /** - * @param string $url - * @param string $expected + * @param string $scheme + * @param int $port */ - #[DataProvider('provideAuthorityReturnsExceptedValues')] - public function testAuthorityReturnsExceptedValues($url, $expected): void + #[DataProvider('provideAuthorityRemovesDefaultPorts')] + public function testAuthorityRemovesDefaultPorts($scheme, $port): void { + $url = "{$scheme}://example.com:{$port}/path"; $uri = new URI($url); - $this->assertSame($expected, $uri->getAuthority()); + $expected = "{$scheme}://example.com/path"; + $this->assertSame($expected, (string) $uri); } public static function provideAuthorityRemovesDefaultPorts(): iterable @@ -667,20 +681,6 @@ public static function provideAuthorityRemovesDefaultPorts(): iterable ]; } - /** - * @param string $scheme - * @param int $port - */ - #[DataProvider('provideAuthorityRemovesDefaultPorts')] - public function testAuthorityRemovesDefaultPorts($scheme, $port): void - { - $url = "{$scheme}://example.com:{$port}/path"; - $uri = new URI($url); - - $expected = "{$scheme}://example.com/path"; - $this->assertSame($expected, (string) $uri); - } - public function testSetAuthorityReconstitutes(): void { $authority = 'me@foo.com:3000'; @@ -691,6 +691,16 @@ public function testSetAuthorityReconstitutes(): void $this->assertSame($authority, $uri->getAuthority()); } + /** + * @param string $path + * @param string $expected + */ + #[DataProvider('provideRemoveDotSegments')] + public function testRemoveDotSegments($path, $expected): void + { + $this->assertSame($expected, URI::removeDotSegments($path)); + } + public static function provideRemoveDotSegments(): iterable { return [ @@ -790,13 +800,35 @@ public static function provideRemoveDotSegments(): iterable } /** - * @param string $path + * @param string $rel * @param string $expected */ - #[DataProvider('provideRemoveDotSegments')] - public function testRemoveDotSegments($path, $expected): void + #[DataProvider('defaultResolutions')] + public function testResolveRelativeURI($rel, $expected): void { - $this->assertSame($expected, URI::removeDotSegments($path)); + $base = 'http://a/b/c/d'; + $uri = new URI($base); + + $new = $uri->resolveRelativeURI($rel); + + $this->assertSame($expected, (string) $new); + } + + /** + * @param string $rel + * @param string $expected + */ + #[DataProvider('defaultResolutions')] + public function testResolveRelativeURIHTTPS($rel, $expected): void + { + $base = 'https://a/b/c/d'; + $expected = str_replace('http:', 'https:', $expected); + + $uri = new URI($base); + + $new = $uri->resolveRelativeURI($rel); + + $this->assertSame($expected, (string) $new); } public static function defaultResolutions(): iterable @@ -829,38 +861,6 @@ public static function defaultResolutions(): iterable ]; } - /** - * @param string $rel - * @param string $expected - */ - #[DataProvider('defaultResolutions')] - public function testResolveRelativeURI($rel, $expected): void - { - $base = 'http://a/b/c/d'; - $uri = new URI($base); - - $new = $uri->resolveRelativeURI($rel); - - $this->assertSame($expected, (string) $new); - } - - /** - * @param string $rel - * @param string $expected - */ - #[DataProvider('defaultResolutions')] - public function testResolveRelativeURIHTTPS($rel, $expected): void - { - $base = 'https://a/b/c/d'; - $expected = str_replace('http:', 'https:', $expected); - - $uri = new URI($base); - - $new = $uri->resolveRelativeURI($rel); - - $this->assertSame($expected, (string) $new); - } - public function testResolveRelativeURIWithNoBase(): void { $base = 'http://a'; diff --git a/tests/system/Helpers/ArrayHelperTest.php b/tests/system/Helpers/ArrayHelperTest.php index 77079d2a5a2b..0293cc6c826e 100644 --- a/tests/system/Helpers/ArrayHelperTest.php +++ b/tests/system/Helpers/ArrayHelperTest.php @@ -241,6 +241,32 @@ public function testArrayDeepSearch($key, $expected): void $this->assertSame($expected, $result); } + public static function provideArrayDeepSearch(): iterable + { + return [ + [ + 'key6441', + 'Value 6.4.4.1', + ], + [ + 'key64421', + null, + ], + [ + 42, + 'Value 42', + ], + [ + 'key644', + ['key6441' => 'Value 6.4.4.1'], + ], + [ + '', + null, + ], + ]; + } + public function testArrayDeepSearchReturnNullEmptyArray(): void { $data = []; @@ -308,32 +334,6 @@ public function testArraySortByMultipleKeysFailsInconsistentArraySizes($data): v array_sort_by_multiple_keys($data, $sortColumns); } - public static function provideArrayDeepSearch(): iterable - { - return [ - [ - 'key6441', - 'Value 6.4.4.1', - ], - [ - 'key64421', - null, - ], - [ - 42, - 'Value 42', - ], - [ - 'key644', - ['key6441' => 'Value 6.4.4.1'], - ], - [ - '', - null, - ], - ]; - } - public static function provideSortByMultipleKeys(): iterable { $seed = [ @@ -493,14 +493,6 @@ public function testArrayGroupByIncludeEmpty(array $indexes, array $data, array $this->assertSame($expected, $actual, 'array including empty not the same'); } - #[DataProvider('provideArrayGroupByExcludeEmpty')] - public function testArrayGroupByExcludeEmpty(array $indexes, array $data, array $expected): void - { - $actual = array_group_by($data, $indexes, false); - - $this->assertSame($expected, $actual, 'array excluding empty not the same'); - } - public static function provideArrayGroupByIncludeEmpty(): iterable { yield 'simple group-by test' => [ @@ -912,6 +904,14 @@ public static function provideArrayGroupByIncludeEmpty(): iterable ]; } + #[DataProvider('provideArrayGroupByExcludeEmpty')] + public function testArrayGroupByExcludeEmpty(array $indexes, array $data, array $expected): void + { + $actual = array_group_by($data, $indexes, false); + + $this->assertSame($expected, $actual, 'array excluding empty not the same'); + } + public static function provideArrayGroupByExcludeEmpty(): iterable { yield 'simple group-by test' => [ diff --git a/tests/system/Helpers/InflectorHelperTest.php b/tests/system/Helpers/InflectorHelperTest.php index f0df11977126..59068b8bf751 100644 --- a/tests/system/Helpers/InflectorHelperTest.php +++ b/tests/system/Helpers/InflectorHelperTest.php @@ -246,6 +246,12 @@ public function testDasherize(): void } } + #[DataProvider('provideOrdinal')] + public function testOrdinal(string $suffix, int $number): void + { + $this->assertSame($suffix, ordinal($number)); + } + public static function provideOrdinal(): iterable { return [ @@ -262,12 +268,6 @@ public static function provideOrdinal(): iterable ]; } - #[DataProvider('provideOrdinal')] - public function testOrdinal(string $suffix, int $number): void - { - $this->assertSame($suffix, ordinal($number)); - } - public function testOrdinalize(): void { $suffixedNumbers = [ diff --git a/tests/system/Helpers/URLHelper/CurrentUrlTest.php b/tests/system/Helpers/URLHelper/CurrentUrlTest.php index 1c7de84586e2..b802db349949 100644 --- a/tests/system/Helpers/URLHelper/CurrentUrlTest.php +++ b/tests/system/Helpers/URLHelper/CurrentUrlTest.php @@ -242,6 +242,44 @@ public function testUriStringSubfolderRelative(): void $this->assertSame('assets/image.jpg', uri_string()); } + #[DataProvider('provideUrlIs')] + public function testUrlIs(string $currentPath, string $testPath, bool $expected): void + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/' . $currentPath; + + $this->createRequest($this->config); + + $this->assertSame($expected, url_is($testPath)); + } + + #[DataProvider('provideUrlIs')] + public function testUrlIsNoIndex(string $currentPath, string $testPath, bool $expected): void + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/' . $currentPath; + + $this->config->indexPage = ''; + + $this->createRequest($this->config); + + $this->assertSame($expected, url_is($testPath)); + } + + #[DataProvider('provideUrlIs')] + public function testUrlIsWithSubfolder(string $currentPath, string $testPath, bool $expected): void + { + $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['REQUEST_URI'] = '/' . $currentPath; + $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; + + $this->config->baseURL = 'http://example.com/subfolder/'; + + $this->createRequest($this->config); + + $this->assertSame($expected, url_is($testPath)); + } + public static function provideUrlIs(): iterable { return [ @@ -282,42 +320,4 @@ public static function provideUrlIs(): iterable ], ]; } - - #[DataProvider('provideUrlIs')] - public function testUrlIs(string $currentPath, string $testPath, bool $expected): void - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/' . $currentPath; - - $this->createRequest($this->config); - - $this->assertSame($expected, url_is($testPath)); - } - - #[DataProvider('provideUrlIs')] - public function testUrlIsNoIndex(string $currentPath, string $testPath, bool $expected): void - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/' . $currentPath; - - $this->config->indexPage = ''; - - $this->createRequest($this->config); - - $this->assertSame($expected, url_is($testPath)); - } - - #[DataProvider('provideUrlIs')] - public function testUrlIsWithSubfolder(string $currentPath, string $testPath, bool $expected): void - { - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/' . $currentPath; - $_SERVER['SCRIPT_NAME'] = '/subfolder/index.php'; - - $this->config->baseURL = 'http://example.com/subfolder/'; - - $this->createRequest($this->config); - - $this->assertSame($expected, url_is($testPath)); - } } diff --git a/tests/system/Helpers/URLHelper/MiscUrlTest.php b/tests/system/Helpers/URLHelper/MiscUrlTest.php index 78c7fd5b0d24..81ec9770ea2a 100644 --- a/tests/system/Helpers/URLHelper/MiscUrlTest.php +++ b/tests/system/Helpers/URLHelper/MiscUrlTest.php @@ -128,6 +128,21 @@ public function testIndexPageAlt(): void $this->assertSame('banana.php', index_page($this->config)); } + /** + * @param mixed $expected + * @param mixed $uri + * @param mixed $title + * @param mixed $attributes + */ + #[DataProvider('provideAnchor')] + public function testAnchor($expected = '', $uri = '', $title = '', $attributes = ''): void + { + $uriString = 'http://example.com/'; + $this->createRequest($uriString); + + $this->assertSame($expected, anchor($uri, $title, $attributes, $this->config)); + } + // Test anchor public static function provideAnchor(): iterable @@ -179,9 +194,11 @@ public static function provideAnchor(): iterable * @param mixed $title * @param mixed $attributes */ - #[DataProvider('provideAnchor')] - public function testAnchor($expected = '', $uri = '', $title = '', $attributes = ''): void + #[DataProvider('provideAnchorNoindex')] + public function testAnchorNoindex($expected = '', $uri = '', $title = '', $attributes = ''): void { + $this->config->indexPage = ''; + $uriString = 'http://example.com/'; $this->createRequest($uriString); @@ -243,8 +260,8 @@ public static function provideAnchorNoindex(): iterable * @param mixed $title * @param mixed $attributes */ - #[DataProvider('provideAnchorNoindex')] - public function testAnchorNoindex($expected = '', $uri = '', $title = '', $attributes = ''): void + #[DataProvider('provideAnchorTargetted')] + public function testAnchorTargetted($expected = '', $uri = '', $title = '', $attributes = ''): void { $this->config->indexPage = ''; @@ -299,11 +316,9 @@ public static function provideAnchorTargetted(): iterable * @param mixed $title * @param mixed $attributes */ - #[DataProvider('provideAnchorTargetted')] - public function testAnchorTargetted($expected = '', $uri = '', $title = '', $attributes = ''): void + #[DataProvider('provideAnchorExamples')] + public function testAnchorExamples($expected = '', $uri = '', $title = '', $attributes = ''): void { - $this->config->indexPage = ''; - $uriString = 'http://example.com/'; $this->createRequest($uriString); @@ -344,13 +359,13 @@ public static function provideAnchorExamples(): iterable * @param mixed $title * @param mixed $attributes */ - #[DataProvider('provideAnchorExamples')] - public function testAnchorExamples($expected = '', $uri = '', $title = '', $attributes = ''): void + #[DataProvider('provideAnchorPopup')] + public function testAnchorPopup($expected = '', $uri = '', $title = '', $attributes = false): void { $uriString = 'http://example.com/'; $this->createRequest($uriString); - $this->assertSame($expected, anchor($uri, $title, $attributes, $this->config)); + $this->assertSame($expected, anchor_popup($uri, $title, $attributes, $this->config)); } // Test anchor_popup @@ -397,17 +412,17 @@ public static function provideAnchorPopup(): iterable /** * @param mixed $expected - * @param mixed $uri + * @param mixed $email * @param mixed $title * @param mixed $attributes */ - #[DataProvider('provideAnchorPopup')] - public function testAnchorPopup($expected = '', $uri = '', $title = '', $attributes = false): void + #[DataProvider('provideMailto')] + public function testMailto($expected = '', $email = '', $title = '', $attributes = ''): void { $uriString = 'http://example.com/'; $this->createRequest($uriString); - $this->assertSame($expected, anchor_popup($uri, $title, $attributes, $this->config)); + $this->assertSame($expected, mailto($email, $title, $attributes)); } // Test mailto @@ -439,13 +454,13 @@ public static function provideMailto(): iterable * @param mixed $title * @param mixed $attributes */ - #[DataProvider('provideMailto')] - public function testMailto($expected = '', $email = '', $title = '', $attributes = ''): void + #[DataProvider('provideSafeMailto')] + public function testSafeMailto($expected = '', $email = '', $title = '', $attributes = ''): void { $uriString = 'http://example.com/'; $this->createRequest($uriString); - $this->assertSame($expected, mailto($email, $title, $attributes)); + $this->assertSame($expected, safe_mailto($email, $title, $attributes)); } // Test safe_mailto @@ -471,21 +486,6 @@ public static function provideSafeMailto(): iterable ]; } - /** - * @param mixed $expected - * @param mixed $email - * @param mixed $title - * @param mixed $attributes - */ - #[DataProvider('provideSafeMailto')] - public function testSafeMailto($expected = '', $email = '', $title = '', $attributes = ''): void - { - $uriString = 'http://example.com/'; - $this->createRequest($uriString); - - $this->assertSame($expected, safe_mailto($email, $title, $attributes)); - } - public function testSafeMailtoWithCsp(): void { $this->config->CSPEnabled = true; @@ -496,6 +496,16 @@ public function testSafeMailtoWithCsp(): void $this->assertMatchesRegularExpression('/