StringHelperTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. use fphammerle\helpers\StringHelper;
  4. class StringHelperTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testPrepend()
  7. {
  8. $this->assertEquals(
  9. 'prefixstring',
  10. StringHelper::prepend('prefix', 'string')
  11. );
  12. }
  13. public function testPrependToEmpty()
  14. {
  15. $this->assertEquals(
  16. 'prefix',
  17. StringHelper::prepend('prefix', '')
  18. );
  19. }
  20. public function testPrependToNull()
  21. {
  22. $this->assertEquals(
  23. null,
  24. StringHelper::prepend('prefix', null)
  25. );
  26. }
  27. public function testPrependNull()
  28. {
  29. $this->assertEquals(
  30. 'string',
  31. StringHelper::prepend(null, 'string')
  32. );
  33. }
  34. public function testPrependToList()
  35. {
  36. $this->assertEquals(
  37. ['prefix1', 'prefix2', 'prefix3'],
  38. StringHelper::prepend('prefix', ['1', '2', '3'])
  39. );
  40. }
  41. public function testPrependToDict()
  42. {
  43. $this->assertEquals(
  44. ['a' => 'prefix1', 'b' => 'prefix2', 'prefix3'],
  45. StringHelper::prepend('prefix', ['a' => '1', 'b' => '2', '3'])
  46. );
  47. }
  48. public function testPrependToDictWithNull()
  49. {
  50. $this->assertEquals(
  51. ['a' => 'prefix1', 'b' => null, 'prefix3'],
  52. StringHelper::prepend('prefix', ['a' => '1', 'b' => null, '3'])
  53. );
  54. }
  55. public function testAppend()
  56. {
  57. $this->assertEquals(
  58. 'stringsuffix',
  59. StringHelper::append('string', 'suffix')
  60. );
  61. }
  62. public function testAppendToEmpty()
  63. {
  64. $this->assertEquals(
  65. 'suffix',
  66. StringHelper::append('', 'suffix')
  67. );
  68. }
  69. public function testAppendToNull()
  70. {
  71. $this->assertEquals(
  72. null,
  73. StringHelper::append(null, 'suffix')
  74. );
  75. }
  76. public function testAppendNull()
  77. {
  78. $this->assertEquals(
  79. 'string',
  80. StringHelper::append('string', null)
  81. );
  82. }
  83. public function testAppendToList()
  84. {
  85. $this->assertEquals(
  86. ['1postfix', '2postfix', '3postfix'],
  87. StringHelper::append(['1', '2', '3'], 'postfix')
  88. );
  89. }
  90. public function testAppendToDict()
  91. {
  92. $this->assertEquals(
  93. ['a' => '1postfix', 'b' => '2postfix', '3postfix'],
  94. StringHelper::append(['a' => '1', 'b' => '2', '3'], 'postfix')
  95. );
  96. }
  97. public function testAppendToDictWithNull()
  98. {
  99. $this->assertEquals(
  100. ['a' => '1postfix', 'b' => null, '3postfix'],
  101. StringHelper::append(['a' => '1', 'b' => null, '3'], 'postfix')
  102. );
  103. }
  104. public function testEmbed()
  105. {
  106. $this->assertEquals(
  107. 'prefixstringsuffix',
  108. StringHelper::embed('prefix', 'string', 'suffix')
  109. );
  110. }
  111. public function testEmbedToEmpty()
  112. {
  113. $this->assertEquals(
  114. 'prefixsuffix',
  115. StringHelper::embed('prefix', '', 'suffix')
  116. );
  117. }
  118. public function testEmbedToNull()
  119. {
  120. $this->assertEquals(
  121. null,
  122. StringHelper::embed('prefix', null, 'suffix')
  123. );
  124. }
  125. public function testEmbedNull()
  126. {
  127. $this->assertEquals(
  128. 'string',
  129. StringHelper::embed(null, 'string', null)
  130. );
  131. }
  132. public function testEmbedToList()
  133. {
  134. $this->assertEquals(
  135. ['prefix1postfix', 'prefix2postfix', 'prefix3postfix'],
  136. StringHelper::embed('prefix', ['1', '2', '3'], 'postfix')
  137. );
  138. }
  139. public function testEmbedToDict()
  140. {
  141. $this->assertEquals(
  142. ['a' => 'prefix1postfix', 'b' => 'prefix2postfix', 'prefix3postfix'],
  143. StringHelper::embed('prefix', ['a' => '1', 'b' => '2', '3'], 'postfix')
  144. );
  145. }
  146. public function testEmbedToDictWithNull()
  147. {
  148. $this->assertEquals(
  149. ['a' => 'prefix1postfix', 'b' => null, 'prefix3postfix'],
  150. StringHelper::embed('prefix', ['a' => '1', 'b' => null, '3'], 'postfix')
  151. );
  152. }
  153. public function testEmbrace()
  154. {
  155. $this->assertEquals(
  156. 'bracestringbrace',
  157. StringHelper::embrace('brace', 'string')
  158. );
  159. }
  160. public function testEmbraceToEmpty()
  161. {
  162. $this->assertEquals(
  163. 'bracebrace',
  164. StringHelper::embrace('brace', '')
  165. );
  166. }
  167. public function testEmbraceToNull()
  168. {
  169. $this->assertEquals(
  170. null,
  171. StringHelper::embrace('brace', null)
  172. );
  173. }
  174. public function testEmbraceNull()
  175. {
  176. $this->assertEquals(
  177. 'string',
  178. StringHelper::embrace(null, 'string')
  179. );
  180. }
  181. public function testEmbraceToList()
  182. {
  183. $this->assertEquals(
  184. ['brace1brace', 'brace2brace', 'brace3brace'],
  185. StringHelper::embrace('brace', ['1', '2', '3'])
  186. );
  187. }
  188. public function testEmbraceToDict()
  189. {
  190. $this->assertEquals(
  191. ['a' => 'brace1brace', 'b' => 'brace2brace', 'brace3brace'],
  192. StringHelper::embrace('brace', ['a' => '1', 'b' => '2', '3'])
  193. );
  194. }
  195. public function testEmbraceToDictWithNull()
  196. {
  197. $this->assertEquals(
  198. ['a' => 'brace1brace', 'b' => null, 'brace3brace'],
  199. StringHelper::embrace('brace', ['a' => '1', 'b' => null, '3'])
  200. );
  201. }
  202. public function testImplode()
  203. {
  204. $this->assertEquals(
  205. 'a,b,c,d',
  206. StringHelper::implode(',', ['a', 'b', 'c', 'd'])
  207. );
  208. }
  209. public function testImplodeWithNull()
  210. {
  211. $this->assertEquals(
  212. 'a,b,d',
  213. StringHelper::implode(',', ['a', 'b', null, 'd'])
  214. );
  215. }
  216. public function testImplodeEmpty()
  217. {
  218. $this->assertEquals(
  219. 'acd',
  220. StringHelper::implode('', ['a', '', 'c', 'd'])
  221. );
  222. }
  223. public function testImplodeByEmpty()
  224. {
  225. $this->assertEquals(
  226. 'abcd',
  227. StringHelper::implode('', ['a', 'b', 'c', 'd'])
  228. );
  229. }
  230. public function testImplodeNothing()
  231. {
  232. $this->assertEquals(
  233. null,
  234. StringHelper::implode(',', [null, null, null])
  235. );
  236. }
  237. public function containsAnyProvider()
  238. {
  239. return [
  240. [['a', 'b', 's'], 'string', true],
  241. [['a', 'b'], 'string', false],
  242. [['a'], '', false],
  243. [['a'], 'string', false],
  244. [['ng'], 'string', true],
  245. [['ngg', 'ri'], 'string', true],
  246. [['ngg'], 'string', false],
  247. [['sti', 'sri'], 'string', false],
  248. [['sti', 'str', 'sri'], 'string', true],
  249. [['str', 'sri'], 'string', true],
  250. [[], '', false],
  251. [[], 'string', false],
  252. ];
  253. }
  254. /**
  255. * @dataProvider containsAnyProvider
  256. */
  257. public function testContainsAny(array $needles, $haystack, $result)
  258. {
  259. $this->assertSame($result, StringHelper::containsAny($needles, $haystack));
  260. }
  261. public function containsAnyEmptyNeedleProvider()
  262. {
  263. return [
  264. [[''], ''],
  265. [[''], 'string'],
  266. [['a', '', 'c'], ''],
  267. [['a', '', 'c'], 'string'],
  268. ];
  269. }
  270. /**
  271. * @dataProvider containsAnyEmptyNeedleProvider
  272. * @expectedException InvalidArgumentException
  273. */
  274. public function testContainsAnyEmptyNeedle(array $needles, $haystack)
  275. {
  276. StringHelper::containsAny($needles, $haystack);
  277. }
  278. }