NotifyManagerNewOrderJobTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Tests\Unit\Jobs;
  3. use App\Jobs\NotifyManagerNewOrderJob;
  4. use App\Models\Order;
  5. use App\Models\User;
  6. use App\Notifications\FireBaseNotification;
  7. use Illuminate\Support\Facades\Bus;
  8. use Illuminate\Support\Facades\Queue;
  9. use Mockery;
  10. use Tests\TestCase;
  11. class NotifyManagerNewOrderJobTest extends TestCase
  12. {
  13. protected function tearDown(): void
  14. {
  15. Mockery::close();
  16. parent::tearDown();
  17. }
  18. public function test_job_can_be_dispatched(): void
  19. {
  20. Bus::fake();
  21. $order = Mockery::mock(Order::class);
  22. NotifyManagerNewOrderJob::dispatch($order);
  23. Bus::assertDispatched(NotifyManagerNewOrderJob::class);
  24. }
  25. public function test_job_is_queued_via_queue_fake(): void
  26. {
  27. Queue::fake();
  28. $order = Mockery::mock(Order::class);
  29. NotifyManagerNewOrderJob::dispatch($order);
  30. Queue::assertPushed(NotifyManagerNewOrderJob::class);
  31. }
  32. public function test_job_not_dispatched_without_dispatch_call(): void
  33. {
  34. Bus::fake();
  35. Bus::assertNotDispatched(NotifyManagerNewOrderJob::class);
  36. }
  37. public function test_job_handle_sends_notification_when_token_exists(): void
  38. {
  39. $user = Mockery::mock(User::class)->makePartial();
  40. $user->token_fcm = 'some-fcm-token';
  41. $user->shouldReceive('notify')->once()->with(Mockery::type(FireBaseNotification::class));
  42. $orderStatus = (object)['name' => 'Новый'];
  43. $order = Mockery::mock(Order::class);
  44. // common_name вызывает $this->area->name и $this->district->shortname — мокируем getAttribute
  45. $order->shouldReceive('getAttribute')->with('common_name')->andReturn('ул. Тестовая 1');
  46. $order->shouldReceive('getAttribute')->with('user')->andReturn($user);
  47. $order->shouldReceive('getAttribute')->with('orderStatus')->andReturn($orderStatus);
  48. $job = new NotifyManagerNewOrderJob($order);
  49. $job->handle();
  50. $this->addToAssertionCount(1); // Mockery expectation: notify called once
  51. }
  52. public function test_job_handle_skips_notification_when_no_token(): void
  53. {
  54. $user = Mockery::mock(User::class)->makePartial();
  55. $user->token_fcm = null;
  56. $user->shouldNotReceive('notify');
  57. $orderStatus = (object)['name' => 'Новый'];
  58. $order = Mockery::mock(Order::class);
  59. $order->shouldReceive('getAttribute')->with('common_name')->andReturn('ул. Тестовая 1');
  60. $order->shouldReceive('getAttribute')->with('user')->andReturn($user);
  61. $order->shouldReceive('getAttribute')->with('orderStatus')->andReturn($orderStatus);
  62. $job = new NotifyManagerNewOrderJob($order);
  63. $job->handle();
  64. $this->assertTrue(true);
  65. }
  66. public function test_job_handle_skips_notification_when_no_user(): void
  67. {
  68. $orderStatus = (object)['name' => 'Новый'];
  69. $order = Mockery::mock(Order::class);
  70. $order->shouldReceive('getAttribute')->with('common_name')->andReturn('ул. Тестовая 1');
  71. $order->shouldReceive('getAttribute')->with('user')->andReturn(null);
  72. $order->shouldReceive('getAttribute')->with('orderStatus')->andReturn($orderStatus);
  73. $job = new NotifyManagerNewOrderJob($order);
  74. $job->handle();
  75. $this->assertTrue(true);
  76. }
  77. }