From 5e13cc5cab35b3413b985a32836e2a41b85c323c Mon Sep 17 00:00:00 2001 From: ayushchd Date: Fri, 21 Jun 2013 12:08:47 +0545 Subject: [PATCH 1/3] Unit tests for ctype and native string classes --- libraries/StringNativeType.class.php | 2 +- test/libraries/PMA_StringCType_test.php | 261 +++++++++++++++++++ test/libraries/PMA_StringNativeType_test.php | 248 ++++++++++++++++++ 3 files changed, 510 insertions(+), 1 deletion(-) create mode 100644 test/libraries/PMA_StringCType_test.php create mode 100644 test/libraries/PMA_StringNativeType_test.php diff --git a/libraries/StringNativeType.class.php b/libraries/StringNativeType.class.php index 4e995d0f2a..d9e47ecda7 100644 --- a/libraries/StringNativeType.class.php +++ b/libraries/StringNativeType.class.php @@ -39,7 +39,7 @@ class PMA_StringNativeType */ public static function isAlpha($c) { - return ($GLOBALS['PMA_StringType']::isUpper($c) || $GLOBALS['PMA_StringType']::isLower($c)); + return (self::isUpper($c) || self::isLower($c)); } // end of the "isAlpha()" function /** diff --git a/test/libraries/PMA_StringCType_test.php b/test/libraries/PMA_StringCType_test.php new file mode 100644 index 0000000000..d030fcb02c --- /dev/null +++ b/test/libraries/PMA_StringCType_test.php @@ -0,0 +1,261 @@ +markTestSkipped( + "ctype extension not present." + ); + } + } + + /** + * Test for PMA_StringCType::isAlnum + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isAlnumData + */ + public function testIsAlnum($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringCType::isAlnum($str) + ); + } + + /** + * Data provider for testIsAlnum + * + * @return array Test data + */ + public function isAlnumData() + { + return array( + array(true, "AbCd1zyZ9"), + array(false, "foo!#$bar") + ); + } + + /** + * Test for PMA_StringCType::isAlpha + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isAlphaData + */ + public function testIsAlpha($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringCType::isAlpha($str) + ); + } + + /** + * Data provider for testIsAlpha + * + * @return array Test data + */ + public function isAlphaData() + { + return array( + array(true, "kJW"), + array(false, "k12"), + ); + } + + /** + * Test for PMA_StringCType::isDigit + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isDigitData + */ + public function testIsDigit($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringCType::isDigit($str) + ); + } + + /** + * Data provider for testIsDigit + * + * @return array Test data + */ + public function isDigitData() + { + return array( + array(false, "kJW"), + array(false, "?.foo!#21"), + array(true, "12"), + ); + } + + /** + * Test for PMA_StringCType::isUpper + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isUpperData + */ + public function testIsUpper($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringCType::isUpper($str) + ); + } + + /** + * Data provider for testIsUpper + * + * @return array Test data + */ + public function isUpperData() + { + return array( + array(true, "ABCD"), + array(false, "AbCD"), + array(false, "ABCD12!3") + ); + } + + /** + * Test for PMA_StringCType::isLower + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isLowerData + */ + public function testIsLower($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringCType::isLower($str) + ); + } + + /** + * Data provider for testIsLower + * + * @return array Test data + */ + public function isLowerData() + { + return array( + array(true, "abcd"), + array(false, "aBcd"), + array(false, "abcd12!3") + ); + } + + /** + * Test for PMA_StringCType::isSpace + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isSpaceData + */ + public function testIsSpace($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringCType::isSpace($str) + ); + } + + /** + * Data provider for testIsSpace + * + * @return array Test data + */ + public function isSpaceData() + { + return array( + array(true, " "), + array(false, '\n\r\t'), + array(true, "\n\r\t"), + array(false, "\ntest"), + ); + } + + /** + * Test for PMA_StringCType::isHexDigit + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isHexDigitData + */ + public function testIsHexDigit($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringCType::isHexDigit($str) + ); + } + + /** + * Data provider for testIsHexDigit + * + * @return array Test data + */ + public function isHexDigitData() + { + return array( + array(true, "AB10BC99"), + array(false, "AR1012"), + array(true, "ab12bc99") + ); + } + +} +?> diff --git a/test/libraries/PMA_StringNativeType_test.php b/test/libraries/PMA_StringNativeType_test.php new file mode 100644 index 0000000000..f18763e64b --- /dev/null +++ b/test/libraries/PMA_StringNativeType_test.php @@ -0,0 +1,248 @@ +assertEquals( + $expected, + PMA_StringNativeType::isAlnum($str) + ); + } + + /** + * Data provider for testIsAlnum + * + * @return array Test data + */ + public function isAlnumData() + { + return array( + array(true, "A"), + array(false, "."), + array(true, "a"), + array(true, "2") + ); + } + + /** + * Test for PMA_StringNativeType::isAlpha + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isAlphaData + */ + public function testIsAlpha($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringNativeType::isAlpha($str) + ); + } + + /** + * Data provider for testIsAlpha + * + * @return array Test data + */ + public function isAlphaData() + { + return array( + array(true, "k"), + array(false, "1"), + ); + } + + /** + * Test for PMA_StringNativeType::isDigit + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isDigitData + */ + public function testIsDigit($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringNativeType::isDigit($str) + ); + } + + /** + * Data provider for testIsDigit + * + * @return array Test data + */ + public function isDigitData() + { + return array( + array(false, "k"), + array(false, "?"), + array(true, "1"), + ); + } + + /** + * Test for PMA_StringNativeType::isUpper + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isUpperData + */ + public function testIsUpper($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringNativeType::isUpper($str) + ); + } + + /** + * Data provider for testIsUpper + * + * @return array Test data + */ + public function isUpperData() + { + return array( + array(true, "A"), + array(false, "b"), + array(false, "1") + ); + } + + /** + * Test for PMA_StringNativeType::isLower + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isLowerData + */ + public function testIsLower($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringNativeType::isLower($str) + ); + } + + /** + * Data provider for testIsLower + * + * @return array Test data + */ + public function isLowerData() + { + return array( + array(true, "a"), + array(false, "B"), + array(false, "1") + ); + } + + /** + * Test for PMA_StringNativeType::isSpace + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isSpaceData + */ + public function testIsSpace($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringNativeType::isSpace($str) + ); + } + + /** + * Data provider for testIsSpace + * + * @return array Test data + */ + public function isSpaceData() + { + return array( + array(true, " "), + array(false, '\n'), + array(true, "\n"), + array(false, "t"), + ); + } + + /** + * Test for PMA_StringNativeType::isHexDigit + * + * @param integer $expected Expected output + * @param string $str String to check + * + * @return void + * @test + * @dataProvider isHexDigitData + */ + public function testIsHexDigit($expected, $str) + { + $this->assertEquals( + $expected, + PMA_StringNativeType::isHexDigit($str) + ); + } + + /** + * Data provider for testIsHexDigit + * + * @return array Test data + */ + public function isHexDigitData() + { + return array( + array(true, "A"), + array(false, "R"), + array(true, "a") + ); + } + +} +?> From fe93b9234004d74644b3800a742cad781c5df494 Mon Sep 17 00:00:00 2001 From: ayushchd Date: Fri, 21 Jun 2013 13:16:47 +0545 Subject: [PATCH 2/3] Share code between StringMB and StringNative tests --- test/libraries/PMA_StringMB_test.php | 145 +---------------------- test/libraries/PMA_StringNative_test.php | 38 ++++-- 2 files changed, 30 insertions(+), 153 deletions(-) diff --git a/test/libraries/PMA_StringMB_test.php b/test/libraries/PMA_StringMB_test.php index 10a5124e8b..bde4d46db4 100644 --- a/test/libraries/PMA_StringMB_test.php +++ b/test/libraries/PMA_StringMB_test.php @@ -10,13 +10,14 @@ * Include to test. */ require_once 'libraries/StringMB.class.php'; +require_once 'test/libraries/PMA_StringNative_test.php'; /** * Tests for Specialized String Functions (multi-byte) for phpMyAdmin * * @package PhpMyAdmin-test */ -class PMA_String_Mb_Test extends PHPUnit_Framework_TestCase +class PMA_String_Mb_Test extends PMA_StringNative_Test { protected $internal_encoding; @@ -29,6 +30,9 @@ class PMA_String_Mb_Test extends PHPUnit_Framework_TestCase protected function setUp() { $this->internal_encoding = mb_internal_encoding(); + mb_internal_encoding("UTF-8"); + + $this->testClass = new PMA_StringMB; } /** @@ -41,144 +45,5 @@ class PMA_String_Mb_Test extends PHPUnit_Framework_TestCase { mb_internal_encoding($this->internal_encoding); } - - /** - * Test for PMA_StringMB::strlen - * - * @param integer $length Length of the string - * @param string $str String to check for - * @param string $encoding Encoding of the string - * - * @return void - * @test - * @dataProvider mbStrlenData - */ - public function testMbStrlen($length, $str, $encoding) - { - mb_internal_encoding($encoding); - $this->assertEquals( - $length, - PMA_StringMB::strlen($str) - ); - } - - /** - * Data provider for testMbStrlen - * - * @return array Test data - */ - public function mbStrlenData() - { - return array( - array(2, "ab", "UTF-8"), - array(9, "åèö - doo", "UTF-8"), - array(12, "åèö - doo", "ISO-8859-1") - ); - } - - /** - * Test for PMA_StringMB::substr - * - * @param string $str Expected substring - * @param string $haystack String to check in - * @param int $start Starting position of substring - * @param int $length Length of substring - * @param string $encoding Encoding of the string - * - * @return void - * @test - * @dataProvider mbSubStrData - */ - public function testMbSubStr($str, $haystack, $start, $length, $encoding) - { - mb_internal_encoding($encoding); - $this->assertEquals( - $str, - PMA_StringMB::substr($haystack, $start, $length) - ); - } - - /** - * Data provider for testMbSubStr - * - * @return array Test data - */ - public function mbSubStrData() - { - return array( - array("b", "ab", 1, 1, "UTF-8"), - array("èö", "åèö - doo", 1, 2, "UTF-8"), - ); - } - - /** - * Test for PMA_StringMB::strpos - * - * @param int $pos Expected position - * @param string $haystack String to search in - * @param string $needle String to search for - * @param int $offset Search offset - * @param string $encoding Encoding to test against - * - * @return void - * @test - * @dataProvider mbStrposData - */ - public function testMbStrpos($pos, $haystack, $needle, $offset, $encoding) - { - mb_internal_encoding($encoding); - $this->assertEquals( - $pos, - PMA_StringMB::strpos($haystack, $needle, $offset) - ); - } - - /** - * Data provider for testMbStrpos - * - * @return array Test data - */ - public function mbStrposData() - { - return array( - array(1, "ab", "b", 0, "UTF-8"), - array(2, "åèö - doo", "ö", 0, "UTF-8"), - array(6, "åèöè", "è", 3, "ISO-8859-1") - ); - } - - /** - * Test for PMA_StringMB::strtolower - * - * @param string $expected Expected lowercased string - * @param string $string String to convert to lowercase - * @param string $encoding Encoding to test against - * - * @return void - * @test - * @dataProvider mbStrToLowerData - */ - public function testMbStrToLower($expected, $string, $encoding) - { - mb_internal_encoding($encoding); - $this->assertEquals( - $expected, - PMA_StringMB::strtolower($string) - ); - } - - /** - * Data provider for testMbStrpos - * - * @return array Test data - */ - public function mbStrToLowerData() - { - return array( - array("mary had a", "Mary Had A", "UTF-8"), - array("τάχιστη", "Τάχιστη", "UTF-8") - ); - } - } ?> diff --git a/test/libraries/PMA_StringNative_test.php b/test/libraries/PMA_StringNative_test.php index 5b8e24bd08..5e1947a7ec 100644 --- a/test/libraries/PMA_StringNative_test.php +++ b/test/libraries/PMA_StringNative_test.php @@ -18,7 +18,19 @@ require_once 'libraries/StringNative.class.php'; */ class PMA_StringNative_Test extends PHPUnit_Framework_TestCase { + protected $testClass; + /** + * Setup function for test cases + * + * @access protected + * @return void + */ + protected function setUp() + { + $this->testClass = new PMA_StringNative; + } + /** * Test for PMA_StringNative::strlen * @@ -27,13 +39,13 @@ class PMA_StringNative_Test extends PHPUnit_Framework_TestCase * * @return void * @test - * @dataProvider nativeStrlenData + * @dataProvider strlenData */ public function testNativeStrlen($length, $str) { $this->assertEquals( $length, - PMA_StringNative::strlen($str) + $this->testClass->strlen($str) ); } @@ -42,7 +54,7 @@ class PMA_StringNative_Test extends PHPUnit_Framework_TestCase * * @return array Test data */ - public function nativeStrlenData() + public function strlenData() { return array( array(2, "ab"), @@ -61,13 +73,13 @@ class PMA_StringNative_Test extends PHPUnit_Framework_TestCase * * @return void * @test - * @dataProvider nativeSubStrData + * @dataProvider subStrData */ public function testNativeSubStr($str, $haystack, $start, $length) { $this->assertEquals( $str, - PMA_StringNative::substr($haystack, $start, $length) + $this->testClass->substr($haystack, $start, $length) ); } @@ -76,7 +88,7 @@ class PMA_StringNative_Test extends PHPUnit_Framework_TestCase * * @return array Test data */ - public function nativeSubStrData() + public function subStrData() { return array( array("b", "ab", 1, 1), @@ -94,13 +106,13 @@ class PMA_StringNative_Test extends PHPUnit_Framework_TestCase * * @return void * @test - * @dataProvider nativeStrposData + * @dataProvider strposData */ public function testNativeStrpos($pos, $haystack, $needle, $offset) { $this->assertEquals( $pos, - PMA_StringNative::strpos($haystack, $needle, $offset) + $this->testClass->strpos($haystack, $needle, $offset) ); } @@ -125,13 +137,13 @@ class PMA_StringNative_Test extends PHPUnit_Framework_TestCase * * @return void * @test - * @dataProvider nativeStrToLowerData + * @dataProvider strToLowerData */ public function testNativeStrToLower($expected, $string) { $this->assertEquals( $expected, - PMA_StringNative::strtolower($string) + $this->testClass->strtolower($string) ); } @@ -140,11 +152,11 @@ class PMA_StringNative_Test extends PHPUnit_Framework_TestCase * * @return array Test data */ - public function nativeStrToLowerData() + public function strToLowerData() { return array( - array("mary had a", "Mary Had A", "UTF-8"), - array("test string", "TEST STRING", "UTF-8") + array("mary had a", "Mary Had A"), + array("test string", "TEST STRING") ); } } From 20e9b69b2665d428ee5ebd1b2dc5d365023e1ff7 Mon Sep 17 00:00:00 2001 From: ayushchd Date: Mon, 24 Jun 2013 14:23:05 +0545 Subject: [PATCH 3/3] Add check for multibyte extension in PMA_StringMB_test --- test/libraries/PMA_StringMB_test.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/test/libraries/PMA_StringMB_test.php b/test/libraries/PMA_StringMB_test.php index bde4d46db4..71ffc325e6 100644 --- a/test/libraries/PMA_StringMB_test.php +++ b/test/libraries/PMA_StringMB_test.php @@ -29,10 +29,14 @@ class PMA_String_Mb_Test extends PMA_StringNative_Test */ protected function setUp() { - $this->internal_encoding = mb_internal_encoding(); - mb_internal_encoding("UTF-8"); - - $this->testClass = new PMA_StringMB; + if (@function_exists('mb_strlen')) { + $this->internal_encoding = mb_internal_encoding(); + $this->testClass = new PMA_StringMB; + } else { + $this->markTestSkipped( + "Multibyte functions don't exist, skipping test." + ); + } } /** @@ -43,7 +47,9 @@ class PMA_String_Mb_Test extends PMA_StringNative_Test */ protected function tearDown() { - mb_internal_encoding($this->internal_encoding); + if (isset($this->internal_encoding)) { + mb_internal_encoding($this->internal_encoding); + } } } ?>