SupportTestingBusFakeTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Illuminate\Contracts\Bus\QueueingDispatcher;
  4. use Illuminate\Support\Testing\Fakes\BusFake;
  5. use Mockery as m;
  6. use PHPUnit\Framework\Constraint\ExceptionMessage;
  7. use PHPUnit\Framework\ExpectationFailedException;
  8. use PHPUnit\Framework\TestCase;
  9. class SupportTestingBusFakeTest extends TestCase
  10. {
  11. /** @var \Illuminate\Support\Testing\Fakes\BusFake */
  12. protected $fake;
  13. protected function setUp(): void
  14. {
  15. parent::setUp();
  16. $this->fake = new BusFake(m::mock(QueueingDispatcher::class));
  17. }
  18. protected function tearDown(): void
  19. {
  20. parent::tearDown();
  21. m::close();
  22. }
  23. public function testAssertDispatched()
  24. {
  25. try {
  26. $this->fake->assertDispatched(BusJobStub::class);
  27. $this->fail();
  28. } catch (ExpectationFailedException $e) {
  29. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was not dispatched.'));
  30. }
  31. $this->fake->dispatch(new BusJobStub);
  32. $this->fake->assertDispatched(BusJobStub::class);
  33. }
  34. public function testAssertDispatchedWithClosure()
  35. {
  36. $this->fake->dispatch(new BusJobStub);
  37. $this->fake->assertDispatched(function (BusJobStub $job) {
  38. return true;
  39. });
  40. }
  41. public function testAssertDispatchedAfterResponse()
  42. {
  43. try {
  44. $this->fake->assertDispatchedAfterResponse(BusJobStub::class);
  45. $this->fail();
  46. } catch (ExpectationFailedException $e) {
  47. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was not dispatched after sending the response.'));
  48. }
  49. $this->fake->dispatchAfterResponse(new BusJobStub);
  50. $this->fake->assertDispatchedAfterResponse(BusJobStub::class);
  51. }
  52. public function testAssertDispatchedAfterResponseClosure()
  53. {
  54. try {
  55. $this->fake->assertDispatchedAfterResponse(function (BusJobStub $job) {
  56. return true;
  57. });
  58. $this->fail();
  59. } catch (ExpectationFailedException $e) {
  60. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was not dispatched after sending the response.'));
  61. }
  62. }
  63. public function testAssertDispatchedSync()
  64. {
  65. try {
  66. $this->fake->assertDispatchedSync(BusJobStub::class);
  67. $this->fail();
  68. } catch (ExpectationFailedException $e) {
  69. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was not dispatched synchronously.'));
  70. }
  71. $this->fake->dispatch(new BusJobStub);
  72. try {
  73. $this->fake->assertDispatchedSync(BusJobStub::class);
  74. $this->fail();
  75. } catch (ExpectationFailedException $e) {
  76. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was not dispatched synchronously.'));
  77. }
  78. $this->fake->dispatchSync(new BusJobStub);
  79. $this->fake->assertDispatchedSync(BusJobStub::class);
  80. }
  81. public function testAssertDispatchedSyncClosure()
  82. {
  83. try {
  84. $this->fake->assertDispatchedSync(function (BusJobStub $job) {
  85. return true;
  86. });
  87. $this->fail();
  88. } catch (ExpectationFailedException $e) {
  89. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was not dispatched synchronously.'));
  90. }
  91. }
  92. public function testAssertDispatchedNow()
  93. {
  94. $this->fake->dispatchNow(new BusJobStub);
  95. $this->fake->assertDispatched(BusJobStub::class);
  96. }
  97. public function testAssertDispatchedWithCallbackInt()
  98. {
  99. $this->fake->dispatch(new BusJobStub);
  100. $this->fake->dispatchNow(new BusJobStub);
  101. try {
  102. $this->fake->assertDispatched(BusJobStub::class, 1);
  103. $this->fail();
  104. } catch (ExpectationFailedException $e) {
  105. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was pushed 2 times instead of 1 times.'));
  106. }
  107. $this->fake->assertDispatched(BusJobStub::class, 2);
  108. }
  109. public function testAssertDispatchedAfterResponseWithCallbackInt()
  110. {
  111. $this->fake->dispatchAfterResponse(new BusJobStub);
  112. $this->fake->dispatchAfterResponse(new BusJobStub);
  113. try {
  114. $this->fake->assertDispatchedAfterResponse(BusJobStub::class, 1);
  115. $this->fail();
  116. } catch (ExpectationFailedException $e) {
  117. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was pushed 2 times instead of 1 times.'));
  118. }
  119. $this->fake->assertDispatchedAfterResponse(BusJobStub::class, 2);
  120. }
  121. public function testAssertDispatchedSyncWithCallbackInt()
  122. {
  123. $this->fake->dispatchSync(new BusJobStub);
  124. $this->fake->dispatchSync(new BusJobStub);
  125. try {
  126. $this->fake->assertDispatchedSync(BusJobStub::class, 1);
  127. $this->fail();
  128. } catch (ExpectationFailedException $e) {
  129. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was synchronously pushed 2 times instead of 1 times.'));
  130. }
  131. $this->fake->assertDispatchedSync(BusJobStub::class, 2);
  132. }
  133. public function testAssertDispatchedWithCallbackFunction()
  134. {
  135. $this->fake->dispatch(new OtherBusJobStub);
  136. $this->fake->dispatchNow(new OtherBusJobStub(1));
  137. try {
  138. $this->fake->assertDispatched(OtherBusJobStub::class, function ($job) {
  139. return $job->id === 0;
  140. });
  141. $this->fail();
  142. } catch (ExpectationFailedException $e) {
  143. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\OtherBusJobStub] job was not dispatched.'));
  144. }
  145. $this->fake->assertDispatched(OtherBusJobStub::class, function ($job) {
  146. return $job->id === null;
  147. });
  148. $this->fake->assertDispatched(OtherBusJobStub::class, function ($job) {
  149. return $job->id === 1;
  150. });
  151. }
  152. public function testAssertDispatchedAfterResponseWithCallbackFunction()
  153. {
  154. $this->fake->dispatchAfterResponse(new OtherBusJobStub);
  155. $this->fake->dispatchAfterResponse(new OtherBusJobStub(1));
  156. try {
  157. $this->fake->assertDispatchedAfterResponse(OtherBusJobStub::class, function ($job) {
  158. return $job->id === 0;
  159. });
  160. $this->fail();
  161. } catch (ExpectationFailedException $e) {
  162. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\OtherBusJobStub] job was not dispatched after sending the response.'));
  163. }
  164. $this->fake->assertDispatchedAfterResponse(OtherBusJobStub::class, function ($job) {
  165. return $job->id === null;
  166. });
  167. $this->fake->assertDispatchedAfterResponse(OtherBusJobStub::class, function ($job) {
  168. return $job->id === 1;
  169. });
  170. }
  171. public function testAssertDispatchedSyncWithCallbackFunction()
  172. {
  173. $this->fake->dispatchSync(new OtherBusJobStub);
  174. $this->fake->dispatchSync(new OtherBusJobStub(1));
  175. try {
  176. $this->fake->assertDispatchedSync(OtherBusJobStub::class, function ($job) {
  177. return $job->id === 0;
  178. });
  179. $this->fail();
  180. } catch (ExpectationFailedException $e) {
  181. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\OtherBusJobStub] job was not dispatched synchronously.'));
  182. }
  183. $this->fake->assertDispatchedSync(OtherBusJobStub::class, function ($job) {
  184. return $job->id === null;
  185. });
  186. $this->fake->assertDispatchedSync(OtherBusJobStub::class, function ($job) {
  187. return $job->id === 1;
  188. });
  189. }
  190. public function testAssertDispatchedTimes()
  191. {
  192. $this->fake->dispatch(new BusJobStub);
  193. $this->fake->dispatchNow(new BusJobStub);
  194. try {
  195. $this->fake->assertDispatchedTimes(BusJobStub::class, 1);
  196. $this->fail();
  197. } catch (ExpectationFailedException $e) {
  198. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was pushed 2 times instead of 1 times.'));
  199. }
  200. $this->fake->assertDispatchedTimes(BusJobStub::class, 2);
  201. }
  202. public function testAssertDispatchedAfterResponseTimes()
  203. {
  204. $this->fake->dispatchAfterResponse(new BusJobStub);
  205. $this->fake->dispatchAfterResponse(new BusJobStub);
  206. try {
  207. $this->fake->assertDispatchedAfterResponseTimes(BusJobStub::class, 1);
  208. $this->fail();
  209. } catch (ExpectationFailedException $e) {
  210. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was pushed 2 times instead of 1 times.'));
  211. }
  212. $this->fake->assertDispatchedAfterResponseTimes(BusJobStub::class, 2);
  213. }
  214. public function testAssertDispatchedSyncTimes()
  215. {
  216. $this->fake->dispatchSync(new BusJobStub);
  217. $this->fake->dispatchSync(new BusJobStub);
  218. try {
  219. $this->fake->assertDispatchedSyncTimes(BusJobStub::class, 1);
  220. $this->fail();
  221. } catch (ExpectationFailedException $e) {
  222. $this->assertThat($e, new ExceptionMessage('The expected [Illuminate\Tests\Support\BusJobStub] job was synchronously pushed 2 times instead of 1 times.'));
  223. }
  224. $this->fake->assertDispatchedSyncTimes(BusJobStub::class, 2);
  225. }
  226. public function testAssertNotDispatched()
  227. {
  228. $this->fake->assertNotDispatched(BusJobStub::class);
  229. $this->fake->dispatch(new BusJobStub);
  230. $this->fake->dispatchNow(new BusJobStub);
  231. try {
  232. $this->fake->assertNotDispatched(BusJobStub::class);
  233. $this->fail();
  234. } catch (ExpectationFailedException $e) {
  235. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\BusJobStub] job was dispatched.'));
  236. }
  237. }
  238. public function testAssertNotDispatchedWithClosure()
  239. {
  240. $this->fake->dispatch(new BusJobStub);
  241. $this->fake->dispatchNow(new BusJobStub);
  242. try {
  243. $this->fake->assertNotDispatched(function (BusJobStub $job) {
  244. return true;
  245. });
  246. $this->fail();
  247. } catch (ExpectationFailedException $e) {
  248. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\BusJobStub] job was dispatched.'));
  249. }
  250. }
  251. public function testAssertNotDispatchedAfterResponse()
  252. {
  253. $this->fake->assertNotDispatchedAfterResponse(BusJobStub::class);
  254. $this->fake->dispatchAfterResponse(new BusJobStub);
  255. try {
  256. $this->fake->assertNotDispatchedAfterResponse(BusJobStub::class);
  257. $this->fail();
  258. } catch (ExpectationFailedException $e) {
  259. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\BusJobStub] job was dispatched after sending the response.'));
  260. }
  261. }
  262. public function testAssertNotDispatchedAfterResponseClosure()
  263. {
  264. $this->fake->dispatchAfterResponse(new BusJobStub);
  265. try {
  266. $this->fake->assertNotDispatchedAfterResponse(function (BusJobStub $job) {
  267. return true;
  268. });
  269. $this->fail();
  270. } catch (ExpectationFailedException $e) {
  271. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\BusJobStub] job was dispatched after sending the response.'));
  272. }
  273. }
  274. public function testAssertNotDispatchedSync()
  275. {
  276. $this->fake->assertNotDispatchedSync(BusJobStub::class);
  277. $this->fake->dispatchSync(new BusJobStub);
  278. try {
  279. $this->fake->assertNotDispatchedSync(BusJobStub::class);
  280. $this->fail();
  281. } catch (ExpectationFailedException $e) {
  282. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\BusJobStub] job was dispatched synchronously.'));
  283. }
  284. }
  285. public function testAssertNotDispatchedSyncClosure()
  286. {
  287. $this->fake->dispatchSync(new BusJobStub);
  288. try {
  289. $this->fake->assertNotDispatchedSync(function (BusJobStub $job) {
  290. return true;
  291. });
  292. $this->fail();
  293. } catch (ExpectationFailedException $e) {
  294. $this->assertThat($e, new ExceptionMessage('The unexpected [Illuminate\Tests\Support\BusJobStub] job was dispatched synchronously.'));
  295. }
  296. }
  297. public function testAssertNothingDispatched()
  298. {
  299. $this->fake->assertNothingDispatched();
  300. $this->fake->dispatch(new BusJobStub);
  301. try {
  302. $this->fake->assertNothingDispatched();
  303. $this->fail();
  304. } catch (ExpectationFailedException $e) {
  305. $this->assertThat($e, new ExceptionMessage('Jobs were dispatched unexpectedly.'));
  306. }
  307. }
  308. public function testAssertDispatchedWithIgnoreClass()
  309. {
  310. $dispatcher = m::mock(QueueingDispatcher::class);
  311. $job = new BusJobStub;
  312. $dispatcher->shouldReceive('dispatch')->once()->with($job);
  313. $dispatcher->shouldReceive('dispatchNow')->once()->with($job, null);
  314. $otherJob = new OtherBusJobStub;
  315. $dispatcher->shouldReceive('dispatch')->never()->with($otherJob);
  316. $dispatcher->shouldReceive('dispatchNow')->never()->with($otherJob, null);
  317. $fake = new BusFake($dispatcher, OtherBusJobStub::class);
  318. $fake->dispatch($job);
  319. $fake->dispatchNow($job);
  320. $fake->dispatch($otherJob);
  321. $fake->dispatchNow($otherJob);
  322. $fake->assertNotDispatched(BusJobStub::class);
  323. $fake->assertDispatchedTimes(OtherBusJobStub::class, 2);
  324. }
  325. public function testAssertDispatchedWithIgnoreCallback()
  326. {
  327. $dispatcher = m::mock(QueueingDispatcher::class);
  328. $job = new BusJobStub;
  329. $dispatcher->shouldReceive('dispatch')->once()->with($job);
  330. $dispatcher->shouldReceive('dispatchNow')->once()->with($job, null);
  331. $otherJob = new OtherBusJobStub;
  332. $dispatcher->shouldReceive('dispatch')->once()->with($otherJob);
  333. $dispatcher->shouldReceive('dispatchNow')->once()->with($otherJob, null);
  334. $anotherJob = new OtherBusJobStub(1);
  335. $dispatcher->shouldReceive('dispatch')->never()->with($anotherJob);
  336. $dispatcher->shouldReceive('dispatchNow')->never()->with($anotherJob, null);
  337. $fake = new BusFake($dispatcher, [
  338. function ($command) {
  339. return $command instanceof OtherBusJobStub && $command->id === 1;
  340. },
  341. ]);
  342. $fake->dispatch($job);
  343. $fake->dispatchNow($job);
  344. $fake->dispatch($otherJob);
  345. $fake->dispatchNow($otherJob);
  346. $fake->dispatch($anotherJob);
  347. $fake->dispatchNow($anotherJob);
  348. $fake->assertNotDispatched(BusJobStub::class);
  349. $fake->assertDispatchedTimes(OtherBusJobStub::class, 2);
  350. $fake->assertNotDispatched(OtherBusJobStub::class, function ($job) {
  351. return $job->id === null;
  352. });
  353. $fake->assertDispatched(OtherBusJobStub::class, function ($job) {
  354. return $job->id === 1;
  355. });
  356. }
  357. }
  358. class BusJobStub
  359. {
  360. //
  361. }
  362. class OtherBusJobStub
  363. {
  364. public $id;
  365. public function __construct($id = null)
  366. {
  367. $this->id = $id;
  368. }
  369. }