forked from adhocore/php-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputHelperTest.php
More file actions
164 lines (139 loc) · 4.43 KB
/
OutputHelperTest.php
File metadata and controls
164 lines (139 loc) · 4.43 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
<?php
/*
* This file is part of the PHP-CLI package.
*
* (c) Jitendra Adhikari <jiten.adhikary@gmail.com>
* <https://github.com/adhocore>
*
* Licensed under MIT license.
*/
namespace Ahc\Cli\Test\Helper;
use Ahc\Cli\Helper\OutputHelper;
use Ahc\Cli\Input\Argument;
use Ahc\Cli\Input\Command;
use Ahc\Cli\Input\Option;
use Ahc\Cli\Output\Color;
use Ahc\Cli\Output\Writer;
use DateTime;
use PHPUnit\Framework\TestCase;
use function file;
use function implode;
use function str_replace;
use const FILE_IGNORE_NEW_LINES;
class OutputHelperTest extends TestCase
{
protected static string $ou = __DIR__ . '/output';
public function setUp(): void
{
file_put_contents(static::$ou, '', LOCK_EX);
}
public static function tearDownAfterClass(): void
{
// Make sure we clean up after ourselves:
if (file_exists(static::$ou)) {
unlink(static::$ou);
}
}
public function test_show_arguments()
{
$this->newHelper()->showArgumentsHelp([
new Argument('<path>', 'The path'),
new Argument('[config:defaultConfig]'),
], 'Arg Header', 'Arg Footer');
$this->assertSame([
'Arg Header',
'',
'Arguments:',
' <path> The path',
' [config] [default: "defaultConfig"]',
'',
'Arg Footer',
], $this->output());
}
public function test_show_options()
{
$this->newHelper()->showOptionsHelp([
new Option('-h --help', 'Show help'),
new Option('-n|--full-name <name>', 'Full name', 'John'),
], 'Opt Header', 'Opt Footer');
$this->assertSame([
'Opt Header',
'',
'Options:',
' <-n|--full-name> Full name [default: "John"]',
' [-h|--help] Show help',
'',
'Opt Footer',
], $this->output());
}
public function test_show_commands()
{
$this->newHelper()->showCommandsHelp([
new Command('rm', 'Remove file or folder'),
new Command('mkdir', 'Make a folder'),
new Command('group:rm', 'Remove file or folder'),
new Command('group:mkdir', 'Make a folder'),
], 'Cmd Header', 'Cmd Footer');
// If the default group exists, we expect visually to be rendered at the very top.
$this->assertSame([
'Cmd Header',
'',
'Commands:',
' mkdir Make a folder',
' rm Remove file or folder',
'group',
' group:mkdir Make a folder',
' group:rm Remove file or folder',
'',
'Cmd Footer',
], $this->output());
}
public function test_empty()
{
$this->newHelper()->showCommandsHelp([], 'Header');
$this->assertSame([
'Header',
'',
'Commands:',
' (n/a)',
], $this->output());
}
public function test_show_usage()
{
$argv0 = $_SERVER['argv'][0];
$_SERVER['argv'][0] = 'test';
$this->newHelper()->showUsage(implode('', [
'<bold> $0</end> <comment>-a apple</end> ## apple only<eol>',
'<bold> $0</end> <comment>-a apple -b ball</end> ## apple ball<eol>',
'loooooooooooong text ## something<eol>',
'no shell comments<eol>',
'short text ## something else<eol>',
]));
$this->assertEquals([
'',
'Usage Examples:',
' test -a apple # apple only',
' test -a apple -b ball # apple ball',
'loooooooooooong text # something',
'no shell comments',
'short text # something else',
'',
], $this->output());
$_SERVER['argv'][0] = $argv0;
}
public function test_stringify()
{
$str = $this->newHelper()->stringifyArgs([[null, 'string', 10000, 12.345, new DateTime]]);
$this->assertSame("[NULL, 'string', 10000, 12.345, DateTime]", $str);
}
public function newHelper(): OutputHelper
{
return new OutputHelper(new Writer(static::$ou, new class extends Color {
protected string $format = ':txt:';
}));
}
protected function output(): array
{
return str_replace("\033[0m", '', file(static::$ou, FILE_IGNORE_NEW_LINES));
}
}