Ensure time is rounded when there's no name column present

Also drops the total footer when there's only one item displayed
This commit is contained in:
Daniel Bachhuber 2016-10-26 06:17:12 -07:00
parent f843cd2d34
commit 2b57115c55
2 changed files with 25 additions and 21 deletions

View file

@ -26,12 +26,12 @@ Feature: Profile arbitary code execution
Scenario: Profile calls to the object cache
Given a WP install
When I run `wp profile eval 'wp_cache_get( "foo" );' --fields=cache_hits,cache_misses`
When I run `wp profile eval 'wp_cache_get( "foo" );' --fields=time,cache_hits,cache_misses`
Then STDOUT should be a table containing rows:
| cache_hits | cache_misses |
| 0 | 1 |
| time | cache_hits | cache_misses |
| 0s | 0 | 1 |
When I run `wp profile eval 'wp_cache_set( "foo", "bar" ); wp_cache_get( "foo" ); wp_cache_get( "foo" );' --fields=cache_hits,cache_misses`
When I run `wp profile eval 'wp_cache_set( "foo", "bar" ); wp_cache_get( "foo" ); wp_cache_get( "foo" );' --fields=time,cache_hits,cache_misses`
Then STDOUT should be a table containing rows:
| cache_hits | cache_misses |
| 2 | 0 |
| time | cache_hits | cache_misses |
| 0s | 2 | 0 |

View file

@ -27,7 +27,9 @@ class Formatter {
$format_args['fields'] = explode( ',', $format_args['fields'] );
}
$this->total_cell_index = array_search( $fields[0], $format_args['fields'] );
if ( 'time' !== $fields[0] ) {
$this->total_cell_index = array_search( $fields[0], $format_args['fields'] );
}
$format_args['fields'] = array_map( 'trim', $format_args['fields'] );
@ -66,7 +68,7 @@ class Formatter {
$totals = array_fill( 0, count( $fields ), null );
if ( ! is_null( $this->total_cell_index ) ) {
$totals[ $this->total_cell_index ] = 'total';
$totals[ $this->total_cell_index ] = 'total (' . count( $items ) . ')';
}
$location_index = array_search( 'location', $fields );
foreach ( $items as $item ) {
@ -101,22 +103,24 @@ class Formatter {
}
$table->addRow( $values );
}
foreach( $totals as $i => $value ) {
if ( null === $value ) {
continue;
}
if ( stripos( $fields[ $i ], '_time' ) || 'time' === $fields[ $i ] ) {
$totals[ $i ] = round( $value, 4 ) . 's';
}
if ( is_array( $value ) ) {
if ( ! empty( $value ) ) {
$totals[ $i ] = round( ( array_sum( $value ) / count( $value ) ), 2 ) . '%';
} else {
$totals[ $i ] = null;
if ( count( $items ) > 1 ) {
foreach( $totals as $i => $value ) {
if ( null === $value ) {
continue;
}
if ( stripos( $fields[ $i ], '_time' ) || 'time' === $fields[ $i ] ) {
$totals[ $i ] = round( $value, 4 ) . 's';
}
if ( is_array( $value ) ) {
if ( ! empty( $value ) ) {
$totals[ $i ] = round( ( array_sum( $value ) / count( $value ) ), 2 ) . '%';
} else {
$totals[ $i ] = null;
}
}
}
$table->setFooters( $totals );
}
$table->setFooters( $totals );
foreach( $table->getDisplayLines() as $line ) {
\WP_CLI::line( $line );