StringTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @CreateTime: 2019/9/14 下午10:41
  4. * @Author: huizhang <tuzisir@163.com>
  5. * @Copyright: copyright(2019) Easyswoole all rights reserved
  6. * @Description: SplString 单元测试
  7. */
  8. namespace EasySwoole\Spl\Test;
  9. use EasySwoole\Spl\SplString;
  10. use PHPUnit\Framework\TestCase;
  11. class StringTest extends TestCase {
  12. public function testSetString() {
  13. $splString = new SplString();
  14. $splString->setString('Easyswoole');
  15. $this->assertEquals('Easyswoole', $splString->__toString());
  16. }
  17. public function testSplit() {
  18. $splString = new SplString('Hello, Easyswoole');
  19. $this->assertEquals([
  20. 'Hello', ', Eas', 'yswoo', 'le'
  21. ], $splString->split(5)->getArrayCopy());
  22. }
  23. public function testExplode() {
  24. $splString = new SplString('Hello, Easyswoole');
  25. $this->assertEquals([
  26. 'Hello', 'Easyswoole'
  27. ], $splString->explode(', ')->getArrayCopy());
  28. }
  29. public function testSubString() {
  30. $splString = new SplString('Hello, Easyswoole');
  31. $this->assertEquals('Hello', $splString->subString(0, 5)->__toString());
  32. }
  33. public function testEncodingConvert() {
  34. $splString = new SplString('Hello, Easyswoole');
  35. $this->assertEquals('Hello, Easyswoole', $splString->encodingConvert('UTF-8')->__toString());
  36. }
  37. public function testUtf8() {
  38. $splString = new SplString('Hello, Easyswoole');
  39. $this->assertEquals('Hello, Easyswoole', $splString->utf8()->__toString());
  40. }
  41. public function testUnicodeToUtf8() {
  42. $splString = new SplString('Hello, Easyswoole');
  43. $this->assertEquals('Hello, Easyswoole', $splString->unicodeToUtf8()->__toString());
  44. }
  45. public function testToUnicode() {
  46. $splString = new SplString('Hello, Easyswoole');
  47. $this->assertEquals('\U0048\U0065\U006C\U006C\U006F\U002C\U0020\U0045\U0061\U0073\U0079\U0073\U0077\U006F\U006F\U006C\U0065', $splString->toUnicode()->__toString());
  48. }
  49. public function testCompare() {
  50. $splString = new SplString('Hello, Easyswoole');
  51. $this->assertEquals(-18, $splString->compare('Hello, Es'));
  52. }
  53. public function testLtrim() {
  54. $splString = new SplString(' es');
  55. $this->assertEquals('es', $splString->lTrim());
  56. }
  57. public function testRtrim() {
  58. $splString = new SplString('es ');
  59. $this->assertEquals('es', $splString->rTrim());
  60. }
  61. public function testTrime() {
  62. $splString = new SplString(' es ');
  63. $this->assertEquals('es', $splString->trim());
  64. }
  65. public function testPad() {
  66. $splString = new SplString('Easy');
  67. $splString->pad(10, 'swoole');
  68. $this->assertEquals('Easyswoole', $splString->__toString());
  69. $splString->pad(16, 'Hello,', STR_PAD_LEFT);
  70. $this->assertEquals('Hello,Easyswoole', $splString->__toString());
  71. $splString->pad(18, '@', STR_PAD_BOTH);
  72. $this->assertEquals('@Hello,Easyswoole@', $splString->__toString());
  73. }
  74. public function testRepeat() {
  75. $splString = new SplString('EasySwoole');
  76. $splString->repeat(2);
  77. $this->assertEquals('EasySwooleEasySwoole', $splString->__toString());
  78. }
  79. public function testLength() {
  80. $splString = new SplString('EasySwoole');
  81. $this->assertEquals(10, $splString->length());
  82. }
  83. public function testUpper() {
  84. $splString = new SplString('EasySwoole');
  85. $this->assertEquals('EASYSWOOLE', $splString->upper());
  86. }
  87. public function testLower() {
  88. $splString = new SplString('EasySwoole');
  89. $this->assertEquals('easyswoole', $splString->lower());
  90. }
  91. public function testStripTags() {
  92. $splString = new SplString('<span>Easyswoole</span>');
  93. $this->assertEquals('Easyswoole', $splString->stripTags()->__toString());
  94. }
  95. public function testReplace() {
  96. $splString = new SplString('Hello, es!');
  97. $this->assertEquals('Hello, Easyswoole!', $splString->replace('es', 'Easyswoole'));
  98. }
  99. public function testBetween() {
  100. $splString = new SplString('Hello,Easyswoole');
  101. $this->assertEquals(',', $splString->between('Hello', 'Easyswoole')->__toString());
  102. }
  103. public function testRegex() {
  104. $splString = new SplString('Hello,Easyswoole');
  105. $this->assertEquals('Easyswoole', $splString->regex('/Easyswoole/'));
  106. }
  107. public function testExist() {
  108. $splString = new SplString('Hello,Easyswoole');
  109. $this->assertTrue($splString->exist('Easyswoole'));
  110. }
  111. public function testKebab() {
  112. $splString = new SplString('EasySwoole');
  113. $this->assertEquals('easy-swoole', $splString->kebab()->__toString());
  114. }
  115. public function testSnake() {
  116. $splString = new SplString('EasySwoole');
  117. $this->assertEquals('easy_swoole', $splString->snake()->__toString());
  118. }
  119. public function testStudly() {
  120. $splString = new SplString('easy_swoole');
  121. $this->assertEquals('EasySwoole', $splString->studly()->__toString());
  122. }
  123. public function testCamel() {
  124. $splString = new SplString('easy_swoole');
  125. $this->assertEquals('easySwoole', $splString->camel()->__toString());
  126. }
  127. public function testReplaceArray() {
  128. $splString = new SplString('easy_easy_easy');
  129. $this->assertEquals('as_bs_cs', $splString->replaceArray('easy', ['as', 'bs', 'cs'])->__toString());
  130. }
  131. public function testReplaceFirst() {
  132. $splString = new SplString('easy_easy_easy');
  133. $this->assertEquals('as_easy_easy', $splString->replaceFirst('easy', 'as')->__toString());
  134. }
  135. public function testReplaceLast() {
  136. $splString = new SplString('easy_easy_easy');
  137. $this->assertEquals('easy_easy_as', $splString->replaceLast('easy', 'as')->__toString());
  138. }
  139. public function testStart() {
  140. $splString = new SplString('Easyswoole');
  141. $this->assertEquals('Hello,Easyswoole', $splString->start('Hello,')->__toString());
  142. }
  143. public function testAfter() {
  144. $splString = new SplString('Hello,Easyswoole');
  145. $this->assertEquals('Easyswoole', $splString->after('Hello,')->__toString());
  146. }
  147. public function testBefore() {
  148. $splString = new SplString('Hello,Easyswoole');
  149. $this->assertEquals('Hello,', $splString->before('Easyswoole')->__toString());
  150. }
  151. public function testEndsWith() {
  152. $splString = new SplString('Hello,Easyswoole');
  153. $this->assertTrue($splString->endsWith('Easyswoole'));
  154. }
  155. public function testStartsWith() {
  156. $splString = new SplString('Hello,Easyswoole');
  157. $this->assertTrue($splString->startsWith('Hello'));
  158. }
  159. }