Skip to content

Commit ad9f84e

Browse files
Copilotswissspidy
andcommitted
Add tests for None case and closure formatting, implement lazy-loaded actions field
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 6ed191c commit ad9f84e

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

features/cron-event.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ Feature: Manage WP Cron events
165165
<?php
166166
add_action( 'wp_cli_test_hook', 'wp_cli_test_function' );
167167
add_action( 'wp_cli_test_hook', array( 'MyTestClass', 'my_method' ) );
168+
add_action( 'wp_cli_test_hook_closure', function() {
169+
// Test closure
170+
} );
168171
169172
function wp_cli_test_function() {
170173
// Test function
@@ -202,3 +205,27 @@ Feature: Manage WP Cron events
202205
"""
203206
wp_cli_test_function
204207
"""
208+
209+
When I run `wp cron event schedule wp_cli_test_hook_closure now`
210+
Then STDOUT should contain:
211+
"""
212+
Success: Scheduled event with hook 'wp_cli_test_hook_closure'
213+
"""
214+
215+
When I run `wp cron event list --hook=wp_cli_test_hook_closure --fields=hook,actions --format=csv`
216+
Then STDOUT should contain:
217+
"""
218+
Closure
219+
"""
220+
221+
When I run `wp cron event schedule wp_cli_test_hook_no_actions now`
222+
Then STDOUT should contain:
223+
"""
224+
Success: Scheduled event with hook 'wp_cli_test_hook_no_actions'
225+
"""
226+
227+
When I run `wp cron event list --hook=wp_cli_test_hook_no_actions --fields=hook,actions --format=csv`
228+
Then STDOUT should contain:
229+
"""
230+
None
231+
"""

src/Cron_Event_Command.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ public function list_( $args, $assoc_args ) {
114114
$events = array();
115115
}
116116

117+
// Populate actions field only if requested
118+
if ( ! empty( $formatter->fields ) && in_array( 'actions', $formatter->fields, true ) ) {
119+
foreach ( $events as $event ) {
120+
$event->actions = self::get_hook_actions( $event->hook );
121+
}
122+
}
123+
117124
foreach ( $events as $key => $event ) {
118125
foreach ( $this->fields as $field ) {
119126
if ( ! empty( $assoc_args[ $field ] ) && $event->{$field} !== $assoc_args[ $field ] ) {

0 commit comments

Comments
 (0)