-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOutputTest.php
More file actions
196 lines (145 loc) · 5.51 KB
/
Copy pathOutputTest.php
File metadata and controls
196 lines (145 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace DotTest\Maker\IO;
use Dot\Maker\ColorEnum;
use Dot\Maker\IO\Output;
use PHPUnit\Framework\TestCase;
use function fclose;
use function fopen;
use function rewind;
use function stream_get_contents;
use function stream_get_meta_data;
use const PHP_EOL;
class OutputTest extends TestCase
{
public function testWillModifyErrorStream(): void
{
$oldErrorStream = Output::getErrorStream();
$this->assertIsResource($oldErrorStream);
$oldErrorStreamMetadata = stream_get_meta_data($oldErrorStream);
$this->assertSame('STDIO', $oldErrorStreamMetadata['stream_type']);
$this->assertSame('php://stderr', $oldErrorStreamMetadata['uri']);
Output::setErrorStream(fopen('php://memory', 'w+'));
$newErrorStream = Output::getErrorStream();
$this->assertIsResource($newErrorStream);
$newErrorStreamMetadata = stream_get_meta_data($newErrorStream);
$this->assertSame('MEMORY', $newErrorStreamMetadata['stream_type']);
$this->assertSame('php://memory', $newErrorStreamMetadata['uri']);
$this->assertNotSame($oldErrorStream, $newErrorStream);
fclose($oldErrorStream);
fclose($newErrorStream);
}
public function testWillModifyOutputStream(): void
{
$oldOutputStream = Output::getOutputStream();
$this->assertIsResource($oldOutputStream);
$oldOutputStreamMetadata = stream_get_meta_data($oldOutputStream);
$this->assertSame('STDIO', $oldOutputStreamMetadata['stream_type']);
$this->assertSame('php://stdout', $oldOutputStreamMetadata['uri']);
Output::setOutputStream(fopen('php://memory', 'w+'));
$newOutputStream = Output::getOutputStream();
$this->assertIsResource($newOutputStream);
$newOutputStreamMetadata = stream_get_meta_data($newOutputStream);
$this->assertSame('MEMORY', $newOutputStreamMetadata['stream_type']);
$this->assertSame('php://memory', $newOutputStreamMetadata['uri']);
$this->assertNotSame($oldOutputStream, $newOutputStream);
fclose($oldOutputStream);
fclose($newOutputStream);
}
public function testWillOutputError(): void
{
Output::setErrorStream(fopen('php://memory', 'w+'));
$stream = Output::getErrorStream();
$this->assertIsResource($stream);
$message = 'Test message';
Output::error($message);
rewind($stream);
$this->assertSame(
ColorEnum::colorize($message, ColorEnum::ForegroundBrightRed) . PHP_EOL,
stream_get_contents($stream)
);
fclose($stream);
}
public function testWillOutputInfo(): void
{
Output::setOutputStream(fopen('php://memory', 'w+'));
$stream = Output::getOutputStream();
$this->assertIsResource($stream);
$message = 'Test message';
Output::info($message);
rewind($stream);
$this->assertSame(
ColorEnum::colorize($message, ColorEnum::ForegroundBrightBlue) . PHP_EOL,
stream_get_contents($stream)
);
fclose($stream);
}
public function testWillOutputSuccess(): void
{
Output::setOutputStream(fopen('php://memory', 'w+'));
$stream = Output::getOutputStream();
$this->assertIsResource($stream);
$message = 'Test message';
Output::success($message);
rewind($stream);
$this->assertSame(
ColorEnum::colorize($message, ColorEnum::ForegroundBrightGreen) . PHP_EOL,
stream_get_contents($stream)
);
fclose($stream);
}
public function testWillOutputWarning(): void
{
Output::setOutputStream(fopen('php://memory', 'w+'));
$stream = Output::getOutputStream();
$this->assertIsResource($stream);
$message = 'Test message';
Output::warning($message);
rewind($stream);
$this->assertSame(
ColorEnum::colorize($message, ColorEnum::ForegroundBrightYellow) . PHP_EOL,
stream_get_contents($stream)
);
fclose($stream);
}
public function testWillWrite(): void
{
Output::setOutputStream(fopen('php://memory', 'w+'));
$stream = Output::getOutputStream();
$this->assertIsResource($stream);
$message = 'Test message';
Output::write($message);
rewind($stream);
$this->assertSame($message, stream_get_contents($stream));
fclose($stream);
}
public function testWillWriteLine(): void
{
Output::setOutputStream(fopen('php://memory', 'w+'));
$stream = Output::getOutputStream();
$this->assertIsResource($stream);
$message = 'Test message';
Output::writeLine($message);
rewind($stream);
$this->assertSame($message . PHP_EOL, stream_get_contents($stream));
fclose($stream);
}
public function testWillAlwaysGetErrorStream(): void
{
Output::setErrorStream(fopen('php://memory', 'w+'));
$stream = Output::getErrorStream();
$this->assertIsResource($stream);
fclose($stream);
$stream = Output::getErrorStream();
$this->assertIsResource($stream);
}
public function testWillAlwaysGetOutputStream(): void
{
Output::setOutputStream(fopen('php://memory', 'w+'));
$stream = Output::getOutputStream();
$this->assertIsResource($stream);
fclose($stream);
$stream = Output::getOutputStream();
$this->assertIsResource($stream);
}
}