|
10 | 10 | use StellarWP\Shepherd\Tests\Tasks\Do_Action_Task; |
11 | 11 | use StellarWP\Shepherd\Tests\Tasks\Do_Prefixed_Action_Task; |
12 | 12 | use StellarWP\Shepherd\Tests\Traits\With_AS_Assertions; |
| 13 | +use Exception; |
13 | 14 |
|
14 | 15 | class Regulator_Test extends WPTestCase { |
15 | 16 | use With_Uopz; |
@@ -219,4 +220,111 @@ public function it_should_schedule_cleanup_task_when_tables_are_registered(): vo |
219 | 220 |
|
220 | 221 | $this->assertEquals( $current_count + 1, did_action( 'shepherd_' . $prefix . '_cleanup_task_scheduled' ), 'Cleanup task should not be scheduled when tables are not registered' ); |
221 | 222 | } |
| 223 | + |
| 224 | + /** |
| 225 | + * @test |
| 226 | + */ |
| 227 | + public function it_should_use_custom_dispatch_handler_when_provided_via_filter(): void { |
| 228 | + $prefix = Config::get_hook_prefix(); |
| 229 | + $regulator = Config::get_container()->get( Regulator::class ); |
| 230 | + |
| 231 | + $custom_handler_called = false; |
| 232 | + $handler_received_task = null; |
| 233 | + $handler_received_delay = null; |
| 234 | + |
| 235 | + add_filter( "shepherd_{$prefix}_dispatch_handler", function( $handler, $task, $delay ) use ( &$custom_handler_called, &$handler_received_task, &$handler_received_delay ) { |
| 236 | + return function( $task, $delay ) use ( &$custom_handler_called, &$handler_received_task, &$handler_received_delay ) { |
| 237 | + $custom_handler_called = true; |
| 238 | + $handler_received_task = $task; |
| 239 | + $handler_received_delay = $delay; |
| 240 | + }; |
| 241 | + }, 10, 3 ); |
| 242 | + |
| 243 | + $test_task = new Do_Action_Task(); |
| 244 | + $delay = 60; |
| 245 | + |
| 246 | + $last_task_id = $regulator->get_last_scheduled_task_id(); |
| 247 | + |
| 248 | + $regulator->dispatch( $test_task, $delay ); |
| 249 | + |
| 250 | + $this->assertTrue( $custom_handler_called, 'Custom dispatch handler should have been called' ); |
| 251 | + $this->assertSame( $test_task, $handler_received_task, 'Custom handler should receive the task' ); |
| 252 | + $this->assertSame( $delay, $handler_received_delay, 'Custom handler should receive the delay' ); |
| 253 | + |
| 254 | + $this->assertSame( $last_task_id, $regulator->get_last_scheduled_task_id(), 'Task should not be scheduled when custom handler is used' ); |
| 255 | + } |
| 256 | + |
| 257 | + /** |
| 258 | + * @test |
| 259 | + */ |
| 260 | + public function it_should_fire_fail_action_when_throwing_exception_in_custom_dispatch_handler(): void { |
| 261 | + $prefix = Config::get_hook_prefix(); |
| 262 | + $regulator = Config::get_container()->get( Regulator::class ); |
| 263 | + |
| 264 | + add_filter( "shepherd_{$prefix}_dispatch_handler", function( $handler, $task, $delay ) { |
| 265 | + return function( $task, $delay ) { |
| 266 | + throw new Exception( 'Custom dispatch handler failed' ); |
| 267 | + }; |
| 268 | + }, 10, 3 ); |
| 269 | + |
| 270 | + $test_task = new Do_Action_Task(); |
| 271 | + $delay = 60; |
| 272 | + |
| 273 | + $this->assertSame( 0, did_action( "shepherd_{$prefix}_task_scheduling_failed" ) ); |
| 274 | + $regulator->dispatch( $test_task, $delay ); |
| 275 | + $this->assertTrue( 0 < did_action( "shepherd_{$prefix}_task_scheduling_failed" ) ); |
| 276 | + } |
| 277 | + |
| 278 | + /** |
| 279 | + * @test |
| 280 | + */ |
| 281 | + public function it_should_do_default_when_returning_null(): void { |
| 282 | + $prefix = Config::get_hook_prefix(); |
| 283 | + $regulator = Config::get_container()->get( Regulator::class ); |
| 284 | + |
| 285 | + $called = false; |
| 286 | + |
| 287 | + // Add a custom dispatch handler via filter |
| 288 | + add_filter( "shepherd_{$prefix}_dispatch_handler", function( $handler, $task, $delay ) use ( &$called ) { |
| 289 | + return function( $task, $delay ) use ( &$called ) { |
| 290 | + $called = true; |
| 291 | + }; |
| 292 | + }, 10, 3 ); |
| 293 | + |
| 294 | + // Add a custom dispatch handler via filter |
| 295 | + add_filter( "shepherd_{$prefix}_dispatch_handler", function( $handler, $task, $delay ) { |
| 296 | + return null; |
| 297 | + }, 10, 3 ); |
| 298 | + |
| 299 | + $test_task = new Do_Action_Task(); |
| 300 | + $delay = 60; |
| 301 | + |
| 302 | + $last_task_id = $regulator->get_last_scheduled_task_id(); |
| 303 | + |
| 304 | + $regulator->dispatch( $test_task, $delay ); |
| 305 | + |
| 306 | + $this->assertFalse( $called, 'Custom dispatch handler should have been called' ); |
| 307 | + $this->assertNotSame( $last_task_id, $regulator->get_last_scheduled_task_id(), 'Task should be scheduled when custom handler is not used' ); |
| 308 | + } |
| 309 | + |
| 310 | + /** |
| 311 | + * @test |
| 312 | + */ |
| 313 | + public function it_should_do_default_when_returning_non_callable(): void { |
| 314 | + $prefix = Config::get_hook_prefix(); |
| 315 | + $regulator = Config::get_container()->get( Regulator::class ); |
| 316 | + |
| 317 | + |
| 318 | + // Add a custom dispatch handler via filter |
| 319 | + add_filter( "shepherd_{$prefix}_dispatch_handler", function( $handler, $task, $delay ) { |
| 320 | + return 'not a callable'; |
| 321 | + }, 10, 3 ); |
| 322 | + |
| 323 | + $test_task = new Do_Action_Task(); |
| 324 | + $delay = 60; |
| 325 | + |
| 326 | + $regulator->dispatch( $test_task, $delay ); |
| 327 | + |
| 328 | + $this->assertNotNull( $regulator->get_last_scheduled_task_id(), 'Task should be scheduled when custom handler is not callable' ); |
| 329 | + } |
222 | 330 | } |
0 commit comments