Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 7 | class ActionScheduler_wpCommentLogger_Test extends ActionScheduler_UnitTestCase { |
||
| 8 | public function test_default_logger() { |
||
| 9 | $logger = ActionScheduler::logger(); |
||
| 10 | $this->assertInstanceOf( 'ActionScheduler_Logger', $logger ); |
||
| 11 | $this->assertInstanceOf( 'ActionScheduler_wpCommentLogger', $logger ); |
||
| 12 | } |
||
| 13 | |||
| 14 | public function test_add_log_entry() { |
||
| 15 | $action_id = as_schedule_single_action( time(), 'a hook' ); |
||
| 16 | $logger = ActionScheduler::logger(); |
||
| 17 | $message = 'Logging that something happened'; |
||
| 18 | $log_id = $logger->log( $action_id, $message ); |
||
| 19 | $entry = $logger->get_entry( $log_id ); |
||
| 20 | |||
| 21 | $this->assertEquals( $action_id, $entry->get_action_id() ); |
||
| 22 | $this->assertEquals( $message, $entry->get_message() ); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function test_add_log_datetime() { |
||
| 26 | $action_id = as_schedule_single_action( time(), 'a hook' ); |
||
| 27 | $logger = ActionScheduler::logger(); |
||
| 28 | $message = 'Logging that something happened'; |
||
| 29 | $date = new DateTime( 'now', new DateTimeZone( 'UTC' ) ); |
||
| 30 | $log_id = $logger->log( $action_id, $message, $date ); |
||
| 31 | $entry = $logger->get_entry( $log_id ); |
||
| 32 | |||
| 33 | $this->assertEquals( $action_id, $entry->get_action_id() ); |
||
| 34 | $this->assertEquals( $message, $entry->get_message() ); |
||
| 35 | |||
| 36 | $date = new ActionScheduler_DateTime( 'now', new DateTimeZone( 'UTC' ) ); |
||
| 37 | $log_id = $logger->log( $action_id, $message, $date ); |
||
| 38 | $entry = $logger->get_entry( $log_id ); |
||
| 39 | |||
| 40 | $this->assertEquals( $action_id, $entry->get_action_id() ); |
||
| 41 | $this->assertEquals( $message, $entry->get_message() ); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function test_null_log_entry() { |
||
| 45 | $logger = ActionScheduler::logger(); |
||
| 46 | $entry = $logger->get_entry( 1 ); |
||
| 47 | $this->assertEquals( '', $entry->get_action_id() ); |
||
| 48 | $this->assertEquals( '', $entry->get_message() ); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function test_erroneous_entry_id() { |
||
| 52 | $comment = wp_insert_comment(array( |
||
| 53 | 'comment_post_ID' => 1, |
||
| 54 | 'comment_author' => 'test', |
||
| 55 | 'comment_content' => 'this is not a log entry', |
||
| 56 | )); |
||
| 57 | $logger = ActionScheduler::logger(); |
||
| 58 | $entry = $logger->get_entry( $comment ); |
||
| 59 | $this->assertEquals( '', $entry->get_action_id() ); |
||
| 60 | $this->assertEquals( '', $entry->get_message() ); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function test_storage_comments() { |
||
| 64 | $action_id = as_schedule_single_action( time(), 'a hook' ); |
||
| 65 | $logger = ActionScheduler::logger(); |
||
| 66 | $logs = $logger->get_logs( $action_id ); |
||
| 67 | $expected = new ActionScheduler_LogEntry( $action_id, 'action created' ); |
||
| 68 | $this->assertTrue( in_array( $this->log_entry_to_array( $expected ) , $this->log_entry_to_array( $logs ) ) ); |
||
| 69 | } |
||
| 70 | |||
| 71 | protected function log_entry_to_array( $logs ) { |
||
| 72 | if ( $logs instanceof ActionScheduler_LogEntry ) { |
||
| 73 | return array( 'action_id' => $logs->get_action_id(), 'message' => $logs->get_message() ); |
||
| 74 | } |
||
| 75 | |||
| 76 | foreach ( $logs as $id => $log) { |
||
| 77 | $logs[ $id ] = array( 'action_id' => $log->get_action_id(), 'message' => $log->get_message() ); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $logs; |
||
| 81 | } |
||
| 82 | |||
| 83 | public function test_execution_comments() { |
||
| 84 | $action_id = as_schedule_single_action( time(), 'a hook' ); |
||
| 85 | $logger = ActionScheduler::logger(); |
||
| 86 | $started = new ActionScheduler_LogEntry( $action_id, 'action started' ); |
||
| 87 | $finished = new ActionScheduler_LogEntry( $action_id, 'action complete' ); |
||
| 88 | |||
| 89 | $runner = new ActionScheduler_QueueRunner(); |
||
| 90 | $runner->run(); |
||
| 91 | |||
| 92 | $logs = $logger->get_logs( $action_id ); |
||
| 93 | $this->assertTrue( in_array( $this->log_entry_to_array( $started ), $this->log_entry_to_array( $logs ) ) ); |
||
| 94 | $this->assertTrue( in_array( $this->log_entry_to_array( $finished ), $this->log_entry_to_array( $logs ) ) ); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function test_failed_execution_comments() { |
||
| 98 | $hook = md5(rand()); |
||
| 99 | add_action( $hook, array( $this, '_a_hook_callback_that_throws_an_exception' ) ); |
||
| 100 | $action_id = as_schedule_single_action( time(), $hook ); |
||
| 101 | $logger = ActionScheduler::logger(); |
||
| 102 | $started = new ActionScheduler_LogEntry( $action_id, 'action started' ); |
||
| 103 | $finished = new ActionScheduler_LogEntry( $action_id, 'action complete' ); |
||
| 104 | $failed = new ActionScheduler_LogEntry( $action_id, 'action failed: Execution failed' ); |
||
| 105 | |||
| 106 | $runner = new ActionScheduler_QueueRunner(); |
||
| 107 | $runner->run(); |
||
| 108 | |||
| 109 | $logs = $logger->get_logs( $action_id ); |
||
| 110 | $this->assertTrue( in_array( $this->log_entry_to_array( $started ), $this->log_entry_to_array( $logs ) ) ); |
||
| 111 | $this->assertFalse( in_array( $this->log_entry_to_array( $finished ), $this->log_entry_to_array( $logs ) ) ); |
||
| 112 | $this->assertTrue( in_array( $this->log_entry_to_array( $failed ), $this->log_entry_to_array( $logs ) ) ); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function test_fatal_error_comments() { |
||
| 116 | $hook = md5(rand()); |
||
| 117 | $action_id = as_schedule_single_action( time(), $hook ); |
||
| 118 | $logger = ActionScheduler::logger(); |
||
| 119 | do_action( 'action_scheduler_unexpected_shutdown', $action_id, array( |
||
| 120 | 'type' => E_ERROR, |
||
| 121 | 'message' => 'Test error', |
||
| 122 | 'file' => __FILE__, |
||
| 123 | 'line' => __LINE__, |
||
| 124 | )); |
||
| 125 | |||
| 126 | $logs = $logger->get_logs( $action_id ); |
||
| 127 | $found_log = FALSE; |
||
| 128 | foreach ( $logs as $l ) { |
||
| 129 | if ( strpos( $l->get_message(), 'unexpected shutdown' ) === 0 ) { |
||
| 130 | $found_log = TRUE; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | $this->assertTrue( $found_log, 'Unexpected shutdown log not found' ); |
||
| 134 | } |
||
| 135 | |||
| 136 | public function test_canceled_action_comments() { |
||
| 137 | $action_id = as_schedule_single_action( time(), 'a hook' ); |
||
| 138 | as_unschedule_action( 'a hook' ); |
||
| 139 | $logger = ActionScheduler::logger(); |
||
| 140 | $logs = $logger->get_logs( $action_id ); |
||
| 141 | $expected = new ActionScheduler_LogEntry( $action_id, 'action canceled' ); |
||
| 142 | $this->assertTrue( in_array( $this->log_entry_to_array( $expected ), $this->log_entry_to_array( $logs ) ) ); |
||
| 143 | } |
||
| 144 | |||
| 145 | public function _a_hook_callback_that_throws_an_exception() { |
||
| 146 | throw new RuntimeException('Execution failed'); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function test_filtering_of_get_comments() { |
||
| 150 | $post_id = $this->factory->post->create_object(array( |
||
| 151 | 'post_title' => __FUNCTION__, |
||
| 152 | )); |
||
| 153 | $comment_id = $this->factory->comment->create_object(array( |
||
| 154 | 'comment_post_ID' => $post_id, |
||
| 155 | 'comment_author' => __CLASS__, |
||
| 156 | 'comment_content' => __FUNCTION__, |
||
| 157 | )); |
||
| 158 | |||
| 159 | // Verify that we're getting the expected comment before we add logging comments |
||
| 160 | $comments = get_comments(); |
||
| 161 | $this->assertCount( 1, $comments ); |
||
| 162 | $this->assertEquals( $comment_id, $comments[0]->comment_ID ); |
||
| 163 | |||
| 164 | |||
| 165 | $action_id = as_schedule_single_action( time(), 'a hook' ); |
||
| 166 | $logger = ActionScheduler::logger(); |
||
| 167 | $message = 'Logging that something happened'; |
||
| 168 | $log_id = $logger->log( $action_id, $message ); |
||
| 169 | |||
| 170 | |||
| 171 | // Verify that logging comments are excluded from general comment queries |
||
| 172 | $comments = get_comments(); |
||
| 173 | $this->assertCount( 1, $comments ); |
||
| 174 | $this->assertEquals( $comment_id, $comments[0]->comment_ID ); |
||
| 175 | |||
| 176 | // Verify that logging comments are returned when asking for them specifically |
||
| 177 | $comments = get_comments(array( |
||
| 178 | 'type' => ActionScheduler_wpCommentLogger::TYPE, |
||
| 179 | )); |
||
| 180 | // Expecting two: one when the action is created, another when we added our custom log |
||
| 181 | $this->assertCount( 2, $comments ); |
||
| 182 | $this->assertContains( $log_id, wp_list_pluck($comments, 'comment_ID')); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 |