From 367904b98072205fe9ed98203fb799fb61c69a7b Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 12 Jan 2023 17:14:34 +1300 Subject: [PATCH] NFC superficial prelminary cleanup in test class --- tests/phpunit/CRM/Utils/StringTest.php | 155 ++++++++++++++----------- 1 file changed, 85 insertions(+), 70 deletions(-) diff --git a/tests/phpunit/CRM/Utils/StringTest.php b/tests/phpunit/CRM/Utils/StringTest.php index c9d46ab108..ed452120f7 100644 --- a/tests/phpunit/CRM/Utils/StringTest.php +++ b/tests/phpunit/CRM/Utils/StringTest.php @@ -6,7 +6,7 @@ */ class CRM_Utils_StringTest extends CiviUnitTestCase { - public function testBase64Url() { + public function testBase64Url(): void { $examples = [ 'a' => 'YQ', 'ab' => 'YWI', @@ -19,7 +19,7 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { } } - public function testStripPathChars() { + public function testStripPathChars(): void { $testSet = [ '' => '', NULL => NULL, @@ -32,11 +32,11 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { foreach ($testSet as $in => $expected) { $out = CRM_Utils_String::stripPathChars($in); - $this->assertEquals($out, $expected, "Output does not match"); + $this->assertEquals($out, $expected, 'Output does not match'); } } - public function testExtractName() { + public function testExtractName(): void { $cases = [ [ 'full_name' => 'Alan', @@ -85,7 +85,12 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { } } - public function testEllipsify() { + /** + * Test the ellipsify function. + * + * @noinspection SpellCheckingInspection + */ + public function testEllipsify(): void { $maxLen = 5; $cases = [ '1' => '1', @@ -101,7 +106,7 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { $this->assertEquals(TRUE, mb_check_encoding(CRM_Utils_String::ellipsify($input, $maxLen), 'UTF-8')); } - public function testRandom() { + public function testRandom(): void { for ($i = 0; $i < 4; $i++) { $actual = CRM_Utils_String::createRandom(4, 'abc'); $this->assertEquals(4, strlen($actual)); @@ -116,7 +121,7 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { /** * @return array */ - public function parsePrefixData() { + public function parsePrefixData(): array { $cases = []; $cases[] = ['administer CiviCRM', NULL, [NULL, 'administer CiviCRM']]; $cases[] = ['administer CiviCRM', 'com_civicrm', ['com_civicrm', 'administer CiviCRM']]; @@ -127,11 +132,12 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { /** * @dataProvider parsePrefixData + * * @param $input * @param $defaultPrefix * @param $expected */ - public function testParsePrefix($input, $defaultPrefix, $expected) { + public function testParsePrefix($input, $defaultPrefix, $expected): void { $actual = CRM_Utils_String::parsePrefix(':', $input, $defaultPrefix); $this->assertEquals($expected, $actual); } @@ -139,7 +145,7 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { /** * @return array */ - public function booleanDataProvider() { + public function booleanDataProvider(): array { // array(0 => $input, 1 => $expectedOutput) $cases = []; $cases[] = [TRUE, TRUE]; @@ -166,16 +172,22 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { } /** - * @param $input + * @param mixed $input * @param bool $expected - * * @dataProvider booleanDataProvider + * + * @dataProvider booleanDataProvider */ - public function testStrToBool($input, $expected) { + public function testStrToBool($input, bool $expected): void { $actual = CRM_Utils_String::strtobool($input); - $this->assertTrue($expected === $actual); + $this->assertSame($expected, $actual); } - public function startEndCases() { + /** + * Data provider for checking how strings start and end. + * + * @noinspection SpellCheckingInspection + */ + public function startEndCases(): array { $cases = []; $cases[] = ['startsWith', 'foo', '', TRUE]; $cases[] = ['startsWith', 'foo', 'f', TRUE]; @@ -194,19 +206,20 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { } /** - * @param string $func + * @param string $function * One of: 'startsWith' or 'endsWith'. * @param $string * @param $fragment * @param $expectedResult + * * @dataProvider startEndCases */ - public function testStartEndWith($func, $string, $fragment, $expectedResult) { - $actualResult = \CRM_Utils_String::$func($string, $fragment); - $this->assertEquals($expectedResult, $actualResult, "Checking $func($string,$fragment)"); + public function testStartEndWith(string $function, $string, $fragment, $expectedResult): void { + $actualResult = CRM_Utils_String::$function($string, $fragment); + $this->assertEquals($expectedResult, $actualResult, "Checking $function($string,$fragment)"); } - public function wildcardCases() { + public function wildcardCases(): array { $cases = []; $cases[] = ['*', ['foo.bar.1', 'foo.bar.2', 'foo.whiz', 'bang.bang']]; $cases[] = ['foo.*', ['foo.bar.1', 'foo.bar.2', 'foo.whiz']]; @@ -221,7 +234,7 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { * @param $expectedResults * @dataProvider wildcardCases */ - public function testFilterByWildCards($patterns, $expectedResults) { + public function testFilterByWildCards($patterns, $expectedResults): void { $data = ['foo.bar.1', 'foo.bar.2', 'foo.whiz', 'bang.bang']; $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data); @@ -230,7 +243,7 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { $patterns = (array) $patterns; $patterns[] = 'noise'; - $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data, FALSE); + $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data); $this->assertEquals($expectedResults, $actualResults); $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data, TRUE); @@ -242,12 +255,12 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { * @see https://issues.civicrm.org/jira/browse/CRM-14283 * * @param string $imageURL - * @param book $forceHttps + * @param bool $forceHttps * @param string $expected * * @dataProvider simplifyURLProvider */ - public function testSimplifyURL($imageURL, $forceHttps, $expected) { + public function testSimplifyURL(string $imageURL, bool $forceHttps, string $expected): void { $this->assertEquals( $expected, CRM_Utils_String::simplifyURL($imageURL, $forceHttps) @@ -258,18 +271,20 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { * Used for testNormalizeImageURL above * * @return array + * @throws \CRM_Core_Exception + * @noinspection HttpUrlsUsage */ - public function simplifyURLProvider() { + public function simplifyURLProvider(): array { $config = CRM_Core_Config::singleton(); $urlParts = CRM_Utils_String::simpleParseUrl($config->userFrameworkBaseURL); $localDomain = $urlParts['host+port']; if (empty($localDomain)) { - throw new \Exception("Failed to determine local base URL"); + throw new CRM_Core_Exception('Failed to determine local base URL'); } $externalDomain = 'example.org'; // Ensure that $externalDomain really is different from $localDomain - if ($externalDomain == $localDomain) { + if ($externalDomain === $localDomain) { $externalDomain = 'example.net'; } @@ -295,14 +310,14 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { "http://$externalDomain/sites/default/files/coffee-mug.jpg", ], 'local URL' => [ - "/sites/default/files/coffee-mug.jpg", + '/sites/default/files/coffee-mug.jpg', FALSE, - "/sites/default/files/coffee-mug.jpg", + '/sites/default/files/coffee-mug.jpg', ], 'local URL without a forward slash' => [ - "sites/default/files/coffee-mug.jpg", + 'sites/default/files/coffee-mug.jpg', FALSE, - "/sites/default/files/coffee-mug.jpg", + '/sites/default/files/coffee-mug.jpg', ], 'empty input' => [ '', @@ -318,7 +333,7 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { * * @dataProvider parseURLProvider */ - public function testSimpleParseUrl($url, $expected) { + public function testSimpleParseUrl(string $url, array $expected): void { $this->assertEquals( $expected, CRM_Utils_String::simpleParseUrl($url) @@ -330,40 +345,40 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { * * @return array */ - public function parseURLProvider() { + public function parseURLProvider(): array { return [ - "prototypical example" => [ - "https://example.com:8000/foo/bar/?id=1#fragment", + 'prototypical example' => [ + 'https://example.com:8000/foo/bar/?id=1#fragment', [ - 'host+port' => "example.com:8000", - 'path+query' => "/foo/bar/?id=1", + 'host+port' => 'example.com:8000', + 'path+query' => '/foo/bar/?id=1', ], ], - "default port example" => [ - "https://example.com/foo/bar/?id=1#fragment", + 'default port example' => [ + 'https://example.com/foo/bar/?id=1#fragment', [ - 'host+port' => "example.com", - 'path+query' => "/foo/bar/?id=1", + 'host+port' => 'example.com', + 'path+query' => '/foo/bar/?id=1', ], ], - "empty" => [ - "", + 'empty' => [ + '', [ - 'host+port' => "", - 'path+query' => "", + 'host+port' => '', + 'path+query' => '', ], ], - "path only" => [ - "/foo/bar/image.png", + 'path only' => [ + '/foo/bar/image.png', [ - 'host+port' => "", - 'path+query' => "/foo/bar/image.png", + 'host+port' => '', + 'path+query' => '/foo/bar/image.png', ], ], ]; } - public function purifyHTMLProvider() { + public function purifyHTMLProvider(): array { $tests = []; $tests[] = ['HOVER', 'HOVER']; $tests[] = ['hello', 'hello']; @@ -371,51 +386,51 @@ class CRM_Utils_StringTest extends CiviUnitTestCase { } /** - * Test ouput of purifyHTML + * Test output of purifyHTML + * * @param string $testString * @param string $expectedString + * * @dataProvider purifyHTMLProvider */ - public function testPurifyHTML($testString, $expectedString) { + public function testPurifyHTML(string $testString, string $expectedString): void { $this->assertEquals($expectedString, CRM_Utils_String::purifyHTML($testString)); } - public function getGoodSerializeExamples() { - $strs = []; - - $strs[] = ['a:1:{s:1:"a";s:1:"b";}']; - $strs[] = ['d:1.2;']; - $strs[] = ['s:3:"abc";']; - $strs[] = ['N;']; - $strs[] = ['a:7:{i:0;N;i:1;s:3:"abc";i:2;i:1;i:3;d:2.3;i:4;b:1;i:5;b:0;i:6;i:0;}']; - - return $strs; + public function getGoodSerializeExamples(): array { + $strings = []; + $strings[] = ['a:1:{s:1:"a";s:1:"b";}']; + $strings[] = ['d:1.2;']; + $strings[] = ['s:3:"abc";']; + $strings[] = ['N;']; + $strings[] = ['a:7:{i:0;N;i:1;s:3:"abc";i:2;i:1;i:3;d:2.3;i:4;b:1;i:5;b:0;i:6;i:0;}']; + return $strings; } /** * @param string $str * A safe serialized value. + * * @dataProvider getGoodSerializeExamples */ - public function testGoodSerialize($str) { + public function testGoodSerialize(string $str): void { $this->assertEquals(unserialize($str), CRM_Utils_String::unserialize($str)); } - public function getBadSerializeExamples() { - $strs = []; - - $strs[] = ['O:8:"stdClass":0:{}']; - $strs[] = ['O:9:"Exception":7:{s:10:"*message";s:3:"abc";s:17:"Exceptionstring";s:0:"";s:7:"*code";i:0;s:7:"*file";s:17:"Command line code";s:7:"*line";i:1;s:16:"Exceptiontrace";a:0:{}s:19:"Exceptionprevious";N;}']; - - return $strs; + public function getBadSerializeExamples(): array { + $strings = []; + $strings[] = ['O:8:"stdClass":0:{}']; + $strings[] = ['O:9:"Exception":7:{s:10:"*message";s:3:"abc";s:17:"ExceptionString";s:0:"";s:7:"*code";i:0;s:7:"*file";s:17:"Command line code";s:7:"*line";i:1;s:16:"ExceptionTrace";a:0:{}s:19:"ExceptionPrevious";N;}']; + return $strings; } /** * @param string $str * An unsafe serialized value. + * * @dataProvider getBadSerializeExamples */ - public function testBadSerializeExamples($str) { + public function testBadSerializeExamples(string $str): void { $this->assertFalse(CRM_Utils_String::unserialize($str)); } -- 2.25.1