packages/requirements/tests/Unit/Console/TextTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Console;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\Attributes\Small;
10use PHPUnit\Framework\TestCase;
11use Requirements\Console\Text;
12use stdClass;
13
14#[CoversClass(Text::class)]
15#[Small]
16final class TextTest extends TestCase
17{
18 #[DataProvider('providerPlainValues')]
19 public function testPlainConvertsScalarValues(mixed $value, string $expected): void
20 {
21 self::assertSame($expected, Text::plain($value));
22 }
23
24 /**
25 * @return array<string, array{mixed, string}>
26 */
27 public static function providerPlainValues(): array
28 {
29 return [
30 'string' => ['Names shall start with a letter.', 'Names shall start with a letter.'],
31 'integer' => [42, '42'],
32 'float' => [1.5, '1.5'],
33 'empty string' => ['', ''],
34 'null' => [null, ''],
35 'boolean' => [true, ''],
36 'list' => [['text'], ''],
37 'object' => [new stdClass(), ''],
38 ];
39 }
40
41 #[DataProvider('providerPlainControlCharacters')]
42 public function testPlainRemovesControlCharacters(string $value, string $expected): void
43 {
44 self::assertSame($expected, Text::plain($value));
45 }
46
47 /**
48 * @return array<string, array{string, string}>
49 */
50 public static function providerPlainControlCharacters(): array
51 {
52 return [
53 'null byte' => ["a\x00b", 'ab'],
54 'bell' => ["a\x07b", 'ab'],
55 'backspace' => ["a\x08b", 'ab'],
56 'vertical tab' => ["a\x0bb", 'ab'],
57 'form feed' => ["a\x0cb", 'ab'],
58 'shift out' => ["a\x0eb", 'ab'],
59 'escape sequence' => ["a\x1b[31mb", 'a[31mb'],
60 'unit separator' => ["a\x1fb", 'ab'],
61 'delete' => ["a\x7fb", 'ab'],
62 'several' => ["\x01a\x02b\x03", 'ab'],
63 'tab kept' => ["a\tb", "a\tb"],
64 'line feed kept' => ["a\nb", "a\nb"],
65 'carriage return kept' => ["a\rb", "a\rb"],
66 'space kept' => ['a b', 'a b'],
67 'multibyte kept' => ['Grüße · 名前', 'Grüße · 名前'],
68 ];
69 }
70
71 #[DataProvider('providerPlainMarkup')]
72 public function testPlainEscapesConsoleMarkup(string $value, string $expected): void
73 {
74 self::assertSame($expected, Text::plain($value));
75 }
76
77 /**
78 * @return array<string, array{string, string}>
79 */
80 public static function providerPlainMarkup(): array
81 {
82 return [
83 'style tags' => ['<info>x</info>', '\\<info\\>x\\</info\\>'],
84 'error tag' => ['<error>', '\\<error\\>'],
85 'backslash' => ['a\\b', 'a\\b'],
86 ];
87 }
88}
89