PendingBatchFake.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Closure;
  4. use Illuminate\Bus\PendingBatch;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Traits\ReflectsClosures;
  7. class PendingBatchFake extends PendingBatch
  8. {
  9. use ReflectsClosures;
  10. /**
  11. * The fake bus instance.
  12. *
  13. * @var \Illuminate\Support\Testing\Fakes\BusFake
  14. */
  15. protected $bus;
  16. /**
  17. * Create a new pending batch instance.
  18. *
  19. * @param \Illuminate\Support\Testing\Fakes\BusFake $bus
  20. * @param \Illuminate\Support\Collection $jobs
  21. */
  22. public function __construct(BusFake $bus, Collection $jobs)
  23. {
  24. $this->bus = $bus;
  25. $this->jobs = $jobs->filter()->values();
  26. }
  27. /**
  28. * Dispatch the batch.
  29. *
  30. * @return \Illuminate\Bus\Batch
  31. */
  32. public function dispatch()
  33. {
  34. return $this->bus->recordPendingBatch($this);
  35. }
  36. /**
  37. * Dispatch the batch after the response is sent to the browser.
  38. *
  39. * @return \Illuminate\Bus\Batch
  40. */
  41. public function dispatchAfterResponse()
  42. {
  43. return $this->bus->recordPendingBatch($this);
  44. }
  45. /**
  46. * Determine if the jobs in the batch match the given jobs.
  47. *
  48. * @param array $expectedJobs
  49. * @return bool
  50. */
  51. public function hasJobs(array $expectedJobs)
  52. {
  53. if (count($this->jobs) !== count($expectedJobs)) {
  54. return false;
  55. }
  56. foreach ($expectedJobs as $index => $expectedJob) {
  57. if ($expectedJob instanceof Closure) {
  58. $expectedType = $this->firstClosureParameterType($expectedJob);
  59. if (! $this->jobs[$index] instanceof $expectedType) {
  60. return false;
  61. }
  62. if (! $expectedJob($this->jobs[$index])) {
  63. return false;
  64. }
  65. } elseif (is_string($expectedJob)) {
  66. if ($expectedJob != get_class($this->jobs[$index])) {
  67. return false;
  68. }
  69. } elseif (serialize($expectedJob) != serialize($this->jobs[$index])) {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. }