MultiByteTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Text
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Text_MultiByteTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Text_MultiByteTest::main");
  25. }
  26. /**
  27. * Zend_Text_MultiByte
  28. */
  29. require_once 'Zend/Text/MultiByte.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Text
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Text
  37. */
  38. class Zend_Text_MultiByteTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Runs the test methods of this class.
  42. *
  43. * @return void
  44. */
  45. public static function main()
  46. {
  47. $suite = new PHPUnit_Framework_TestSuite("Zend_Text_MultiByteTest");
  48. $result = PHPUnit_TextUI_TestRunner::run($suite);
  49. }
  50. /**
  51. * Standard cut tests
  52. */
  53. public function testWordWrapCutSingleLine()
  54. {
  55. $line = Zend_Text_MultiByte::wordWrap('äbüöcß', 2, ' ', true);
  56. $this->assertEquals('äb üö cß', $line);
  57. }
  58. public function testWordWrapCutMultiLine()
  59. {
  60. $line = Zend_Text_MultiByte::wordWrap('äbüöc ß äbüöcß', 2, ' ', true);
  61. $this->assertEquals('äb üö c ß äb üö cß', $line);
  62. }
  63. public function testWordWrapCutMultiLineShortWords()
  64. {
  65. $line = Zend_Text_MultiByte::wordWrap('Ä very long wöööööööööööörd.', 8, "\n", true);
  66. $this->assertEquals("Ä very\nlong\nwööööööö\nööööörd.", $line);
  67. }
  68. public function testWordWrapCutMultiLineWithPreviousNewlines()
  69. {
  70. $line = Zend_Text_MultiByte::wordWrap("Ä very\nlong wöööööööööööörd.", 8, "\n", false);
  71. $this->assertEquals("Ä very\nlong\nwöööööööööööörd.", $line);
  72. }
  73. /**
  74. * Long-Break tests
  75. */
  76. public function testWordWrapLongBreak()
  77. {
  78. $line = Zend_Text_MultiByte::wordWrap("Ä very<br>long wöö<br>öööööööö<br>öörd.", 8, '<br>', false);
  79. $this->assertEquals("Ä very<br>long wöö<br>öööööööö<br>öörd.", $line);
  80. }
  81. /**
  82. * Alternative cut tests
  83. */
  84. public function testWordWrapCutBeginningSingleSpace()
  85. {
  86. $line = Zend_Text_MultiByte::wordWrap(' äüöäöü', 3, ' ', true);
  87. $this->assertEquals(' äüö äöü', $line);
  88. }
  89. public function testWordWrapCutEndingSingleSpace()
  90. {
  91. $line = Zend_Text_MultiByte::wordWrap('äüöäöü ', 3, ' ', true);
  92. $this->assertEquals('äüö äöü ', $line);
  93. }
  94. public function testWordWrapCutEndingSingleSpaceWithNonSpaceDivider()
  95. {
  96. $line = Zend_Text_MultiByte::wordWrap('äöüäöü ', 3, '-', true);
  97. $this->assertEquals('äöü-äöü-', $line);
  98. }
  99. public function testWordWrapCutEndingTwoSpaces()
  100. {
  101. $line = Zend_Text_MultiByte::wordWrap('äüöäöü ', 3, ' ', true);
  102. $this->assertEquals('äüö äöü ', $line);
  103. }
  104. public function testWordWrapNoCutEndingSingleSpace()
  105. {
  106. $line = Zend_Text_Multibyte::wordWrap('12345 ', 5, '-', false);
  107. $this->assertEquals('12345-', $line);
  108. }
  109. public function testWordWrapNoCutEndingTwoSpaces()
  110. {
  111. $line = Zend_Text_MultiByte::wordWrap('12345 ', 5, '-', false);
  112. $this->assertEquals('12345- ', $line);
  113. }
  114. public function testWordWrapCutEndingThreeSpaces()
  115. {
  116. $line = Zend_Text_MultiByte::wordWrap('äüöäöü ', 3, ' ', true);
  117. $this->assertEquals('äüö äöü ', $line);
  118. }
  119. public function testWordWrapCutEndingTwoBreaks()
  120. {
  121. $line = Zend_Text_MultiByte::wordWrap('äüöäöü--', 3, '-', true);
  122. $this->assertEquals('äüö-äöü--', $line);
  123. }
  124. public function testWordWrapCutTab()
  125. {
  126. $line = Zend_Text_MultiByte::wordWrap("äbü\töcß", 3, ' ', true);
  127. $this->assertEquals("äbü \töc ß", $line);
  128. }
  129. public function testWordWrapCutNewlineWithSpace()
  130. {
  131. $line = Zend_Text_MultiByte::wordWrap("äbü\nößt", 3, ' ', true);
  132. $this->assertEquals("äbü \nöß t", $line);
  133. }
  134. public function testWordWrapCutNewlineWithNewline()
  135. {
  136. $line = Zend_Text_MultiByte::wordWrap("äbü\nößte", 3, "\n", true);
  137. $this->assertEquals("äbü\nößt\ne", $line);
  138. }
  139. /**
  140. * Break cut tests
  141. */
  142. public function testWordWrapCutBreakBefore()
  143. {
  144. $line = Zend_Text_MultiByte::wordWrap('foobar-foofoofoo', 8, '-', true);
  145. $this->assertEquals('foobar-foofoofo-o', $line);
  146. }
  147. public function testWordWrapCutBreakWith()
  148. {
  149. $line = Zend_Text_MultiByte::wordWrap('foobar-foobar', 6, '-', true);
  150. $this->assertEquals('foobar-foobar', $line);
  151. }
  152. public function testWordWrapCutBreakWithin()
  153. {
  154. $line = Zend_Text_MultiByte::wordWrap('foobar-foobar', 7, '-', true);
  155. $this->assertEquals('foobar-foobar', $line);
  156. }
  157. public function testWordWrapCutBreakWithinEnd()
  158. {
  159. $line = Zend_Text_MultiByte::wordWrap('foobar-', 7, '-', true);
  160. $this->assertEquals('foobar-', $line);
  161. }
  162. public function testWordWrapCutBreakAfter()
  163. {
  164. $line = Zend_Text_MultiByte::wordWrap('foobar-foobar', 5, '-', true);
  165. $this->assertEquals('fooba-r-fooba-r', $line);
  166. }
  167. /**
  168. * Standard no-cut tests
  169. */
  170. public function testWordWrapNoCutSingleLine()
  171. {
  172. $line = Zend_Text_MultiByte::wordWrap('äbüöcß', 2, ' ', false);
  173. $this->assertEquals('äbüöcß', $line);
  174. }
  175. public function testWordWrapNoCutMultiLine()
  176. {
  177. $line = Zend_Text_MultiByte::wordWrap('äbüöc ß äbüöcß', 2, "\n", false);
  178. $this->assertEquals("äbüöc\nß\näbüöcß", $line);
  179. }
  180. public function testWordWrapNoCutMultiWord()
  181. {
  182. $line = Zend_Text_MultiByte::wordWrap('äöü äöü äöü', 5, "\n", false);
  183. $this->assertEquals("äöü\näöü\näöü", $line);
  184. }
  185. /**
  186. * Break no-cut tests
  187. */
  188. public function testWordWrapNoCutBreakBefore()
  189. {
  190. $line = Zend_Text_MultiByte::wordWrap('foobar-foofoofoo', 8, '-', false);
  191. $this->assertEquals('foobar-foofoofoo', $line);
  192. }
  193. public function testWordWrapNoCutBreakWith()
  194. {
  195. $line = Zend_Text_MultiByte::wordWrap('foobar-foobar', 6, '-', false);
  196. $this->assertEquals('foobar-foobar', $line);
  197. }
  198. public function testWordWrapNoCutBreakWithin()
  199. {
  200. $line = Zend_Text_MultiByte::wordWrap('foobar-foobar', 7, '-', false);
  201. $this->assertEquals('foobar-foobar', $line);
  202. }
  203. public function testWordWrapNoCutBreakWithinEnd()
  204. {
  205. $line = Zend_Text_MultiByte::wordWrap('foobar-', 7, '-', false);
  206. $this->assertEquals('foobar-', $line);
  207. }
  208. public function testWordWrapNoCutBreakAfter()
  209. {
  210. $line = Zend_Text_MultiByte::wordWrap('foobar-foobar', 5, '-', false);
  211. $this->assertEquals('foobar-foobar', $line);
  212. }
  213. /**
  214. * Pad tests
  215. */
  216. public function testLeftPad()
  217. {
  218. $text = Zend_Text_MultiByte::strPad('äää', 5, 'ö', STR_PAD_LEFT);
  219. $this->assertEquals('ööäää', $text);
  220. }
  221. public function testCenterPad()
  222. {
  223. $text = Zend_Text_MultiByte::strPad('äää', 6, 'ö', STR_PAD_BOTH);
  224. $this->assertEquals('öäääöö', $text);
  225. }
  226. public function testRightPad()
  227. {
  228. $text = Zend_Text_MultiByte::strPad('äääöö', 5, 'ö', STR_PAD_RIGHT);
  229. $this->assertEquals('äääöö', $text);
  230. }
  231. /**
  232. * @group ZF-12186
  233. */
  234. public function testPadInputLongerThanPadLength()
  235. {
  236. $text = Zend_Text_MultiByte::strPad('äääöö', 2, 'ö');
  237. $this->assertEquals('äääöö', $text);
  238. }
  239. /**
  240. * @group ZF-12186
  241. */
  242. public function testPadInputSameAsPadLength()
  243. {
  244. $text = Zend_Text_MultiByte::strPad('äääöö', 5, 'ö');
  245. $this->assertEquals('äääöö', $text);
  246. }
  247. /**
  248. * @group ZF-12186
  249. */
  250. public function testPadNegativePadLength()
  251. {
  252. $text = Zend_Text_MultiByte::strPad('äääöö', -2, 'ö');
  253. $this->assertEquals('äääöö', $text);
  254. }
  255. }
  256. // Call Zend_Text_MultiByteTest::main() if this source file is executed directly.
  257. if (PHPUnit_MAIN_METHOD == "Zend_Text_MultiByteTest::main") {
  258. Zend_Text_MultiByteTest::main();
  259. }