QueueConnectionTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Illuminate\Tests\Integration\Queue;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Database\DatabaseTransactionsManager;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Support\Facades\Bus;
  8. use Mockery as m;
  9. use Orchestra\Testbench\TestCase;
  10. use Throwable;
  11. class QueueConnectionTest extends TestCase
  12. {
  13. protected function getEnvironmentSetUp($app)
  14. {
  15. $app['config']->set('queue.default', 'sqs');
  16. $app['config']->set('queue.connections.sqs.after_commit', true);
  17. }
  18. protected function tearDown(): void
  19. {
  20. QueueConnectionTestJob::$ran = false;
  21. m::close();
  22. }
  23. public function testJobWontGetDispatchedInsideATransaction()
  24. {
  25. $this->app->singleton('db.transactions', function () {
  26. $transactionManager = m::mock(DatabaseTransactionsManager::class);
  27. $transactionManager->shouldReceive('addCallback')->once()->andReturn(null);
  28. return $transactionManager;
  29. });
  30. Bus::dispatch(new QueueConnectionTestJob);
  31. }
  32. public function testJobWillGetDispatchedInsideATransactionWhenExplicitlyIndicated()
  33. {
  34. $this->app->singleton('db.transactions', function () {
  35. $transactionManager = m::mock(DatabaseTransactionsManager::class);
  36. $transactionManager->shouldNotReceive('addCallback')->andReturn(null);
  37. return $transactionManager;
  38. });
  39. try {
  40. Bus::dispatch((new QueueConnectionTestJob)->beforeCommit());
  41. } catch (Throwable $e) {
  42. // This job was dispatched
  43. }
  44. }
  45. public function testJobWontGetDispatchedInsideATransactionWhenExplicitlyIndicated()
  46. {
  47. $this->app['config']->set('queue.connections.sqs.after_commit', false);
  48. $this->app->singleton('db.transactions', function () {
  49. $transactionManager = m::mock(DatabaseTransactionsManager::class);
  50. $transactionManager->shouldReceive('addCallback')->once()->andReturn(null);
  51. return $transactionManager;
  52. });
  53. try {
  54. Bus::dispatch((new QueueConnectionTestJob)->afterCommit());
  55. } catch (SqsException $e) {
  56. // This job was dispatched
  57. }
  58. }
  59. }
  60. class QueueConnectionTestJob implements ShouldQueue
  61. {
  62. use Dispatchable, Queueable;
  63. public static $ran = false;
  64. public function handle()
  65. {
  66. static::$ran = true;
  67. }
  68. }