| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support\Testing\Fakes; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use Illuminate\Bus\BatchRepository; |
| 7 |
use Illuminate\Bus\ChainedBatch; |
| 8 |
use Illuminate\Bus\PendingBatch; |
| 9 |
use Illuminate\Contracts\Bus\QueueingDispatcher; |
| 10 |
use Illuminate\Support\Arr; |
| 11 |
use Illuminate\Support\Collection; |
| 12 |
use Illuminate\Support\Str; |
| 13 |
use Illuminate\Support\Traits\ReflectsClosures; |
| 14 |
use PHPUnit\Framework\Assert as PHPUnit; |
| 15 |
use RuntimeException; |
| 16 |
|
| 17 |
class BusFake implements Fake, QueueingDispatcher |
| 18 |
{ |
| 19 |
use ReflectsClosures; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public $dispatcher; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
protected $jobsToFake = []; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
protected $jobsToDispatch = []; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
protected $batchRepository; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
protected $commands = []; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected $commandsSync = []; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
protected $commandsAfterResponse = []; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
protected $batches = []; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
protected bool $serializeAndRestore = false; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function __construct(QueueingDispatcher $dispatcher, $jobsToFake = [], ?BatchRepository $batchRepository = null) |
| 92 |
{ |
| 93 |
$this->dispatcher = $dispatcher; |
| 94 |
$this->jobsToFake = Arr::wrap($jobsToFake); |
| 95 |
$this->batchRepository = $batchRepository ?: new BatchRepositoryFake; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function except($jobsToDispatch) |
| 105 |
{ |
| 106 |
$this->jobsToDispatch = array_merge($this->jobsToDispatch, Arr::wrap($jobsToDispatch)); |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function assertDispatched($command, $callback = null) |
| 119 |
{ |
| 120 |
if ($command instanceof Closure) { |
| 121 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 122 |
} |
| 123 |
|
| 124 |
if (is_numeric($callback)) { |
| 125 |
return $this->assertDispatchedTimes($command, $callback); |
| 126 |
} |
| 127 |
|
| 128 |
PHPUnit::assertTrue( |
| 129 |
$this->dispatched($command, $callback)->count() > 0 || |
| 130 |
$this->dispatchedAfterResponse($command, $callback)->count() > 0 || |
| 131 |
$this->dispatchedSync($command, $callback)->count() > 0, |
| 132 |
"The expected [{$command}] job was not dispatched." |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public function assertDispatchedOnce($command) |
| 143 |
{ |
| 144 |
$this->assertDispatchedTimes($command, 1); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function assertDispatchedTimes($command, $times = 1) |
| 155 |
{ |
| 156 |
$callback = null; |
| 157 |
|
| 158 |
if ($command instanceof Closure) { |
| 159 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 160 |
} |
| 161 |
|
| 162 |
$count = $this->dispatched($command, $callback)->count() + |
| 163 |
$this->dispatchedAfterResponse($command, $callback)->count() + |
| 164 |
$this->dispatchedSync($command, $callback)->count(); |
| 165 |
|
| 166 |
PHPUnit::assertSame( |
| 167 |
$times, $count, |
| 168 |
sprintf( |
| 169 |
"The expected [{$command}] job was pushed {$count} %s instead of {$times} %s.", |
| 170 |
Str::plural('time', $count), |
| 171 |
Str::plural('time', $times) |
| 172 |
) |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function assertNotDispatched($command, $callback = null) |
| 184 |
{ |
| 185 |
if ($command instanceof Closure) { |
| 186 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 187 |
} |
| 188 |
|
| 189 |
PHPUnit::assertTrue( |
| 190 |
$this->dispatched($command, $callback)->count() === 0 && |
| 191 |
$this->dispatchedAfterResponse($command, $callback)->count() === 0 && |
| 192 |
$this->dispatchedSync($command, $callback)->count() === 0, |
| 193 |
"The unexpected [{$command}] job was dispatched." |
| 194 |
); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function assertNothingDispatched() |
| 203 |
{ |
| 204 |
$commandNames = implode("\n- ", array_keys($this->commands)); |
| 205 |
|
| 206 |
PHPUnit::assertEmpty($this->commands, "The following jobs were dispatched unexpectedly:\n\n- $commandNames\n"); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public function assertDispatchedSync($command, $callback = null) |
| 217 |
{ |
| 218 |
if ($command instanceof Closure) { |
| 219 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 220 |
} |
| 221 |
|
| 222 |
if (is_numeric($callback)) { |
| 223 |
return $this->assertDispatchedSyncTimes($command, $callback); |
| 224 |
} |
| 225 |
|
| 226 |
PHPUnit::assertTrue( |
| 227 |
$this->dispatchedSync($command, $callback)->count() > 0, |
| 228 |
"The expected [{$command}] job was not dispatched synchronously." |
| 229 |
); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function assertDispatchedSyncTimes($command, $times = 1) |
| 240 |
{ |
| 241 |
$callback = null; |
| 242 |
|
| 243 |
if ($command instanceof Closure) { |
| 244 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 245 |
} |
| 246 |
|
| 247 |
$count = $this->dispatchedSync($command, $callback)->count(); |
| 248 |
|
| 249 |
PHPUnit::assertSame( |
| 250 |
$times, $count, |
| 251 |
sprintf( |
| 252 |
"The expected [{$command}] job was synchronously pushed {$count} %s instead of {$times} %s.", |
| 253 |
Str::plural('time', $count), |
| 254 |
Str::plural('time', $times) |
| 255 |
) |
| 256 |
); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
public function assertNotDispatchedSync($command, $callback = null) |
| 267 |
{ |
| 268 |
if ($command instanceof Closure) { |
| 269 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 270 |
} |
| 271 |
|
| 272 |
PHPUnit::assertCount( |
| 273 |
0, $this->dispatchedSync($command, $callback), |
| 274 |
"The unexpected [{$command}] job was dispatched synchronously." |
| 275 |
); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public function assertDispatchedAfterResponse($command, $callback = null) |
| 286 |
{ |
| 287 |
if ($command instanceof Closure) { |
| 288 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 289 |
} |
| 290 |
|
| 291 |
if (is_numeric($callback)) { |
| 292 |
return $this->assertDispatchedAfterResponseTimes($command, $callback); |
| 293 |
} |
| 294 |
|
| 295 |
PHPUnit::assertTrue( |
| 296 |
$this->dispatchedAfterResponse($command, $callback)->count() > 0, |
| 297 |
"The expected [{$command}] job was not dispatched after sending the response." |
| 298 |
); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
public function assertDispatchedAfterResponseTimes($command, $times = 1) |
| 309 |
{ |
| 310 |
$callback = null; |
| 311 |
|
| 312 |
if ($command instanceof Closure) { |
| 313 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 314 |
} |
| 315 |
|
| 316 |
$count = $this->dispatchedAfterResponse($command, $callback)->count(); |
| 317 |
|
| 318 |
PHPUnit::assertSame( |
| 319 |
$times, $count, |
| 320 |
sprintf( |
| 321 |
"The expected [{$command}] job was pushed {$count} %s instead of {$times} %s.", |
| 322 |
Str::plural('time', $count), |
| 323 |
Str::plural('time', $times) |
| 324 |
) |
| 325 |
); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public function assertNotDispatchedAfterResponse($command, $callback = null) |
| 336 |
{ |
| 337 |
if ($command instanceof Closure) { |
| 338 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 339 |
} |
| 340 |
|
| 341 |
PHPUnit::assertCount( |
| 342 |
0, $this->dispatchedAfterResponse($command, $callback), |
| 343 |
"The unexpected [{$command}] job was dispatched after sending the response." |
| 344 |
); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
public function assertChained(array $expectedChain) |
| 354 |
{ |
| 355 |
$command = $expectedChain[0]; |
| 356 |
|
| 357 |
$expectedChain = array_slice($expectedChain, 1); |
| 358 |
|
| 359 |
$callback = null; |
| 360 |
|
| 361 |
if ($command instanceof Closure) { |
| 362 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 363 |
} elseif ($command instanceof ChainedBatchTruthTest) { |
| 364 |
$instance = $command; |
| 365 |
|
| 366 |
$command = ChainedBatch::class; |
| 367 |
|
| 368 |
$callback = fn ($job) => $instance($job->toPendingBatch()); |
| 369 |
} elseif (! is_string($command)) { |
| 370 |
$instance = $command; |
| 371 |
|
| 372 |
$command = get_class($instance); |
| 373 |
|
| 374 |
$callback = function ($job) use ($instance) { |
| 375 |
return serialize($this->resetChainPropertiesToDefaults($job)) === serialize($instance); |
| 376 |
}; |
| 377 |
} |
| 378 |
|
| 379 |
PHPUnit::assertTrue( |
| 380 |
$this->dispatched($command, $callback)->isNotEmpty(), |
| 381 |
"The expected [{$command}] job was not dispatched." |
| 382 |
); |
| 383 |
|
| 384 |
$this->assertDispatchedWithChainOfObjects($command, $expectedChain, $callback); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
public function assertNothingChained() |
| 393 |
{ |
| 394 |
$this->assertNothingDispatched(); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
protected function resetChainPropertiesToDefaults($job) |
| 404 |
{ |
| 405 |
return tap(clone $job, function ($job) { |
| 406 |
$job->chainConnection = null; |
| 407 |
$job->chainQueue = null; |
| 408 |
$job->chainCatchCallbacks = null; |
| 409 |
$job->chained = []; |
| 410 |
}); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
public function assertDispatchedWithoutChain($command, $callback = null) |
| 421 |
{ |
| 422 |
if ($command instanceof Closure) { |
| 423 |
[$command, $callback] = [$this->firstClosureParameterType($command), $command]; |
| 424 |
} |
| 425 |
|
| 426 |
PHPUnit::assertTrue( |
| 427 |
$this->dispatched($command, $callback)->isNotEmpty(), |
| 428 |
"The expected [{$command}] job was not dispatched." |
| 429 |
); |
| 430 |
|
| 431 |
$this->assertDispatchedWithChainOfObjects($command, [], $callback); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
protected function assertDispatchedWithChainOfObjects($command, $expectedChain, $callback) |
| 443 |
{ |
| 444 |
$chain = $expectedChain; |
| 445 |
|
| 446 |
PHPUnit::assertTrue( |
| 447 |
$this->dispatched($command, $callback)->filter(function ($job) use ($chain) { |
| 448 |
if (count($chain) !== count($job->chained)) { |
| 449 |
return false; |
| 450 |
} |
| 451 |
|
| 452 |
foreach ($job->chained as $index => $serializedChainedJob) { |
| 453 |
if ($chain[$index] instanceof ChainedBatchTruthTest) { |
| 454 |
$chainedBatch = unserialize($serializedChainedJob); |
| 455 |
|
| 456 |
if (! $chainedBatch instanceof ChainedBatch || |
| 457 |
! $chain[$index]($chainedBatch->toPendingBatch())) { |
| 458 |
return false; |
| 459 |
} |
| 460 |
} elseif ($chain[$index] instanceof Closure) { |
| 461 |
[$expectedType, $callback] = [$this->firstClosureParameterType($chain[$index]), $chain[$index]]; |
| 462 |
|
| 463 |
$chainedJob = unserialize($serializedChainedJob); |
| 464 |
|
| 465 |
if (! $chainedJob instanceof $expectedType) { |
| 466 |
throw new RuntimeException('The chained job was expected to be of type '.$expectedType.', '.$chainedJob::class.' chained.'); |
| 467 |
} |
| 468 |
|
| 469 |
if (! $callback($chainedJob)) { |
| 470 |
return false; |
| 471 |
} |
| 472 |
} elseif (is_string($chain[$index])) { |
| 473 |
if ($chain[$index] != get_class(unserialize($serializedChainedJob))) { |
| 474 |
return false; |
| 475 |
} |
| 476 |
} elseif (serialize($chain[$index]) != $serializedChainedJob) { |
| 477 |
return false; |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
return true; |
| 482 |
})->isNotEmpty(), |
| 483 |
'The expected chain was not dispatched.' |
| 484 |
); |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
public function chainedBatch(Closure $callback) |
| 494 |
{ |
| 495 |
return new ChainedBatchTruthTest($callback); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
public function assertBatched(callable|array $callback) |
| 505 |
{ |
| 506 |
$callback = is_array($callback) ? fn (PendingBatchFake $batch) => $batch->hasJobs($callback) : $callback; |
| 507 |
|
| 508 |
PHPUnit::assertTrue( |
| 509 |
$this->batched($callback)->count() > 0, |
| 510 |
'The expected batch was not dispatched.' |
| 511 |
); |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public function assertBatchCount($count) |
| 521 |
{ |
| 522 |
PHPUnit::assertCount( |
| 523 |
$count, $this->batches, |
| 524 |
); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
public function assertNothingBatched() |
| 533 |
{ |
| 534 |
$jobNames = (new Collection($this->batches)) |
| 535 |
->map(fn ($batch) => $batch->jobs->map(fn ($job) => get_class($job))) |
| 536 |
->flatten() |
| 537 |
->join("\n- "); |
| 538 |
|
| 539 |
PHPUnit::assertEmpty($this->batches, "The following batched jobs were dispatched unexpectedly:\n\n- $jobNames\n"); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
public function assertNothingPlaced() |
| 548 |
{ |
| 549 |
$this->assertNothingDispatched(); |
| 550 |
$this->assertNothingBatched(); |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
public function dispatched($command, $callback = null) |
| 561 |
{ |
| 562 |
if (! $this->hasDispatched($command)) { |
| 563 |
return new Collection; |
| 564 |
} |
| 565 |
|
| 566 |
$callback = $callback ?: fn () => true; |
| 567 |
|
| 568 |
return (new Collection($this->commands[$command]))->filter(fn ($command) => $callback($command)); |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
public function dispatchedSync(string $command, $callback = null) |
| 579 |
{ |
| 580 |
if (! $this->hasDispatchedSync($command)) { |
| 581 |
return new Collection; |
| 582 |
} |
| 583 |
|
| 584 |
$callback = $callback ?: fn () => true; |
| 585 |
|
| 586 |
return (new Collection($this->commandsSync[$command]))->filter(fn ($command) => $callback($command)); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public function dispatchedAfterResponse(string $command, $callback = null) |
| 597 |
{ |
| 598 |
if (! $this->hasDispatchedAfterResponse($command)) { |
| 599 |
return new Collection; |
| 600 |
} |
| 601 |
|
| 602 |
$callback = $callback ?: fn () => true; |
| 603 |
|
| 604 |
return (new Collection($this->commandsAfterResponse[$command]))->filter(fn ($command) => $callback($command)); |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
public function batched(callable $callback) |
| 614 |
{ |
| 615 |
if (empty($this->batches)) { |
| 616 |
return new Collection; |
| 617 |
} |
| 618 |
|
| 619 |
return (new Collection($this->batches))->filter(fn ($batch) => $callback($batch)); |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
public function hasDispatched($command) |
| 629 |
{ |
| 630 |
return isset($this->commands[$command]) && ! empty($this->commands[$command]); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
public function hasDispatchedSync($command) |
| 640 |
{ |
| 641 |
return isset($this->commandsSync[$command]) && ! empty($this->commandsSync[$command]); |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
public function hasDispatchedAfterResponse($command) |
| 651 |
{ |
| 652 |
return isset($this->commandsAfterResponse[$command]) && ! empty($this->commandsAfterResponse[$command]); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
public function dispatch($command) |
| 662 |
{ |
| 663 |
if ($this->shouldFakeJob($command)) { |
| 664 |
$this->commands[get_class($command)][] = $this->getCommandRepresentation($command); |
| 665 |
} else { |
| 666 |
return $this->dispatcher->dispatch($command); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
public function dispatchSync($command, $handler = null) |
| 680 |
{ |
| 681 |
if ($this->shouldFakeJob($command)) { |
| 682 |
$this->commandsSync[get_class($command)][] = $this->getCommandRepresentation($command); |
| 683 |
} else { |
| 684 |
return $this->dispatcher->dispatchSync($command, $handler); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
public function dispatchNow($command, $handler = null) |
| 696 |
{ |
| 697 |
if ($this->shouldFakeJob($command)) { |
| 698 |
$this->commands[get_class($command)][] = $this->getCommandRepresentation($command); |
| 699 |
} else { |
| 700 |
return $this->dispatcher->dispatchNow($command, $handler); |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
public function dispatchToQueue($command) |
| 711 |
{ |
| 712 |
if ($this->shouldFakeJob($command)) { |
| 713 |
$this->commands[get_class($command)][] = $this->getCommandRepresentation($command); |
| 714 |
} else { |
| 715 |
return $this->dispatcher->dispatchToQueue($command); |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
public function dispatchAfterResponse($command) |
| 726 |
{ |
| 727 |
if ($this->shouldFakeJob($command)) { |
| 728 |
$this->commandsAfterResponse[get_class($command)][] = $this->getCommandRepresentation($command); |
| 729 |
} else { |
| 730 |
return $this->dispatcher->dispatch($command); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
public function chain($jobs = null) |
| 741 |
{ |
| 742 |
$jobs = Collection::wrap($jobs); |
| 743 |
$jobs = ChainedBatch::prepareNestedBatches($jobs); |
| 744 |
|
| 745 |
return new PendingChainFake($this, $jobs->shift(), $jobs->toArray()); |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
public function findBatch(string $batchId) |
| 755 |
{ |
| 756 |
return $this->batchRepository->find($batchId); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
public function batch($jobs) |
| 766 |
{ |
| 767 |
return new PendingBatchFake($this, Collection::wrap($jobs)); |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
public function dispatchFakeBatch($name = '') |
| 777 |
{ |
| 778 |
return $this->batch([])->name($name)->dispatch(); |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
public function recordPendingBatch(PendingBatch $pendingBatch) |
| 788 |
{ |
| 789 |
$this->batches[] = $pendingBatch; |
| 790 |
|
| 791 |
return $this->batchRepository->store($pendingBatch); |
| 792 |
} |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
protected function shouldFakeJob($command) |
| 801 |
{ |
| 802 |
if ($this->shouldDispatchCommand($command)) { |
| 803 |
return false; |
| 804 |
} |
| 805 |
|
| 806 |
if (empty($this->jobsToFake)) { |
| 807 |
return true; |
| 808 |
} |
| 809 |
|
| 810 |
return (new Collection($this->jobsToFake)) |
| 811 |
->filter(function ($job) use ($command) { |
| 812 |
return $job instanceof Closure |
| 813 |
? $job($command) |
| 814 |
: $job === get_class($command); |
| 815 |
})->isNotEmpty(); |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
protected function shouldDispatchCommand($command) |
| 825 |
{ |
| 826 |
return (new Collection($this->jobsToDispatch)) |
| 827 |
->filter(function ($job) use ($command) { |
| 828 |
return $job instanceof Closure |
| 829 |
? $job($command) |
| 830 |
: $job === get_class($command); |
| 831 |
})->isNotEmpty(); |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
public function serializeAndRestore(bool $serializeAndRestore = true) |
| 841 |
{ |
| 842 |
$this->serializeAndRestore = $serializeAndRestore; |
| 843 |
|
| 844 |
return $this; |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
protected function serializeAndRestoreCommand($command) |
| 854 |
{ |
| 855 |
return unserialize(serialize($command)); |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
protected function getCommandRepresentation($command) |
| 865 |
{ |
| 866 |
return $this->serializeAndRestore ? $this->serializeAndRestoreCommand($command) : $command; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
public function pipeThrough(array $pipes) |
| 876 |
{ |
| 877 |
$this->dispatcher->pipeThrough($pipes); |
| 878 |
|
| 879 |
return $this; |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
public function hasCommandHandler($command) |
| 889 |
{ |
| 890 |
return $this->dispatcher->hasCommandHandler($command); |
| 891 |
} |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
public function getCommandHandler($command) |
| 900 |
{ |
| 901 |
return $this->dispatcher->getCommandHandler($command); |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
public function map(array $map) |
| 911 |
{ |
| 912 |
$this->dispatcher->map($map); |
| 913 |
|
| 914 |
return $this; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
public function dispatchedBatches() |
| 923 |
{ |
| 924 |
return $this->batches; |
| 925 |
} |
| 926 |
} |