1 | <?php |
||
2 | |||
3 | namespace Test; |
||
4 | |||
5 | use PHPUnit\Framework\TestCase; |
||
6 | use Symfony\Component\Console\Input\ArgvInput; |
||
7 | use Symfony\Component\Console\Output\StreamOutput; |
||
8 | use Symfony\Component\Console\Helper\FormatterHelper; |
||
9 | use Symfony\Component\Console\Tester\TesterTrait; |
||
10 | use BlueConsole\Style; |
||
11 | |||
12 | class StyleMessageTest extends TestCase |
||
13 | { |
||
14 | use TesterTrait; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
15 | |||
16 | /** |
||
17 | * @var Style |
||
18 | */ |
||
19 | protected $style; |
||
20 | |||
21 | public function setUp(): void |
||
22 | { |
||
23 | $input = new ArgvInput; |
||
24 | $this->output = new StreamOutput(fopen('php://memory', 'w+', false)); |
||
25 | $formatter = new FormatterHelper; |
||
26 | $this->style = new Style($input, $this->output, $formatter); |
||
27 | } |
||
28 | |||
29 | public function testWarningMessage(): void |
||
30 | { |
||
31 | $this->style->warningMessage('Warning message'); |
||
32 | $this->assertEquals($this->getDisplay(), "[ WARN ] Warning message\n"); |
||
33 | } |
||
34 | |||
35 | public function testErrorMessage(): void |
||
36 | { |
||
37 | $this->style->errorMessage('Error message'); |
||
38 | $this->assertEquals($this->getDisplay(), "[ FAIL ] Error message\n"); |
||
39 | } |
||
40 | |||
41 | public function testOkMessage(): void |
||
42 | { |
||
43 | $this->style->okMessage('Ok message'); |
||
44 | $this->assertEquals($this->getDisplay(), "[ OK ] Ok message\n"); |
||
45 | } |
||
46 | |||
47 | public function testInfoMessage(): void |
||
48 | { |
||
49 | $this->style->infoMessage('Info message'); |
||
50 | $this->assertEquals($this->getDisplay(), "[ INFO ] Info message\n"); |
||
51 | } |
||
52 | |||
53 | public function testToggleTimer(): void |
||
54 | { |
||
55 | $this->style->toggleShowTimer(); |
||
56 | |||
57 | $this->assertTrue($this->style->isTimerOn()); |
||
58 | } |
||
59 | |||
60 | public function testDateFormatting(): void |
||
61 | { |
||
62 | $this->assertEquals('c', $this->style->getDateTimeFormat()); |
||
63 | $this->assertNull($this->style->setDateTimeFormat('d-m-Y')); |
||
64 | $this->assertEquals('d-m-Y', $this->style->getDateTimeFormat()); |
||
65 | |||
66 | $this->assertEquals(14, $this->style->getTimeCharLength()); |
||
67 | $this->assertInstanceOf(Style::class, $this->style->setTimeCharLength(1)); |
||
68 | $this->assertEquals(1, $this->style->getTimeCharLength()); |
||
69 | } |
||
70 | |||
71 | public function testWarningMessageWithTimer(): void |
||
72 | { |
||
73 | $this->style->toggleShowTimer(); |
||
74 | $this->style->warningMessage('Warning message'); |
||
75 | |||
76 | $this->assertRegExp($this->getMessageExpression('warning'), $this->getDisplay()); |
||
77 | } |
||
78 | |||
79 | public function testOkMessageWithTimer(): void |
||
80 | { |
||
81 | $this->style->toggleShowTimer(); |
||
82 | $this->style->okMessage('Ok message'); |
||
83 | |||
84 | $this->assertRegExp($this->getMessageExpression('ok'), $this->getDisplay()); |
||
85 | } |
||
86 | |||
87 | public function testErrorMessageWithTimer(): void |
||
88 | { |
||
89 | $this->style->toggleShowTimer(); |
||
90 | $this->style->errorMessage('Error message'); |
||
91 | |||
92 | $this->assertRegExp($this->getMessageExpression('error'), $this->getDisplay()); |
||
93 | } |
||
94 | |||
95 | public function testInfoMessageWithTimer(): void |
||
96 | { |
||
97 | $this->style->toggleShowTimer(); |
||
98 | $this->style->infoMessage('Info message'); |
||
99 | |||
100 | $this->assertRegExp($this->getMessageExpression('info'), $this->getDisplay()); |
||
101 | } |
||
102 | |||
103 | public function testWarningMessageWithDateTime(): void |
||
104 | { |
||
105 | $this->style->toggleShowTimer(); |
||
106 | $this->style->toggleTimerType(); |
||
107 | $this->style->warningMessage('Warning message'); |
||
108 | |||
109 | $this->assertRegExp($this->getMessageExpression('warning', true), $this->getDisplay()); |
||
110 | } |
||
111 | |||
112 | public function testOkMessageWithDateTime(): void |
||
113 | { |
||
114 | $this->style->toggleShowTimer(); |
||
115 | $this->style->toggleTimerType(); |
||
116 | $this->style->okMessage('Ok message'); |
||
117 | |||
118 | $this->assertRegExp($this->getMessageExpression('ok', true), $this->getDisplay()); |
||
119 | } |
||
120 | |||
121 | public function testErrorMessageWithDateTime(): void |
||
122 | { |
||
123 | $this->style->toggleShowTimer(); |
||
124 | $this->style->toggleTimerType(); |
||
125 | $this->style->errorMessage('Error message'); |
||
126 | |||
127 | $this->assertRegExp($this->getMessageExpression('error', true), $this->getDisplay()); |
||
128 | } |
||
129 | |||
130 | public function testInfoMessageWithDateTime(): void |
||
131 | { |
||
132 | $this->style->toggleShowTimer(); |
||
133 | $this->style->toggleTimerType(); |
||
134 | $this->style->infoMessage('Info message'); |
||
135 | |||
136 | $this->assertRegExp($this->getMessageExpression('info', true), $this->getDisplay()); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param string $type |
||
141 | * @param bool $isDateTime |
||
142 | * @return string |
||
143 | */ |
||
144 | protected function getMessageExpression(string $type, bool $isDateTime = false): string |
||
145 | { |
||
146 | $begin = '/\[ 000000000.00[\d]{2} \]\[ '; |
||
147 | |||
148 | if ($isDateTime) { |
||
149 | $begin = '/\[ 2[\d]{3}-[\d]{2}-[\d]{2}T[\d]{2}:[\d]{2}:[\d]{2}\+[\d]{2}:[\d]{2} \]\[ '; |
||
150 | } |
||
151 | |||
152 | $middle = ' \][ ]{5}'; |
||
153 | $end = ' message\\n/'; |
||
154 | |||
155 | switch ($type) { |
||
156 | case 'warning': |
||
157 | return $begin . 'WARN' . $middle . 'Warning' . $end; |
||
158 | case 'ok': |
||
159 | return $begin . ' OK ' . $middle . 'Ok' . $end; |
||
160 | case 'error': |
||
161 | return $begin . 'FAIL' . $middle . 'Error' . $end; |
||
162 | case 'info': |
||
163 | return $begin . 'INFO' . $middle . 'Info' . $end; |
||
164 | |||
165 | default: |
||
166 | return ''; |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 |