forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.feature
More file actions
191 lines (181 loc) · 5.73 KB
/
formatter.feature
File metadata and controls
191 lines (181 loc) · 5.73 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
Feature: Format output
Scenario: Format output as YAML
Given an empty directory
And a output-yaml.php file:
"""
<?php
/**
* Output data as YAML
*
* <type>
* : Type of output.
*
* [--fields=<fields>]
* : Limit output to particular fields
*
* @when before_wp_load
*/
$output_yaml = function( $args, $assoc_args ) {
$items = array(
array(
'label' => 'Foo',
'slug' => 'foo',
),
array(
'label' => 'Bar',
'slug' => 'bar',
),
);
$format_args = array( 'format' => 'yaml' );
if ( isset( $assoc_args['fields'] ) ) {
$format_args['fields'] = explode( ',', $assoc_args['fields'] );
} else {
$format_args['fields'] = array( 'label', 'slug' );
}
$formatter = new \WP_CLI\Formatter( $format_args );
if ( 'all' === $args[0] ) {
$formatter->display_items( $items );
} else if ( 'single' === $args[0] ) {
$formatter->display_item( $items[0] );
}
};
WP_CLI::add_command( 'yaml', $output_yaml );
"""
When I run `wp --require=output-yaml.php yaml all`
Then STDOUT should be YAML containing:
"""
---
-
label: Foo
slug: foo
-
label: Bar
slug: bar
"""
When I run `wp --require=output-yaml.php yaml all --fields=label`
Then STDOUT should be YAML containing:
"""
---
-
label: Foo
-
label: Bar
"""
And STDOUT should not contain:
"""
slug: bar
"""
When I run `wp --require=output-yaml.php yaml single`
Then STDOUT should be YAML containing:
"""
---
label: Foo
slug: foo
"""
Scenario: Format data in RTL language
Given an empty directory
And a file.php file:
"""
<?php
$items = array(
array(
'id' => 1,
'language' => 'Afrikaans',
'is_rtl' => 0,
),
array(
'id' => 2,
'language' => 'العَرَبِيَّة',
'is_rtl' => 1,
),
array(
'id' => 3,
'language' => 'English',
'is_rtl' => 0,
),
);
$assoc_args = array( 'format' => 'csv' );
$formatter = new WP_CLI\Formatter( $assoc_args, array( 'id', 'language', 'is_rtl' ) );
$formatter->display_items( $items );
"""
When I run `wp eval-file file.php --skip-wordpress`
Then STDOUT should be CSV containing:
| id | language | is_rtl |
| 1 | Afrikaans | 0 |
| 2 | العَرَبِيَّة | 1 |
| 3 | English | 0 |
Scenario: Padding for pre-colorized tables
Given an empty directory
And a file.php file:
"""
<?php
use cli\Colors;
/**
* Fake command.
*
* ## OPTIONS
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* ---
*
* @when before_wp_load
*/
$fake_command = function( $args, $assoc_args ) {
Colors::enable( true );
$items = array(
array( 'package' => Colors::colorize( '%ygaa/gaa-kabes%n' ), 'version' => 'dev-master', 'result' => Colors::colorize( "%r\xf0\x9f\x9b\x87%n" ) ),
array( 'package' => Colors::colorize( '%ygaa/gaa-log%n' ), 'version' => '*', 'result' => Colors::colorize( "%g\xe2\x9c\x94%n" ) ),
array( 'package' => Colors::colorize( '%ygaa/gaa-nonsense%n' ), 'version' => 'v3.0.11', 'result' => Colors::colorize( "%r\xf0\x9f\x9b\x87%n" ) ),
array( 'package' => Colors::colorize( '%ygaa/gaa-100%%new%n' ), 'version' => 'v100%new', 'result' => Colors::colorize( "%g\xe2\x9c\x94%n" ) ),
);
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'package', 'version', 'result' ) );
$formatter->display_items( $items, array( true, false, true ) );
};
WP_CLI::add_command( 'fake', $fake_command );
"""
When I run `wp --require=file.php fake`
Then STDOUT should be a table containing rows:
| package | version | result |
| [33mgaa/gaa-kabes[0m | dev-master | [31m🛇[0m |
| [33mgaa/gaa-log[0m | * | [32m✔[0m |
| [33mgaa/gaa-nonsense[0m | v3.0.11 | [31m🛇[0m |
| [33mgaa/gaa-100%new[0m | v100%new | [32m✔[0m |
Scenario: Table rows containing linebreaks
Given an empty directory
And a file.php file:
"""
<?php
$items = array(
(object) array(
'post_id' => 1,
'meta_key' => 'foo',
'meta_value' => 'foo',
),
(object) array(
'post_id' => 1,
'meta_key' => 'fruits',
'meta_value' => "apple\nbanana\nmango",
),
(object) array(
'post_id' => 1,
'meta_key' => 'bar',
'meta_value' => 'br',
),
);
$assoc_args = array();
$formatter = new WP_CLI\Formatter( $assoc_args, array( 'post_id', 'meta_key', 'meta_value' ) );
$formatter->display_items( $items );
"""
When I run `wp eval-file file.php --skip-wordpress`
Then STDOUT should be a table containing rows:
| post_id | meta_key | meta_value |
| 1 | foo | foo |
| 1 | fruits | apple |
| | | banana |
| | | mango |
| 1 | bar | br |