BusFake.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Illuminate\Support\Testing\Fakes;
  3. use Illuminate\Contracts\Bus\Dispatcher;
  4. use PHPUnit\Framework\Assert as PHPUnit;
  5. class BusFake implements Dispatcher
  6. {
  7. /**
  8. * The commands that have been dispatched.
  9. *
  10. * @var array
  11. */
  12. protected $commands = [];
  13. /**
  14. * Assert if a job was dispatched based on a truth-test callback.
  15. *
  16. * @param string $command
  17. * @param callable|int|null $callback
  18. * @return void
  19. */
  20. public function assertDispatched($command, $callback = null)
  21. {
  22. if (is_numeric($callback)) {
  23. return $this->assertDispatchedTimes($command, $callback);
  24. }
  25. PHPUnit::assertTrue(
  26. $this->dispatched($command, $callback)->count() > 0,
  27. "The expected [{$command}] job was not dispatched."
  28. );
  29. }
  30. /**
  31. * Assert if a job was pushed a number of times.
  32. *
  33. * @param string $command
  34. * @param int $times
  35. * @return void
  36. */
  37. protected function assertDispatchedTimes($command, $times = 1)
  38. {
  39. PHPUnit::assertTrue(
  40. ($count = $this->dispatched($command)->count()) === $times,
  41. "The expected [{$command}] job was pushed {$count} times instead of {$times} times."
  42. );
  43. }
  44. /**
  45. * Determine if a job was dispatched based on a truth-test callback.
  46. *
  47. * @param string $command
  48. * @param callable|null $callback
  49. * @return void
  50. */
  51. public function assertNotDispatched($command, $callback = null)
  52. {
  53. PHPUnit::assertTrue(
  54. $this->dispatched($command, $callback)->count() === 0,
  55. "The unexpected [{$command}] job was dispatched."
  56. );
  57. }
  58. /**
  59. * Get all of the jobs matching a truth-test callback.
  60. *
  61. * @param string $command
  62. * @param callable|null $callback
  63. * @return \Illuminate\Support\Collection
  64. */
  65. public function dispatched($command, $callback = null)
  66. {
  67. if (! $this->hasDispatched($command)) {
  68. return collect();
  69. }
  70. $callback = $callback ?: function () {
  71. return true;
  72. };
  73. return collect($this->commands[$command])->filter(function ($command) use ($callback) {
  74. return $callback($command);
  75. });
  76. }
  77. /**
  78. * Determine if there are any stored commands for a given class.
  79. *
  80. * @param string $command
  81. * @return bool
  82. */
  83. public function hasDispatched($command)
  84. {
  85. return isset($this->commands[$command]) && ! empty($this->commands[$command]);
  86. }
  87. /**
  88. * Dispatch a command to its appropriate handler.
  89. *
  90. * @param mixed $command
  91. * @return mixed
  92. */
  93. public function dispatch($command)
  94. {
  95. return $this->dispatchNow($command);
  96. }
  97. /**
  98. * Dispatch a command to its appropriate handler in the current process.
  99. *
  100. * @param mixed $command
  101. * @param mixed $handler
  102. * @return mixed
  103. */
  104. public function dispatchNow($command, $handler = null)
  105. {
  106. $this->commands[get_class($command)][] = $command;
  107. }
  108. /**
  109. * Set the pipes commands should be piped through before dispatching.
  110. *
  111. * @param array $pipes
  112. * @return $this
  113. */
  114. public function pipeThrough(array $pipes)
  115. {
  116. //
  117. }
  118. /**
  119. * Determine if the given command has a handler.
  120. *
  121. * @param mixed $command
  122. * @return bool
  123. */
  124. public function hasCommandHandler($command)
  125. {
  126. return false;
  127. }
  128. /**
  129. * Retrieve the handler for a command.
  130. *
  131. * @param mixed $command
  132. * @return mixed
  133. */
  134. public function getCommandHandler($command)
  135. {
  136. return false;
  137. }
  138. /**
  139. * Map a command to a handler.
  140. *
  141. * @param array $map
  142. * @return $this
  143. */
  144. public function map(array $map)
  145. {
  146. return $this;
  147. }
  148. }