StringUtilsTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Stdlib\Test\Unit;
  7. use \Magento\Framework\Stdlib\StringUtils;
  8. /**
  9. * Magento\Framework\Stdlib\StringUtilsTest test case
  10. */
  11. class StringUtilsTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\Stdlib\StringUtils
  15. */
  16. protected $_string;
  17. protected function setUp()
  18. {
  19. $this->_string = new StringUtils();
  20. }
  21. /**
  22. * @covers \Magento\Framework\Stdlib\StringUtils::split
  23. */
  24. public function testStrSplit()
  25. {
  26. $this->assertEquals([], $this->_string->split(''));
  27. $this->assertEquals(['1', '2', '3', '4'], $this->_string->split('1234', 1));
  28. $this->assertEquals(['1', '2', ' ', '3', '4'], $this->_string->split('12 34', 1, false, true));
  29. $this->assertEquals(
  30. ['12345', '123', '12345', '6789'],
  31. $this->_string->split('12345 123 123456789', 5, true, true)
  32. );
  33. $this->assertEquals(
  34. ['1234', '5', '123', '1234', '5678', '9'],
  35. $this->_string->split('12345 123 123456789', 4, true, true)
  36. );
  37. }
  38. /**
  39. * @covers \Magento\Framework\Stdlib\StringUtils::splitInjection
  40. */
  41. public function testSplitInjection()
  42. {
  43. $string = '1234567890';
  44. $this->assertEquals('1234 5678 90', $this->_string->splitInjection($string, 4));
  45. }
  46. /**
  47. * @covers \Magento\Framework\Stdlib\StringUtils::cleanString
  48. */
  49. public function testCleanString()
  50. {
  51. $string = '12345';
  52. $this->assertEquals($string, $this->_string->cleanString($string));
  53. }
  54. public function testSubstr()
  55. {
  56. $this->assertSame('tring', $this->_string->substr('string', 1));
  57. }
  58. public function testStrrev()
  59. {
  60. $this->assertSame('0987654321', $this->_string->strrev('1234567890'));
  61. $this->assertSame('', $this->_string->strrev(''));
  62. }
  63. /**
  64. * @covers \Magento\Framework\Stdlib\StringUtils::strpos
  65. */
  66. public function testStrpos()
  67. {
  68. $this->assertEquals(1, $this->_string->strpos('123', 2));
  69. }
  70. /**
  71. * @param string $testString
  72. * @param string $expected
  73. *
  74. * @dataProvider upperCaseWordsDataProvider
  75. */
  76. public function testUpperCaseWords($testString, $expected)
  77. {
  78. $actual = $this->_string->upperCaseWords($testString);
  79. $this->assertEquals($expected, $actual);
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function upperCaseWordsDataProvider()
  85. {
  86. return [
  87. ['test test2', 'Test_Test2'],
  88. ['test_test2', 'Test_Test2'],
  89. ['test_test2 test3', 'Test_Test2_Test3']
  90. ];
  91. }
  92. /**
  93. * @param string $testString
  94. * @param string $sourceSeparator
  95. * @param string $destinationSeparator
  96. * @param string $expected
  97. *
  98. * @dataProvider upperCaseWordsWithSeparatorsDataProvider
  99. */
  100. public function testUpperCaseWordsWithSeparators($testString, $sourceSeparator, $destinationSeparator, $expected)
  101. {
  102. $actual = $this->_string->upperCaseWords($testString, $sourceSeparator, $destinationSeparator);
  103. $this->assertEquals($expected, $actual);
  104. }
  105. /**
  106. * @return array
  107. */
  108. public function upperCaseWordsWithSeparatorsDataProvider()
  109. {
  110. return [['test test2_test3\test4|test5', '|', '\\', 'Test\Test2_test3\test4\Test5']];
  111. }
  112. }