FormatterHelperTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 Symfony\Component\Console\Helper\FormatterHelper;
  12. class FormatterHelperTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testFormatSection()
  15. {
  16. $formatter = new FormatterHelper();
  17. $this->assertEquals(
  18. '<info>[cli]</info> Some text to display',
  19. $formatter->formatSection('cli', 'Some text to display'),
  20. '::formatSection() formats a message in a section'
  21. );
  22. }
  23. public function testFormatBlock()
  24. {
  25. $formatter = new FormatterHelper();
  26. $this->assertEquals(
  27. '<error> Some text to display </error>',
  28. $formatter->formatBlock('Some text to display', 'error'),
  29. '::formatBlock() formats a message in a block'
  30. );
  31. $this->assertEquals(
  32. '<error> Some text to display </error>'."\n".
  33. '<error> foo bar </error>',
  34. $formatter->formatBlock(array('Some text to display', 'foo bar'), 'error'),
  35. '::formatBlock() formats a message in a block'
  36. );
  37. $this->assertEquals(
  38. '<error> </error>'."\n".
  39. '<error> Some text to display </error>'."\n".
  40. '<error> </error>',
  41. $formatter->formatBlock('Some text to display', 'error', true),
  42. '::formatBlock() formats a message in a block'
  43. );
  44. }
  45. public function testFormatBlockWithDiacriticLetters()
  46. {
  47. $formatter = new FormatterHelper();
  48. $this->assertEquals(
  49. '<error> </error>'."\n".
  50. '<error> Du texte à afficher </error>'."\n".
  51. '<error> </error>',
  52. $formatter->formatBlock('Du texte à afficher', 'error', true),
  53. '::formatBlock() formats a message in a block'
  54. );
  55. }
  56. public function testFormatBlockWithDoubleWidthDiacriticLetters()
  57. {
  58. $formatter = new FormatterHelper();
  59. $this->assertEquals(
  60. '<error> </error>'."\n".
  61. '<error> 表示するテキスト </error>'."\n".
  62. '<error> </error>',
  63. $formatter->formatBlock('表示するテキスト', 'error', true),
  64. '::formatBlock() formats a message in a block'
  65. );
  66. }
  67. public function testFormatBlockLGEscaping()
  68. {
  69. $formatter = new FormatterHelper();
  70. $this->assertEquals(
  71. '<error> </error>'."\n".
  72. '<error> \<info>some info\</info> </error>'."\n".
  73. '<error> </error>',
  74. $formatter->formatBlock('<info>some info</info>', 'error', true),
  75. '::formatBlock() escapes \'<\' chars'
  76. );
  77. }
  78. public function testTruncatingWithShorterLengthThanMessageWithSuffix()
  79. {
  80. $formatter = new FormatterHelper();
  81. $message = 'testing truncate';
  82. $this->assertSame('test...', $formatter->truncate($message, 4));
  83. $this->assertSame('testing truncat...', $formatter->truncate($message, 15));
  84. $this->assertSame('testing truncate...', $formatter->truncate($message, 16));
  85. $this->assertSame('zażółć gęślą...', $formatter->truncate('zażółć gęślą jaźń', 12));
  86. }
  87. public function testTruncatingMessageWithCustomSuffix()
  88. {
  89. $formatter = new FormatterHelper();
  90. $message = 'testing truncate';
  91. $this->assertSame('test!', $formatter->truncate($message, 4, '!'));
  92. }
  93. public function testTruncatingWithLongerLengthThanMessageWithSuffix()
  94. {
  95. $formatter = new FormatterHelper();
  96. $message = 'test';
  97. $this->assertSame($message, $formatter->truncate($message, 10));
  98. }
  99. public function testTruncatingWithNegativeLength()
  100. {
  101. $formatter = new FormatterHelper();
  102. $message = 'testing truncate';
  103. $this->assertSame('testing tru...', $formatter->truncate($message, -5));
  104. $this->assertSame('...', $formatter->truncate($message, -100));
  105. }
  106. }