StringHelperTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 implodeProvider()
  203. {
  204. return [
  205. [',', ['a', 'b', null, 'd'], 'a,b,d'],
  206. [',', ['a', 'b', '', 'd'], 'a,b,,d'],
  207. [',', ['a', 'b', 'c', 'd'], 'a,b,c,d'],
  208. [',', [null, null, null], null],
  209. [',', null, null],
  210. ['', ['a', '', 'c', 'd'], 'acd'],
  211. ['', ['a', 3, 'c', 'd'], 'a3cd'],
  212. ['', ['a', 'b', 'c', 'd'], 'abcd'],
  213. [null, ['a', 'b', 'c', 'd'], 'abcd'],
  214. ];
  215. }
  216. /**
  217. * @dataProvider implodeProvider
  218. */
  219. public function testImplode($glue, $pieces, $expected)
  220. {
  221. $this->assertSame($expected, StringHelper::implode($glue, $pieces));
  222. }
  223. public function uniteProvider()
  224. {
  225. return [
  226. [['a', 'b', 'c', 'd'], 'abcd'],
  227. [['a', 'b', '', 'd'], 'abd'],
  228. [['a', 'b', null, 'd'], 'abd'],
  229. [['a', 3, 'c', 'd'], 'a3cd'],
  230. [[null, 'b', 'c', 'd'], 'bcd'],
  231. [[2 => 'c'], 'c'],
  232. [[2 => 'c', 0 => 'a'], 'ca'],
  233. [[2 => 'c', 0 => 'a'], implode('', [3 => 'c', 0 => 'a'])],
  234. [[2 => 'c', 0 => 'a', 1 => 'b'], 'cab'],
  235. [[2 => 'c', 0 => 'a', 1 => 'b'], implode('', [2 => 'c', 0 => 'a', 1 => 'b'])],
  236. [[null, null, null], null],
  237. [null, null],
  238. ];
  239. }
  240. /**
  241. * @dataProvider uniteProvider
  242. */
  243. public function testUnite($pieces, $expected)
  244. {
  245. $this->assertSame($expected, StringHelper::unite($pieces));
  246. }
  247. public function containsAnyProvider()
  248. {
  249. return [
  250. [['a', 'b', 's'], 'string', true],
  251. [['a', 'b'], 'string', false],
  252. [['a'], '', false],
  253. [['a'], 'string', false],
  254. [['ng'], 'string', true],
  255. [['ngg', 'ri'], 'string', true],
  256. [['ngg'], 'string', false],
  257. [['sti', 'sri'], 'string', false],
  258. [['sti', 'str', 'sri'], 'string', true],
  259. [['str', 'sri'], 'string', true],
  260. [[], '', false],
  261. [[], 'string', false],
  262. ];
  263. }
  264. /**
  265. * @dataProvider containsAnyProvider
  266. */
  267. public function testContainsAny(array $needles, $haystack, $result)
  268. {
  269. $this->assertSame($result, StringHelper::containsAny($needles, $haystack));
  270. }
  271. public function containsAnyEmptyNeedleProvider()
  272. {
  273. return [
  274. [[''], ''],
  275. [[''], 'string'],
  276. [['a', '', 'c'], ''],
  277. [['a', '', 'c'], 'string'],
  278. ];
  279. }
  280. /**
  281. * @dataProvider containsAnyEmptyNeedleProvider
  282. * @expectedException InvalidArgumentException
  283. */
  284. public function testContainsAnyEmptyNeedle(array $needles, $haystack)
  285. {
  286. StringHelper::containsAny($needles, $haystack);
  287. }
  288. }