IconvTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\g11n\multibyte\adapter;
  9. use lithium\g11n\multibyte\adapter\Iconv;
  10. class IconvTest extends \lithium\test\Unit {
  11. public $adapter;
  12. public function skip() {
  13. $this->skipIf(!Iconv::enabled(), 'The `Iconv` adapter is not enabled.');
  14. }
  15. public function setUp() {
  16. $this->adapter = new Iconv();
  17. }
  18. public function testStrlen() {
  19. $data = 'äbc';
  20. $result = $this->adapter->strlen($data);
  21. $expected = 3;
  22. $this->assertEqual($expected, $result);
  23. }
  24. public function testStrlenAscii() {
  25. $data = 'abc';
  26. $result = $this->adapter->strlen($data);
  27. $expected = 3;
  28. $this->assertEqual($expected, $result);
  29. }
  30. public function testStrlenEmptyish() {
  31. $data = '';
  32. $result = $this->adapter->strlen($data);
  33. $expected = 0;
  34. $this->assertEqual($expected, $result);
  35. $data = ' ';
  36. $result = $this->adapter->strlen($data);
  37. $expected = 1;
  38. $this->assertEqual($expected, $result);
  39. $data = false;
  40. $result = $this->adapter->strlen($data);
  41. $expected = 0;
  42. $this->assertEqual($expected, $result);
  43. $data = null;
  44. $result = $this->adapter->strlen($data);
  45. $expected = 0;
  46. $this->assertEqual($expected, $result);
  47. $data = 0;
  48. $result = $this->adapter->strlen($data);
  49. $expected = 1;
  50. $this->assertEqual($expected, $result);
  51. $data = '0';
  52. $result = $this->adapter->strlen($data);
  53. $expected = 1;
  54. $this->assertEqual($expected, $result);
  55. }
  56. public function testStrlenInvalidTriggersError() {
  57. $this->expectException('/Detected an incomplete multibyte character in input string/');
  58. $data = "ab\xe9";
  59. $result = $this->adapter->strlen($data);
  60. }
  61. public function testStrpos() {
  62. $haystack = 'abäab';
  63. $needle = 'ä';
  64. $offset = 0;
  65. $result = $this->adapter->strpos($haystack, $needle, $offset);
  66. $expected = 2;
  67. $this->assertEqual($expected, $result);
  68. $haystack = 'abäab';
  69. $needle = 'X';
  70. $offset = 0;
  71. $result = $this->adapter->strpos($haystack, $needle, $offset);
  72. $this->assertFalse($result);
  73. }
  74. public function testStrposAscii() {
  75. $haystack = 'abcab';
  76. $needle = 'c';
  77. $offset = 0;
  78. $result = $this->adapter->strpos($haystack, $needle, $offset);
  79. $expected = 2;
  80. $this->assertEqual($expected, $result);
  81. }
  82. public function testStrposWithOffset() {
  83. $haystack = 'abäab';
  84. $needle = 'b';
  85. $offset = 0;
  86. $result = $this->adapter->strpos($haystack, $needle, $offset);
  87. $expected = 1;
  88. $this->assertEqual($expected, $result);
  89. $haystack = 'abäab';
  90. $needle = 'a';
  91. $offset = 1;
  92. $result = $this->adapter->strpos($haystack, $needle, $offset);
  93. $expected = 3;
  94. $this->assertEqual($expected, $result);
  95. }
  96. public function testStrposNeedleAsOrdinalIsNotApplied() {
  97. $haystack = 'abcab';
  98. $needle = 99;
  99. $offset = 0;
  100. $result = $this->adapter->strpos($haystack, $needle, $offset);
  101. $this->assertFalse($result);
  102. }
  103. public function testStrposTriggersError() {
  104. $haystack = "ab\xe9cab";
  105. $needle = 'c';
  106. $offset = 0;
  107. $this->expectException('/Detected an illegal character in input string/');
  108. $this->adapter->strpos($haystack, $needle, $offset);
  109. }
  110. public function testStrposInvalidOffset() {
  111. $haystack = 'abäab';
  112. $needle = 'a';
  113. $offset = -1;
  114. $this->expectException('/Offset not contained in string/');
  115. $this->adapter->strpos($haystack, $needle, $offset);
  116. }
  117. public function testStrrpos() {
  118. $haystack = 'abäab';
  119. $needle = 'ä';
  120. $result = $this->adapter->strrpos($haystack, $needle);
  121. $expected = 2;
  122. $this->assertEqual($expected, $result);
  123. $haystack = 'abäab';
  124. $needle = 'X';
  125. $result = $this->adapter->strrpos($haystack, $needle);
  126. $this->assertFalse($result);
  127. }
  128. public function testStrrposAscii() {
  129. $haystack = 'abcab';
  130. $needle = 'c';
  131. $result = $this->adapter->strrpos($haystack, $needle);
  132. $expected = 2;
  133. $this->assertEqual($expected, $result);
  134. }
  135. public function testStrrposWithOffset() {
  136. $haystack = 'abäab';
  137. $needle = 'b';
  138. $result = $this->adapter->strrpos($haystack, $needle);
  139. $expected = 4;
  140. $this->assertEqual($expected, $result);
  141. $haystack = 'abäab';
  142. $needle = 'a';
  143. $result = $this->adapter->strrpos($haystack, $needle);
  144. $expected = 3;
  145. $this->assertEqual($expected, $result);
  146. }
  147. public function testStrrposTriggersError() {
  148. $haystack = "ab\xe9cab";
  149. $needle = 'c';
  150. $this->expectException('/Detected an illegal character in input string/');
  151. $this->adapter->strrpos($haystack, $needle);
  152. }
  153. public function testSubstr() {
  154. $string = 'abäab';
  155. $start = 0;
  156. $length = 3;
  157. $result = $this->adapter->substr($string, $start, $length);
  158. $expected = 'abä';
  159. $this->assertEqual($expected, $result);
  160. $string = 'abäab';
  161. $start = 2;
  162. $length = 3;
  163. $result = $this->adapter->substr($string, $start, $length);
  164. $expected = 'äab';
  165. $this->assertEqual($expected, $result);
  166. $string = 'abäab';
  167. $start = -3;
  168. $length = 3;
  169. $result = $this->adapter->substr($string, $start, $length);
  170. $expected = 'äab';
  171. $this->assertEqual($expected, $result);
  172. }
  173. public function testSubstrAscii() {
  174. $string = 'abcab';
  175. $start = 0;
  176. $length = 3;
  177. $result = $this->adapter->substr($string, $start, $length);
  178. $expected = 'abc';
  179. $this->assertEqual($expected, $result);
  180. }
  181. public function testSubstrInvalidTriggersError() {
  182. $string = "ab\xe9cab";
  183. $start = 0;
  184. $length = 3;
  185. $this->expectException('/Detected an illegal character in input string/');
  186. $this->adapter->substr($string, $start, $length);
  187. }
  188. }
  189. ?>