FormatterHelperTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Tests\Helper;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Helper\FormatterHelper;
  13. class FormatterHelperTest extends TestCase
  14. {
  15. public function testFormatSection()
  16. {
  17. $formatter = new FormatterHelper();
  18. $this->assertEquals(
  19. '<info>[cli]</info> Some text to display',
  20. $formatter->formatSection('cli', 'Some text to display'),
  21. '::formatSection() formats a message in a section'
  22. );
  23. }
  24. public function testFormatBlock()
  25. {
  26. $formatter = new FormatterHelper();
  27. $this->assertEquals(
  28. '<error> Some text to display </error>',
  29. $formatter->formatBlock('Some text to display', 'error'),
  30. '::formatBlock() formats a message in a block'
  31. );
  32. $this->assertEquals(
  33. '<error> Some text to display </error>'."\n".
  34. '<error> foo bar </error>',
  35. $formatter->formatBlock(['Some text to display', 'foo bar'], 'error'),
  36. '::formatBlock() formats a message in a block'
  37. );
  38. $this->assertEquals(
  39. '<error> </error>'."\n".
  40. '<error> Some text to display </error>'."\n".
  41. '<error> </error>',
  42. $formatter->formatBlock('Some text to display', 'error', true),
  43. '::formatBlock() formats a message in a block'
  44. );
  45. }
  46. public function testFormatBlockWithDiacriticLetters()
  47. {
  48. $formatter = new FormatterHelper();
  49. $this->assertEquals(
  50. '<error> </error>'."\n".
  51. '<error> Du texte à afficher </error>'."\n".
  52. '<error> </error>',
  53. $formatter->formatBlock('Du texte à afficher', 'error', true),
  54. '::formatBlock() formats a message in a block'
  55. );
  56. }
  57. public function testFormatBlockWithDoubleWidthDiacriticLetters()
  58. {
  59. $formatter = new FormatterHelper();
  60. $this->assertEquals(
  61. '<error> </error>'."\n".
  62. '<error> 表示するテキスト </error>'."\n".
  63. '<error> </error>',
  64. $formatter->formatBlock('表示するテキスト', 'error', true),
  65. '::formatBlock() formats a message in a block'
  66. );
  67. }
  68. public function testFormatBlockLGEscaping()
  69. {
  70. $formatter = new FormatterHelper();
  71. $this->assertEquals(
  72. '<error> </error>'."\n".
  73. '<error> \<info>some info\</info> </error>'."\n".
  74. '<error> </error>',
  75. $formatter->formatBlock('<info>some info</info>', 'error', true),
  76. '::formatBlock() escapes \'<\' chars'
  77. );
  78. }
  79. public function testTruncatingWithShorterLengthThanMessageWithSuffix()
  80. {
  81. $formatter = new FormatterHelper();
  82. $message = 'testing truncate';
  83. $this->assertSame('test...', $formatter->truncate($message, 4));
  84. $this->assertSame('testing truncat...', $formatter->truncate($message, 15));
  85. $this->assertSame('testing truncate...', $formatter->truncate($message, 16));
  86. $this->assertSame('zażółć gęślą...', $formatter->truncate('zażółć gęślą jaźń', 12));
  87. }
  88. public function testTruncatingMessageWithCustomSuffix()
  89. {
  90. $formatter = new FormatterHelper();
  91. $message = 'testing truncate';
  92. $this->assertSame('test!', $formatter->truncate($message, 4, '!'));
  93. }
  94. public function testTruncatingWithLongerLengthThanMessageWithSuffix()
  95. {
  96. $formatter = new FormatterHelper();
  97. $message = 'test';
  98. $this->assertSame($message, $formatter->truncate($message, 10));
  99. }
  100. public function testTruncatingWithNegativeLength()
  101. {
  102. $formatter = new FormatterHelper();
  103. $message = 'testing truncate';
  104. $this->assertSame('testing tru...', $formatter->truncate($message, -5));
  105. $this->assertSame('...', $formatter->truncate($message, -100));
  106. }
  107. }