TextLibTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. App::uses('TextLib', 'Tools.Utility');
  3. /**
  4. */
  5. class TextLibTest extends CakeTestCase {
  6. public $TextLib;
  7. public function setUp() {
  8. parent::setUp();
  9. $this->TextLib = new TextLib();
  10. }
  11. public function testReadTab() {
  12. $data = <<<TXT
  13. some tabbed data
  14. and another line
  15. TXT;
  16. $this->TextLib = new TextLib($data);
  17. $result = $this->TextLib->readTab();
  18. $this->assertSame(2, count($result));
  19. $this->assertSame(['and', 'another', 'line'], $result[1]);
  20. }
  21. public function testReadWithPattern() {
  22. $data = <<<TXT
  23. some random data
  24. and another line
  25. and a third
  26. TXT;
  27. $this->TextLib = new TextLib($data);
  28. $result = $this->TextLib->readWithPattern("%s %s %s");
  29. $this->assertSame(3, count($result));
  30. $this->assertSame(['and', 'a', 'third'], $result[2]);
  31. }
  32. public function testConvertToOrd() {
  33. $this->TextLib = new TextLib('h H');
  34. $is = $this->TextLib->convertToOrd();
  35. //pr($is);
  36. $this->assertEquals($is, '0-104-32-72-0');
  37. $is = $this->TextLib->convertToOrd('x' . NL . 'x' . LF . 'x' . PHP_EOL . 'x' . CR . 'x' . TB . 'x');
  38. //pr($is);
  39. }
  40. public function testConvertToOrdTable() {
  41. $is = $this->TextLib->convertToOrdTable('x' . NL . 'x' . LF . 'x' . PHP_EOL . 'x' . CR . 'x' . TB . 'x');
  42. //pr($is);
  43. }
  44. public function testMaxWords() {
  45. $this->assertEquals('Taylor...', TextLib::maxWords('Taylor Otwell', 1));
  46. $this->assertEquals('Taylor___', TextLib::maxWords('Taylor Otwell', 1, ['ellipsis' => '___']));
  47. $this->assertEquals('Taylor Otwell', TextLib::maxWords('Taylor Otwell', 3));
  48. }
  49. public function testWords() {
  50. $this->TextLib = new TextLib('Hochhaus, Unter dem Bau von ae Äußeren Einflüssen - und von Autos.');
  51. $is = $this->TextLib->words(['min_char' => 3]);
  52. //pr($is);
  53. $this->assertTrue(!empty($is) && is_array($is) && count($is) === 9);
  54. }
  55. }