Skip to content

Commit d4e5dac

Browse files
authored
Merge pull request #1189 from crstauf/phpcs/ActionScheduler_wpCommentLogger_Test.php
PHPCS: `tests/phpunit/logging/ActionScheduler_wpCommentLogger_Test.php`
2 parents 7181366 + 97e045e commit d4e5dac

File tree

2 files changed

+97
-67
lines changed

2 files changed

+97
-67
lines changed

phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
</rule>
5050

5151
<rule ref="PEAR.NamingConventions.ValidClassName">
52+
<exclude-pattern>tests/phpunit/logging/ActionScheduler_wpCommentLogger_Test.php</exclude-pattern>
5253
<exclude-pattern>classes/data-stores/ActionScheduler_wpPostStore_PostStatusRegistrar.php</exclude-pattern>
5354
<exclude-pattern>classes/data-stores/ActionScheduler_wpPostStore_TaxonomyRegistrar.php</exclude-pattern>
5455
<exclude-pattern>tests/phpunit/jobstore/ActionScheduler_wpPostStore_Test.php</exclude-pattern>

tests/phpunit/logging/ActionScheduler_wpCommentLogger_Test.php

Lines changed: 96 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* @package test_cases\logging
66
*/
77
class ActionScheduler_wpCommentLogger_Test extends ActionScheduler_UnitTestCase {
8+
9+
/** @var bool */
810
private $use_comment_logger;
911

1012
public function test_default_logger() {
@@ -19,10 +21,10 @@ public function test_default_logger() {
1921

2022
public function test_add_log_entry() {
2123
$action_id = as_schedule_single_action( time(), 'a hook' );
22-
$logger = ActionScheduler::logger();
23-
$message = 'Logging that something happened';
24-
$log_id = $logger->log( $action_id, $message );
25-
$entry = $logger->get_entry( $log_id );
24+
$logger = ActionScheduler::logger();
25+
$message = 'Logging that something happened';
26+
$log_id = $logger->log( $action_id, $message );
27+
$entry = $logger->get_entry( $log_id );
2628

2729
$this->assertEquals( $action_id, $entry->get_action_id() );
2830
$this->assertEquals( $message, $entry->get_message() );
@@ -39,109 +41,132 @@ public function test_add_log_datetime() {
3941
$this->assertEquals( $action_id, $entry->get_action_id() );
4042
$this->assertEquals( $message, $entry->get_message() );
4143

42-
$date = new ActionScheduler_DateTime( 'now', new DateTimeZone( 'UTC' ) );
43-
$log_id = $logger->log( $action_id, $message, $date );
44-
$entry = $logger->get_entry( $log_id );
44+
$date = new ActionScheduler_DateTime( 'now', new DateTimeZone( 'UTC' ) );
45+
$log_id = $logger->log( $action_id, $message, $date );
46+
$entry = $logger->get_entry( $log_id );
4547

4648
$this->assertEquals( $action_id, $entry->get_action_id() );
4749
$this->assertEquals( $message, $entry->get_message() );
4850
}
4951

5052
public function test_erroneous_entry_id() {
51-
$comment = wp_insert_comment(array(
52-
'comment_post_ID' => 1,
53-
'comment_author' => 'test',
54-
'comment_content' => 'this is not a log entry',
55-
));
53+
$comment = wp_insert_comment(
54+
array(
55+
'comment_post_ID' => 1,
56+
'comment_author' => 'test',
57+
'comment_content' => 'this is not a log entry',
58+
)
59+
);
60+
5661
$logger = ActionScheduler::logger();
57-
$entry = $logger->get_entry( $comment );
62+
$entry = $logger->get_entry( $comment );
63+
5864
$this->assertEquals( '', $entry->get_action_id() );
5965
$this->assertEquals( '', $entry->get_message() );
6066
}
6167

6268
public function test_storage_comments() {
6369
$action_id = as_schedule_single_action( time(), 'a hook' );
64-
$logger = ActionScheduler::logger();
65-
$logs = $logger->get_logs( $action_id );
66-
$expected = new ActionScheduler_LogEntry( $action_id, 'action created' );
67-
$this->assertTrue( in_array( $this->log_entry_to_array( $expected ) , $this->log_entry_to_array( $logs ) ) );
70+
$logger = ActionScheduler::logger();
71+
$logs = $logger->get_logs( $action_id );
72+
$expected = new ActionScheduler_LogEntry( $action_id, 'action created' );
73+
74+
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
75+
$this->assertTrue( in_array( $this->log_entry_to_array( $expected ), $this->log_entry_to_array( $logs ) ) );
6876
}
6977

7078
protected function log_entry_to_array( $logs ) {
7179
if ( $logs instanceof ActionScheduler_LogEntry ) {
72-
return array( 'action_id' => $logs->get_action_id(), 'message' => $logs->get_message() );
80+
return array(
81+
'action_id' => $logs->get_action_id(),
82+
'message' => $logs->get_message(),
83+
);
7384
}
7485

75-
foreach ( $logs as $id => $log) {
76-
$logs[ $id ] = array( 'action_id' => $log->get_action_id(), 'message' => $log->get_message() );
86+
foreach ( $logs as $id => $log ) {
87+
$logs[ $id ] = array(
88+
'action_id' => $log->get_action_id(),
89+
'message' => $log->get_message(),
90+
);
7791
}
7892

7993
return $logs;
8094
}
8195

8296
public function test_execution_comments() {
8397
$action_id = as_schedule_single_action( time(), ActionScheduler_Callbacks::HOOK_WITH_CALLBACK );
84-
$logger = ActionScheduler::logger();
85-
$started = new ActionScheduler_LogEntry( $action_id, 'action started via Unit Tests' );
86-
$finished = new ActionScheduler_LogEntry( $action_id, 'action complete via Unit Tests' );
98+
$logger = ActionScheduler::logger();
99+
$started = new ActionScheduler_LogEntry( $action_id, 'action started via Unit Tests' );
100+
$finished = new ActionScheduler_LogEntry( $action_id, 'action complete via Unit Tests' );
87101

88102
$runner = ActionScheduler_Mocker::get_queue_runner();
89103
$runner->run( 'Unit Tests' );
90104

91105
$logs = $logger->get_logs( $action_id );
106+
107+
// phpcs:disable WordPress.PHP.StrictInArray.MissingTrueStrict
92108
$this->assertTrue( in_array( $this->log_entry_to_array( $started ), $this->log_entry_to_array( $logs ) ) );
93109
$this->assertTrue( in_array( $this->log_entry_to_array( $finished ), $this->log_entry_to_array( $logs ) ) );
110+
// phpcs:enable
94111
}
95112

96113
public function test_failed_execution_comments() {
97-
$hook = md5(rand());
98-
add_action( $hook, array( $this, '_a_hook_callback_that_throws_an_exception' ) );
114+
$hook = md5( wp_rand() );
115+
add_action( $hook, array( $this, 'a_hook_callback_that_throws_an_exception' ) );
116+
99117
$action_id = as_schedule_single_action( time(), $hook );
100-
$logger = ActionScheduler::logger();
101-
$started = new ActionScheduler_LogEntry( $action_id, 'action started via Unit Tests' );
102-
$finished = new ActionScheduler_LogEntry( $action_id, 'action complete via Unit Tests' );
103-
$failed = new ActionScheduler_LogEntry( $action_id, 'action failed via Unit Tests: Execution failed' );
118+
$logger = ActionScheduler::logger();
119+
$started = new ActionScheduler_LogEntry( $action_id, 'action started via Unit Tests' );
120+
$finished = new ActionScheduler_LogEntry( $action_id, 'action complete via Unit Tests' );
121+
$failed = new ActionScheduler_LogEntry( $action_id, 'action failed via Unit Tests: Execution failed' );
104122

105123
$runner = ActionScheduler_Mocker::get_queue_runner();
106124
$runner->run( 'Unit Tests' );
107125

108126
$logs = $logger->get_logs( $action_id );
127+
128+
// phpcs:disable WordPress.PHP.StrictInArray.MissingTrueStrict
109129
$this->assertTrue( in_array( $this->log_entry_to_array( $started ), $this->log_entry_to_array( $logs ) ) );
110130
$this->assertFalse( in_array( $this->log_entry_to_array( $finished ), $this->log_entry_to_array( $logs ) ) );
111131
$this->assertTrue( in_array( $this->log_entry_to_array( $failed ), $this->log_entry_to_array( $logs ) ) );
132+
// phpcs:enable
112133
}
113134

114135
public function test_failed_schedule_next_instance_comments() {
115-
$action_id = rand();
136+
$action_id = wp_rand();
116137
$logger = ActionScheduler::logger();
117138
$log_entry = new ActionScheduler_LogEntry( $action_id, 'There was a failure scheduling the next instance of this action: Execution failed' );
118139

119140
try {
120-
$this->_a_hook_callback_that_throws_an_exception();
141+
$this->a_hook_callback_that_throws_an_exception();
121142
} catch ( Exception $e ) {
122143
do_action( 'action_scheduler_failed_to_schedule_next_instance', $action_id, $e, new ActionScheduler_Action( ActionScheduler_Callbacks::HOOK_WITH_CALLBACK ) );
123144
}
124145

125146
$logs = $logger->get_logs( $action_id );
147+
148+
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
126149
$this->assertTrue( in_array( $this->log_entry_to_array( $log_entry ), $this->log_entry_to_array( $logs ) ) );
127150
}
128151

129152
public function test_fatal_error_comments() {
130-
$hook = md5(rand());
153+
$hook = md5( wp_rand() );
131154
$action_id = as_schedule_single_action( time(), $hook );
132-
$logger = ActionScheduler::logger();
133-
do_action( 'action_scheduler_unexpected_shutdown', $action_id, array(
134-
'type' => E_ERROR,
155+
$logger = ActionScheduler::logger();
156+
$args = array(
157+
'type' => E_ERROR,
135158
'message' => 'Test error',
136-
'file' => __FILE__,
137-
'line' => __LINE__,
138-
));
159+
'file' => __FILE__,
160+
'line' => __LINE__,
161+
);
139162

140-
$logs = $logger->get_logs( $action_id );
141-
$found_log = FALSE;
163+
do_action( 'action_scheduler_unexpected_shutdown', $action_id, $args );
164+
165+
$logs = $logger->get_logs( $action_id );
166+
$found_log = false;
142167
foreach ( $logs as $l ) {
143168
if ( strpos( $l->get_message(), 'unexpected shutdown' ) === 0 ) {
144-
$found_log = TRUE;
169+
$found_log = true;
145170
}
146171
}
147172
$this->assertTrue( $found_log, 'Unexpected shutdown log not found' );
@@ -150,14 +175,17 @@ public function test_fatal_error_comments() {
150175
public function test_canceled_action_comments() {
151176
$action_id = as_schedule_single_action( time(), 'a hook' );
152177
as_unschedule_action( 'a hook' );
153-
$logger = ActionScheduler::logger();
154-
$logs = $logger->get_logs( $action_id );
178+
179+
$logger = ActionScheduler::logger();
180+
$logs = $logger->get_logs( $action_id );
155181
$expected = new ActionScheduler_LogEntry( $action_id, 'action canceled' );
182+
183+
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
156184
$this->assertTrue( in_array( $this->log_entry_to_array( $expected ), $this->log_entry_to_array( $logs ) ) );
157185
}
158186

159-
public function _a_hook_callback_that_throws_an_exception() {
160-
throw new RuntimeException('Execution failed');
187+
public function a_hook_callback_that_throws_an_exception() {
188+
throw new RuntimeException( 'Execution failed' );
161189
}
162190

163191
public function test_filtering_of_get_comments() {
@@ -166,43 +194,44 @@ public function test_filtering_of_get_comments() {
166194
return;
167195
}
168196

169-
$post_id = $this->factory->post->create_object(array(
170-
'post_title' => __FUNCTION__,
171-
));
172-
$comment_id = $this->factory->comment->create_object(array(
173-
'comment_post_ID' => $post_id,
174-
'comment_author' => __CLASS__,
175-
'comment_content' => __FUNCTION__,
176-
));
197+
$post_id = $this->factory->post->create_object( array( 'post_title' => __FUNCTION__ ) );
198+
$comment_id = $this->factory->comment->create_object(
199+
array(
200+
'comment_post_ID' => $post_id,
201+
'comment_author' => __CLASS__,
202+
'comment_content' => __FUNCTION__,
203+
)
204+
);
177205

178-
// Verify that we're getting the expected comment before we add logging comments
206+
// Verify that we're getting the expected comment before we add logging comments.
179207
$comments = get_comments();
180208
$this->assertCount( 1, $comments );
181209
$this->assertEquals( $comment_id, $comments[0]->comment_ID );
182210

183-
184211
$action_id = as_schedule_single_action( time(), 'a hook' );
185-
$logger = ActionScheduler::logger();
186-
$message = 'Logging that something happened';
187-
$log_id = $logger->log( $action_id, $message );
188-
212+
$logger = ActionScheduler::logger();
213+
$message = 'Logging that something happened';
214+
$log_id = $logger->log( $action_id, $message );
189215

190-
// Verify that logging comments are excluded from general comment queries
216+
// Verify that logging comments are excluded from general comment queries.
191217
$comments = get_comments();
192218
$this->assertCount( 1, $comments );
193219
$this->assertEquals( $comment_id, $comments[0]->comment_ID );
194220

195-
// Verify that logging comments are returned when asking for them specifically
196-
$comments = get_comments(array(
197-
'type' => ActionScheduler_wpCommentLogger::TYPE,
198-
));
199-
// Expecting two: one when the action is created, another when we added our custom log
221+
// Verify that logging comments are returned when asking for them specifically.
222+
$comments = get_comments(
223+
array(
224+
'type' => ActionScheduler_wpCommentLogger::TYPE,
225+
)
226+
);
227+
228+
// Expecting two: one when the action is created, another when we added our custom log.
200229
$this->assertCount( 2, $comments );
201-
$this->assertContains( $log_id, wp_list_pluck($comments, 'comment_ID'));
230+
$this->assertContains( $log_id, wp_list_pluck( $comments, 'comment_ID' ) );
202231
}
203232

204233
private function using_comment_logger() {
205-
if ( null === $this->use_comment_logger ) {
234+
if ( is_null( $this->use_comment_logger ) ) {
206235
$this->use_comment_logger = ! ActionScheduler_DataController::dependencies_met();
207236
}
208237

0 commit comments

Comments
 (0)