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