NotifyManagerChangeStatusJobTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Tests\Unit\Jobs;
  3. use App\Jobs\NotifyManagerChangeStatusJob;
  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 NotifyManagerChangeStatusJobTest 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. NotifyManagerChangeStatusJob::dispatch($order);
  23. Bus::assertDispatched(NotifyManagerChangeStatusJob::class);
  24. }
  25. public function test_job_is_queued_via_queue_fake(): void
  26. {
  27. Queue::fake();
  28. $order = Mockery::mock(Order::class);
  29. NotifyManagerChangeStatusJob::dispatch($order);
  30. Queue::assertPushed(NotifyManagerChangeStatusJob::class);
  31. }
  32. public function test_job_not_dispatched_without_dispatch_call(): void
  33. {
  34. Bus::fake();
  35. Bus::assertNotDispatched(NotifyManagerChangeStatusJob::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. $order->shouldReceive('getAttribute')->with('common_name')->andReturn('ул. Тестовая 1');
  45. $order->shouldReceive('getAttribute')->with('user')->andReturn($user);
  46. $order->shouldReceive('getAttribute')->with('orderStatus')->andReturn($orderStatus);
  47. $job = new NotifyManagerChangeStatusJob($order);
  48. $job->handle();
  49. $this->addToAssertionCount(1); // Mockery expectation: notify called once
  50. }
  51. public function test_job_handle_skips_notification_when_no_token(): void
  52. {
  53. $user = Mockery::mock(User::class)->makePartial();
  54. $user->token_fcm = null;
  55. $user->shouldNotReceive('notify');
  56. $orderStatus = (object)['name' => 'В работе'];
  57. $order = Mockery::mock(Order::class);
  58. $order->shouldReceive('getAttribute')->with('common_name')->andReturn('ул. Тестовая 1');
  59. $order->shouldReceive('getAttribute')->with('user')->andReturn($user);
  60. $order->shouldReceive('getAttribute')->with('orderStatus')->andReturn($orderStatus);
  61. $job = new NotifyManagerChangeStatusJob($order);
  62. $job->handle();
  63. $this->assertTrue(true);
  64. }
  65. }