SupportTestingQueueFakeTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use BadMethodCallException;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Foundation\Application;
  6. use Illuminate\Support\Testing\Fakes\QueueFake;
  7. use PHPUnit\Framework\Constraint\ExceptionMessage;
  8. use PHPUnit\Framework\ExpectationFailedException;
  9. use PHPUnit\Framework\TestCase;
  10. class SupportTestingQueueFakeTest extends TestCase
  11. {
  12. /**
  13. * @var \Illuminate\Support\Testing\Fakes\QueueFake
  14. */
  15. private $fake;
  16. /**
  17. * @var \Illuminate\Tests\Support\JobStub
  18. */
  19. private $job;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. $this->fake = new QueueFake(new Application);
  24. $this->job = new JobStub;
  25. }
  26. public function testAssertPushed()
  27. {
  28. try {
  29. $this->fake->assertPushed(JobStub::class);
  30. $this->fail();
  31. } catch (ExpectationFailedException $e) {
  32. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobStub] job was not pushed.'));
  33. }
  34. $this->fake->push($this->job);
  35. $this->fake->assertPushed(JobStub::class);
  36. }
  37. public function testAssertPushedWithClosure()
  38. {
  39. $this->fake->push($this->job);
  40. $this->fake->assertPushed(function (JobStub $job) {
  41. return true;
  42. });
  43. }
  44. public function testQueueSize()
  45. {
  46. $this->assertEquals(0, $this->fake->size());
  47. $this->fake->push($this->job);
  48. $this->assertEquals(1, $this->fake->size());
  49. }
  50. public function testAssertNotPushed()
  51. {
  52. $this->fake->push($this->job);
  53. try {
  54. $this->fake->assertNotPushed(JobStub::class);
  55. $this->fail();
  56. } catch (ExpectationFailedException $e) {
  57. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\JobStub] job was pushed.'));
  58. }
  59. }
  60. public function testAssertNotPushedWithClosure()
  61. {
  62. $this->fake->assertNotPushed(JobStub::class);
  63. $this->fake->push($this->job);
  64. try {
  65. $this->fake->assertNotPushed(function (JobStub $job) {
  66. return true;
  67. });
  68. $this->fail();
  69. } catch (ExpectationFailedException $e) {
  70. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\JobStub] job was pushed.'));
  71. }
  72. }
  73. public function testAssertPushedOn()
  74. {
  75. $this->fake->push($this->job, '', 'foo');
  76. try {
  77. $this->fake->assertPushedOn('bar', JobStub::class);
  78. $this->fail();
  79. } catch (ExpectationFailedException $e) {
  80. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobStub] job was not pushed.'));
  81. }
  82. $this->fake->assertPushedOn('foo', JobStub::class);
  83. }
  84. public function testAssertPushedOnWithClosure()
  85. {
  86. $this->fake->push($this->job, '', 'foo');
  87. try {
  88. $this->fake->assertPushedOn('bar', function (JobStub $job) {
  89. return true;
  90. });
  91. $this->fail();
  92. } catch (ExpectationFailedException $e) {
  93. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobStub] job was not pushed.'));
  94. }
  95. $this->fake->assertPushedOn('foo', function (JobStub $job) {
  96. return true;
  97. });
  98. }
  99. public function testAssertPushedTimes()
  100. {
  101. $this->fake->push($this->job);
  102. $this->fake->push($this->job);
  103. try {
  104. $this->fake->assertPushed(JobStub::class, 1);
  105. $this->fail();
  106. } catch (ExpectationFailedException $e) {
  107. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobStub] job was pushed 2 times instead of 1 times.'));
  108. }
  109. $this->fake->assertPushed(JobStub::class, 2);
  110. }
  111. public function testAssertNothingPushed()
  112. {
  113. $this->fake->assertNothingPushed();
  114. $this->fake->push($this->job);
  115. try {
  116. $this->fake->assertNothingPushed();
  117. $this->fail();
  118. } catch (ExpectationFailedException $e) {
  119. $this->assertThat($e, new ExceptionMessage('Jobs were pushed unexpectedly.'));
  120. }
  121. }
  122. public function testAssertPushedUsingBulk()
  123. {
  124. $this->fake->assertNothingPushed();
  125. $queue = 'my-test-queue';
  126. $this->fake->bulk([
  127. $this->job,
  128. new JobStub,
  129. ], null, $queue);
  130. $this->fake->assertPushedOn($queue, JobStub::class);
  131. $this->fake->assertPushed(JobStub::class, 2);
  132. }
  133. public function testAssertPushedWithChainUsingClassesOrObjectsArray()
  134. {
  135. $this->fake->push(new JobWithChainStub([
  136. new JobStub,
  137. ]));
  138. $this->fake->assertPushedWithChain(JobWithChainStub::class, [
  139. JobStub::class,
  140. ]);
  141. $this->fake->assertPushedWithChain(JobWithChainStub::class, [
  142. new JobStub,
  143. ]);
  144. }
  145. public function testAssertPushedWithoutChain()
  146. {
  147. $this->fake->push(new JobWithChainStub([]));
  148. $this->fake->assertPushedWithoutChain(JobWithChainStub::class);
  149. }
  150. public function testAssertPushedWithChainSameJobDifferentChains()
  151. {
  152. $this->fake->push(new JobWithChainStub([
  153. new JobStub,
  154. ]));
  155. $this->fake->push(new JobWithChainStub([
  156. new JobStub,
  157. new JobStub,
  158. ]));
  159. $this->fake->assertPushedWithChain(JobWithChainStub::class, [
  160. JobStub::class,
  161. ]);
  162. $this->fake->assertPushedWithChain(JobWithChainStub::class, [
  163. JobStub::class,
  164. JobStub::class,
  165. ]);
  166. }
  167. public function testAssertPushedWithChainUsingCallback()
  168. {
  169. $this->fake->push(new JobWithChainAndParameterStub('first', [
  170. new JobStub,
  171. new JobStub,
  172. ]));
  173. $this->fake->push(new JobWithChainAndParameterStub('second', [
  174. new JobStub,
  175. ]));
  176. $this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [
  177. JobStub::class,
  178. ], function ($job) {
  179. return $job->parameter === 'second';
  180. });
  181. try {
  182. $this->fake->assertPushedWithChain(JobWithChainAndParameterStub::class, [
  183. JobStub::class,
  184. JobStub::class,
  185. ], function ($job) {
  186. return $job->parameter === 'second';
  187. });
  188. $this->fail();
  189. } catch (ExpectationFailedException $e) {
  190. $this->assertThat($e, new ExceptionMessage('The expected chain was not pushed'));
  191. }
  192. }
  193. public function testAssertPushedWithChainErrorHandling()
  194. {
  195. try {
  196. $this->fake->assertPushedWithChain(JobWithChainStub::class, []);
  197. $this->fail();
  198. } catch (ExpectationFailedException $e) {
  199. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\JobWithChainStub] job was not pushed'));
  200. }
  201. $this->fake->push(new JobWithChainStub([
  202. new JobStub,
  203. ]));
  204. try {
  205. $this->fake->assertPushedWithChain(JobWithChainStub::class, []);
  206. $this->fail();
  207. } catch (ExpectationFailedException $e) {
  208. $this->assertThat($e, new ExceptionMessage('The expected chain can not be empty'));
  209. }
  210. try {
  211. $this->fake->assertPushedWithChain(JobWithChainStub::class, [
  212. new JobStub,
  213. new JobStub,
  214. ]);
  215. $this->fail();
  216. } catch (ExpectationFailedException $e) {
  217. $this->assertThat($e, new ExceptionMessage('The expected chain was not pushed'));
  218. }
  219. try {
  220. $this->fake->assertPushedWithChain(JobWithChainStub::class, [
  221. JobStub::class,
  222. JobStub::class,
  223. ]);
  224. $this->fail();
  225. } catch (ExpectationFailedException $e) {
  226. $this->assertThat($e, new ExceptionMessage('The expected chain was not pushed'));
  227. }
  228. }
  229. public function testCallUndefinedMethodErrorHandling()
  230. {
  231. try {
  232. $this->fake->undefinedMethod();
  233. } catch (BadMethodCallException $e) {
  234. $this->assertThat($e, new ExceptionMessage(sprintf(
  235. 'Call to undefined method %s::%s()', get_class($this->fake), 'undefinedMethod'
  236. )));
  237. }
  238. }
  239. }
  240. class JobStub
  241. {
  242. public function handle()
  243. {
  244. //
  245. }
  246. }
  247. class JobWithChainStub
  248. {
  249. use Queueable;
  250. public function __construct($chain)
  251. {
  252. $this->chain($chain);
  253. }
  254. public function handle()
  255. {
  256. //
  257. }
  258. }
  259. class JobWithChainAndParameterStub
  260. {
  261. use Queueable;
  262. public $parameter;
  263. public function __construct($parameter, $chain)
  264. {
  265. $this->parameter = $parameter;
  266. $this->chain($chain);
  267. }
  268. public function handle()
  269. {
  270. //
  271. }
  272. }