| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace Tests\Unit\Jobs;
- use App\Jobs\NotifyManagerNewOrderJob;
- use App\Models\Order;
- use App\Models\User;
- use App\Notifications\FireBaseNotification;
- use Illuminate\Support\Facades\Bus;
- use Illuminate\Support\Facades\Queue;
- use Mockery;
- use Tests\TestCase;
- class NotifyManagerNewOrderJobTest extends TestCase
- {
- protected function tearDown(): void
- {
- Mockery::close();
- parent::tearDown();
- }
- public function test_job_can_be_dispatched(): void
- {
- Bus::fake();
- $order = Mockery::mock(Order::class);
- NotifyManagerNewOrderJob::dispatch($order);
- Bus::assertDispatched(NotifyManagerNewOrderJob::class);
- }
- public function test_job_is_queued_via_queue_fake(): void
- {
- Queue::fake();
- $order = Mockery::mock(Order::class);
- NotifyManagerNewOrderJob::dispatch($order);
- Queue::assertPushed(NotifyManagerNewOrderJob::class);
- }
- public function test_job_not_dispatched_without_dispatch_call(): void
- {
- Bus::fake();
- Bus::assertNotDispatched(NotifyManagerNewOrderJob::class);
- }
- public function test_job_handle_sends_notification_when_token_exists(): void
- {
- $user = Mockery::mock(User::class)->makePartial();
- $user->token_fcm = 'some-fcm-token';
- $user->shouldReceive('notify')->once()->with(Mockery::type(FireBaseNotification::class));
- $orderStatus = (object)['name' => 'Новый'];
- $order = Mockery::mock(Order::class);
- // common_name вызывает $this->area->name и $this->district->shortname — мокируем getAttribute
- $order->shouldReceive('getAttribute')->with('common_name')->andReturn('ул. Тестовая 1');
- $order->shouldReceive('getAttribute')->with('user')->andReturn($user);
- $order->shouldReceive('getAttribute')->with('orderStatus')->andReturn($orderStatus);
- $job = new NotifyManagerNewOrderJob($order);
- $job->handle();
- $this->addToAssertionCount(1); // Mockery expectation: notify called once
- }
- public function test_job_handle_skips_notification_when_no_token(): void
- {
- $user = Mockery::mock(User::class)->makePartial();
- $user->token_fcm = null;
- $user->shouldNotReceive('notify');
- $orderStatus = (object)['name' => 'Новый'];
- $order = Mockery::mock(Order::class);
- $order->shouldReceive('getAttribute')->with('common_name')->andReturn('ул. Тестовая 1');
- $order->shouldReceive('getAttribute')->with('user')->andReturn($user);
- $order->shouldReceive('getAttribute')->with('orderStatus')->andReturn($orderStatus);
- $job = new NotifyManagerNewOrderJob($order);
- $job->handle();
- $this->assertTrue(true);
- }
- public function test_job_handle_skips_notification_when_no_user(): void
- {
- $orderStatus = (object)['name' => 'Новый'];
- $order = Mockery::mock(Order::class);
- $order->shouldReceive('getAttribute')->with('common_name')->andReturn('ул. Тестовая 1');
- $order->shouldReceive('getAttribute')->with('user')->andReturn(null);
- $order->shouldReceive('getAttribute')->with('orderStatus')->andReturn($orderStatus);
- $job = new NotifyManagerNewOrderJob($order);
- $job->handle();
- $this->assertTrue(true);
- }
- }
|