MbstringTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\Mbstring;
  10. class MbstringTest extends \lithium\test\Unit {
  11. public $adapter;
  12. public function skip() {
  13. $this->skipIf(!Mbstring::enabled(), 'The `Mbstring` adapter is not enabled.');
  14. }
  15. public function setUp() {
  16. $this->adapter = new Mbstring();
  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 testStrlenInvalid() {
  57. $data = "ab\xe9";
  58. $result = $this->adapter->strlen($data);
  59. $expected = 3;
  60. $this->assertEqual($expected, $result);
  61. }
  62. public function testStrpos() {
  63. $haystack = 'abäab';
  64. $needle = 'ä';
  65. $offset = 0;
  66. $result = $this->adapter->strpos($haystack, $needle, $offset);
  67. $expected = 2;
  68. $this->assertEqual($expected, $result);
  69. $haystack = 'abäab';
  70. $needle = 'X';
  71. $offset = 0;
  72. $result = $this->adapter->strpos($haystack, $needle, $offset);
  73. $this->assertFalse($result);
  74. }
  75. public function testStrposAscii() {
  76. $haystack = 'abcab';
  77. $needle = 'c';
  78. $offset = 0;
  79. $result = $this->adapter->strpos($haystack, $needle, $offset);
  80. $expected = 2;
  81. $this->assertEqual($expected, $result);
  82. }
  83. public function testStrposWithOffset() {
  84. $haystack = 'abäab';
  85. $needle = 'b';
  86. $offset = 0;
  87. $result = $this->adapter->strpos($haystack, $needle, $offset);
  88. $expected = 1;
  89. $this->assertEqual($expected, $result);
  90. $haystack = 'abäab';
  91. $needle = 'a';
  92. $offset = 1;
  93. $result = $this->adapter->strpos($haystack, $needle, $offset);
  94. $expected = 3;
  95. $this->assertEqual($expected, $result);
  96. }
  97. public function testStrposNeedleAsOrdinalIsNotApplied() {
  98. $haystack = 'abcab';
  99. $needle = 99;
  100. $offset = 0;
  101. $result = $this->adapter->strpos($haystack, $needle, $offset);
  102. $this->assertFalse($result);
  103. }
  104. public function testStrposInvalidIsIgnored() {
  105. $haystack = "ab\xe9cab";
  106. $needle = 'c';
  107. $offset = 0;
  108. $result = $this->adapter->strpos($haystack, $needle, $offset);
  109. $expected = 3;
  110. $this->assertEqual($expected, $result);
  111. }
  112. public function testStrposInvalidOffset() {
  113. $haystack = 'abäab';
  114. $needle = 'a';
  115. $offset = -1;
  116. $this->expectException('/Offset not contained in string/');
  117. $this->adapter->strpos($haystack, $needle, $offset);
  118. }
  119. public function testStrrpos() {
  120. $haystack = 'abäab';
  121. $needle = 'ä';
  122. $result = $this->adapter->strrpos($haystack, $needle);
  123. $expected = 2;
  124. $this->assertEqual($expected, $result);
  125. $haystack = 'abäab';
  126. $needle = 'X';
  127. $result = $this->adapter->strrpos($haystack, $needle);
  128. $this->assertFalse($result);
  129. }
  130. public function testStrrposAscii() {
  131. $haystack = 'abcab';
  132. $needle = 'c';
  133. $result = $this->adapter->strrpos($haystack, $needle);
  134. $expected = 2;
  135. $this->assertEqual($expected, $result);
  136. }
  137. public function testStrrposWithOffset() {
  138. $haystack = 'abäab';
  139. $needle = 'b';
  140. $result = $this->adapter->strrpos($haystack, $needle);
  141. $expected = 4;
  142. $this->assertEqual($expected, $result);
  143. $haystack = 'abäab';
  144. $needle = 'a';
  145. $result = $this->adapter->strrpos($haystack, $needle);
  146. $expected = 3;
  147. $this->assertEqual($expected, $result);
  148. }
  149. public function testStrrposInvalidIsIgnored() {
  150. $haystack = "ab\xe9cab";
  151. $needle = 'c';
  152. $result = $this->adapter->strrpos($haystack, $needle);
  153. $expected = 3;
  154. $this->assertEqual($expected, $result);
  155. }
  156. public function testSubstr() {
  157. $string = 'abäab';
  158. $start = 0;
  159. $length = 3;
  160. $result = $this->adapter->substr($string, $start, $length);
  161. $expected = 'abä';
  162. $this->assertEqual($expected, $result);
  163. $string = 'abäab';
  164. $start = 2;
  165. $length = 3;
  166. $result = $this->adapter->substr($string, $start, $length);
  167. $expected = 'äab';
  168. $this->assertEqual($expected, $result);
  169. $string = 'abäab';
  170. $start = -3;
  171. $length = 3;
  172. $result = $this->adapter->substr($string, $start, $length);
  173. $expected = 'äab';
  174. $this->assertEqual($expected, $result);
  175. }
  176. public function testSubstrAscii() {
  177. $string = 'abcab';
  178. $start = 0;
  179. $length = 3;
  180. $result = $this->adapter->substr($string, $start, $length);
  181. $expected = 'abc';
  182. $this->assertEqual($expected, $result);
  183. }
  184. public function testSubstrInvalidProducesUnexpectedResult() {
  185. $string = "ab\xe9cab";
  186. $start = 0;
  187. $length = 3;
  188. $result = $this->adapter->substr($string, $start, $length);
  189. $expected = "ab\xe9ca";
  190. $this->assertEqual($expected, $result);
  191. }
  192. }
  193. ?>