PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/old/test_multibyte.php

https://github.com/psaintlaurent/Habari
PHP | 117 lines | 102 code | 8 blank | 7 comment | 0 complexity | 92b0a289052c3303e2a38b5f610a03a9 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Unit Test for the MultiByte class.
  4. */
  5. include 'bootstrap.php';
  6. class MultiByteTest extends UnitTestCase
  7. {
  8. // static $test_str = 'Iñtërnâtiônàlizætiøn';
  9. static $test_str = 'n2â7t Iñtërnâtiônàlizætiøn l13izæ42tiøn';
  10. static $test_str_slug = 'n2â7t Iñtërnâtiônàlizætiøn l13izæ42tiøn';
  11. static $test_str_slug_asc = 'fasfgreig rr23 34vfg9';
  12. // static $test_str = 'Track';
  13. // static $test_str = '汉字测试';
  14. // static $test_str = 'بما في ذلك الكلمات المستخدمة في صفحات التيكت والويكي';
  15. function test_hab_encoding()
  16. {
  17. $this->assert_equal( MultiByte::hab_encoding(), 'UTF-8' );
  18. printf( "MultiByte internal encoding: %s <br>", MultiByte::hab_encoding() );
  19. printf( "mbstring internal encoding: %s <br>", mb_internal_encoding() );
  20. }
  21. function test_convert_encoding()
  22. {
  23. printf( "Test string: %s <br>", self::$test_str );
  24. $this->assert_equal( MultiByte::convert_encoding( self::$test_str ), mb_convert_encoding( self::$test_str, 'UTF-8', mb_detect_encoding( self::$test_str ) ) );
  25. printf( "After being converted to MultiByte's encoding: %s <br>", MultiByte::convert_encoding( self::$test_str ) );
  26. printf( "After being converted by mbstring to MultiByte's encoding without detecting encoding: %s <br>", mb_convert_encoding( self::$test_str, MultiByte::hab_encoding() ) );
  27. printf( "After being converted by mbstring to MultiByte's encoding after detecting encoding: %s <br>", mb_convert_encoding( self::$test_str, MultiByte::hab_encoding(), mb_detect_encoding( self::$test_str ) ) );
  28. $this->assert_equal( MultiByte::convert_encoding( self::$test_str, 'ASCII' ), mb_convert_encoding( self::$test_str, 'ASCII', mb_detect_encoding( self::$test_str ) ) );
  29. printf( "MultiByte convert to ASCII: %s <br>", MultiByte::convert_encoding( self::$test_str, 'ASCII' ) );
  30. printf( "mbstring convert to ASCII without detecting encoding: %s <br>", mb_convert_encoding( self::$test_str, 'ASCII' ) );
  31. printf( "mbstring convert to ASCII after detecting encoding: %s <br>", mb_convert_encoding( self::$test_str, 'ASCII', mb_detect_encoding( self::$test_str) ) );
  32. }
  33. function test_detect_encoding()
  34. {
  35. $this->assert_equal( MultiByte::detect_encoding( self::$test_str ), mb_detect_encoding( self::$test_str ) );
  36. printf( "MultiByte detected encoding: %s <br>", MultiByte::detect_encoding( self::$test_str ) );
  37. printf( "mbstring detect order: %s <br>", implode( ', ', mb_detect_order() ) );
  38. printf( "mbstring detected encoding: %s <br>", mb_detect_encoding( self::$test_str ) );
  39. }
  40. function test_substr()
  41. {
  42. printf( "Test string: %s <br>", self::$test_str );
  43. printf("Habari encoding: %s <br>", MultiByte::hab_encoding() );
  44. printf( "mb_internal_encoding: %s <br>", mb_internal_encoding() );
  45. printf( "MultiByte detected encoding of test string: %s <br>", MultiByte::detect_encoding( self::$test_str ) );
  46. printf( "mbstring detected encoding of test string: %s <br>", mb_detect_encoding( self::$test_str ) );
  47. $this->assert_equal( MultiByte::substr( self::$test_str, 1, 3 ), mb_substr( self::$test_str, 1, 3 ) );
  48. $this->assert_equal( MultiByte::substr( self::$test_str, 1, 3 ), mb_substr( self::$test_str, 1, 3, mb_detect_encoding( self::$test_str ) ) );
  49. $this->assert_equal( MultiByte::substr( self::$test_str, 5 ), mb_substr( self::$test_str, 5 ) );
  50. printf( " MultiByte substring (begin-1 end-3): %s <br>", MultiByte::substr( self::$test_str, 1, 3 ) );
  51. printf( " MultiByte substring 2 (begin-5 end-null): %s <br>", MultiByte::substr( self::$test_str, 5 ) );
  52. printf( " mbstring substring without encoding detected (begin-1 end-3): %s <br>", mb_substr( self::$test_str, 1, 3 ) );
  53. printf( " mbstring substring with encoding detected (begin-1 end-3): %s <br>", mb_substr( self::$test_str, 1, 3, mb_detect_encoding( self::$test_str ) ) );
  54. printf( " mbstring substring 2 without encoding detected(begin-5 end-null): %s <br>", mb_substr( self::$test_str, 5 ) );
  55. }
  56. function test_strlen()
  57. {
  58. $this->assert_equal( MultiByte::strlen( self::$test_str ), mb_strlen( self::$test_str, mb_detect_encoding( self::$test_str ) ) );
  59. printf( "Test string: %s <br>", self::$test_str );
  60. printf( "MultiByte string length: %d <br>", MultiByte::strlen( self::$test_str ) );
  61. printf( "mbstring string length without detecting encoding: %d <br>", mb_strlen( self::$test_str ) );
  62. printf( "mbstring string length with detecting encoding: %d <br>", mb_strlen( self::$test_str, mb_detect_encoding( self::$test_str ) ) );
  63. }
  64. function test_strtoupper()
  65. {
  66. $this->assert_equal( MultiByte::strtoupper( self::$test_str ), mb_strtoupper( mb_convert_encoding(self::$test_str, 'UTF-8', mb_detect_encoding( self::$test_str ) ), 'UTF-8' ) );
  67. printf( "Test string: %s <br>", self::$test_str );
  68. printf( "MultiByte strtoupper: %s <br>", MultiByte::strtoupper( self::$test_str ) );
  69. printf( "mbstring strtoupper without detecting encoding: %s <br>", mb_strtoupper( self::$test_str ) );
  70. printf( "mstring strtoupper with detecting encoding: %s <br>", mb_strtoupper( self::$test_str, mb_detect_encoding( self::$test_str ) ) );
  71. }
  72. function test_strtolower()
  73. {
  74. $this->assert_equal( MultiByte::strtolower( self::$test_str ), mb_strtolower( mb_convert_encoding(self::$test_str, 'UTF-8', mb_detect_encoding( self::$test_str ) ), 'UTF-8' ) );
  75. printf( "Test string: %s <br>", self::$test_str );
  76. printf( "MultiByte strtolower: %s <br>", MultiByte::strtolower( self::$test_str ) );
  77. printf( "mbstring strtolower without detecting encoding: %s <br>", mb_strtolower( self::$test_str ) );
  78. printf( "mstring strtolower with detecting encoding: %s <br><br>", mb_strtolower( self::$test_str, mb_detect_encoding( self::$test_str ) ) );
  79. }
  80. /*
  81. function test_slugify()
  82. {
  83. print_r( Utils::slugify( self::$test_str_slug ) );
  84. echo "\r\n : ";
  85. print_r( urlencode( Utils::slugify( self::$test_str_slug ) ) );
  86. echo "\r\n : ";
  87. print_r( urldecode( urlencode( Utils::slugify( self::$test_str_slug ) ) ) );
  88. echo "\r\n : ";
  89. print_r( Utils::slugify( self::$test_str_slug_asc ) );
  90. echo "\r\n : ";
  91. print_r( urlencode( Utils::slugify( self::$test_str_slug_asc ) ) );
  92. }
  93. */
  94. }
  95. MultiByteTest::run_one('MultiByteTest');
  96. ?>