TextTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Tools\Test\TestCase\Utility;
  3. use Shim\TestSuite\TestCase;
  4. use Tools\Utility\Text;
  5. class TextTest extends TestCase {
  6. /**
  7. * @var \Tools\Utility\Text;
  8. */
  9. protected $Text;
  10. /**
  11. * @return void
  12. */
  13. public function setUp(): void {
  14. parent::setUp();
  15. $this->Text = new Text();
  16. }
  17. /**
  18. * @return void
  19. */
  20. public function testReadTab() {
  21. $data = <<<TXT
  22. some tabbed data
  23. and another line
  24. TXT;
  25. $result = $this->Text->readTab($data);
  26. $this->assertSame(2, count($result));
  27. $this->assertSame(['and', 'another', 'line'], $result[1]);
  28. }
  29. /**
  30. * @return void
  31. */
  32. public function testReadWithPattern() {
  33. $data = <<<TXT
  34. some random data
  35. and another line
  36. and a third
  37. TXT;
  38. $result = $this->Text->readWithPattern($data, '%s %s %s');
  39. $this->assertSame(3, count($result));
  40. $this->assertSame(['and', 'a', 'third'], $result[2]);
  41. }
  42. /**
  43. * @return void
  44. */
  45. public function testConvertToOrd() {
  46. $is = $this->Text->convertToOrd('h H');
  47. $this->assertSame($is, '0-104-32-72-0');
  48. $is = $this->Text->convertToOrd('x' . PHP_EOL . 'x' . PHP_EOL . 'x' . PHP_EOL . 'x' . PHP_EOL . 'x' . "\t" . 'x');
  49. $this->assertSame('0-120-10-120-10-120-10-120-10-120-9-120-0', $is);
  50. }
  51. /**
  52. * @return void
  53. */
  54. public function testMaxWords() {
  55. $this->assertEquals('Taylor...', Text::maxWords('Taylor Otwell', 1));
  56. $this->assertEquals('Taylor___', Text::maxWords('Taylor Otwell', 1, ['ellipsis' => '___']));
  57. $this->assertEquals('Taylor Otwell', Text::maxWords('Taylor Otwell', 3));
  58. }
  59. /**
  60. * @return void
  61. */
  62. public function testWords() {
  63. $is = $this->Text->words('Hochhaus, Unter dem Bau von ae Äußeren Einflüssen - und von Autos.', ['min_char' => 3]);
  64. $this->assertTrue(!empty($is) && is_array($is) && count($is) === 9);
  65. }
  66. }