|
| 1 | +<?php |
| 2 | +declare( strict_types=1 ); |
| 3 | + |
| 4 | +namespace Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Jobs; |
| 5 | + |
| 6 | +use Automattic\WooCommerce\GoogleListingsAndAds\ActionScheduler\ActionScheduler; |
| 7 | +use Automattic\WooCommerce\GoogleListingsAndAds\Google\NotificationsService; |
| 8 | +use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\ActionSchedulerJobMonitor; |
| 9 | +use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\ProductNotificationJob; |
| 10 | +use Automattic\WooCommerce\GoogleListingsAndAds\Jobs\Notifications\ShippingNotificationJob; |
| 11 | +use Automattic\WooCommerce\GoogleListingsAndAds\Tests\Framework\UnitTest; |
| 12 | +use PHPUnit\Framework\MockObject\MockObject; |
| 13 | +use WC_Helper_Product; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class ShippingNotificationJobTest |
| 17 | + * |
| 18 | + * @group Notifications |
| 19 | + * @package Automattic\WooCommerce\GoogleListingsAndAds\Tests\Unit\Jobs |
| 20 | + */ |
| 21 | +class ShippingNotificationJobTest extends UnitTest { |
| 22 | + |
| 23 | + /** @var MockObject|ActionScheduler $action_scheduler */ |
| 24 | + protected $action_scheduler; |
| 25 | + |
| 26 | + /** @var MockObject|ActionSchedulerJobMonitor $monitor */ |
| 27 | + protected $monitor; |
| 28 | + |
| 29 | + /** @var MockObject|NotificationsService $notification_service */ |
| 30 | + protected $notification_service; |
| 31 | + |
| 32 | + /** @var ProductNotificationJob $job */ |
| 33 | + protected $job; |
| 34 | + |
| 35 | + protected const JOB_NAME = 'notifications/shipping'; |
| 36 | + protected const PROCESS_ITEM_HOOK = 'gla/jobs/' . self::JOB_NAME . '/process_item'; |
| 37 | + |
| 38 | + /** |
| 39 | + * Runs before each test is executed. |
| 40 | + */ |
| 41 | + public function setUp(): void { |
| 42 | + parent::setUp(); |
| 43 | + |
| 44 | + $this->action_scheduler = $this->createMock( ActionScheduler::class ); |
| 45 | + $this->monitor = $this->createMock( ActionSchedulerJobMonitor::class ); |
| 46 | + $this->notification_service = $this->createMock( NotificationsService::class ); |
| 47 | + $this->job = new ShippingNotificationJob( |
| 48 | + $this->action_scheduler, |
| 49 | + $this->monitor, |
| 50 | + $this->notification_service |
| 51 | + ); |
| 52 | + |
| 53 | + $this->job->init(); |
| 54 | + } |
| 55 | + |
| 56 | + public function test_job_name() { |
| 57 | + $this->assertEquals( self::JOB_NAME, $this->job->get_name() ); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_schedule_schedules_immediate_job() { |
| 61 | + $this->notification_service->expects( $this->once() )->method( 'is_enabled' )->willReturn( true ); |
| 62 | + |
| 63 | + $this->action_scheduler->expects( $this->once() ) |
| 64 | + ->method( 'has_scheduled_action' ) |
| 65 | + ->with( self::PROCESS_ITEM_HOOK, [] ) |
| 66 | + ->willReturn( false ); |
| 67 | + |
| 68 | + $this->action_scheduler->expects( $this->once() ) |
| 69 | + ->method( 'schedule_immediate' ) |
| 70 | + ->with( self::PROCESS_ITEM_HOOK ); |
| 71 | + |
| 72 | + $this->job->schedule(); |
| 73 | + } |
| 74 | + |
| 75 | + public function test_schedule_doesnt_schedules_immediate_job_if_already_scheduled() { |
| 76 | + $this->notification_service->expects( $this->once() )->method( 'is_enabled' )->willReturn( true ); |
| 77 | + |
| 78 | + $this->action_scheduler->expects( $this->once() ) |
| 79 | + ->method( 'has_scheduled_action' ) |
| 80 | + ->with( self::PROCESS_ITEM_HOOK, [] ) |
| 81 | + ->willReturn( true ); |
| 82 | + |
| 83 | + $this->action_scheduler->expects( $this->never() )->method( 'schedule_immediate' ); |
| 84 | + |
| 85 | + $this->job->schedule(); |
| 86 | + } |
| 87 | + |
| 88 | + public function test_schedule_doesnt_schedules_immediate_job_if_not_enabled() { |
| 89 | + $this->notification_service->expects( $this->once() )->method( 'is_enabled' )->willReturn( false ); |
| 90 | + |
| 91 | + $this->action_scheduler->expects( $this->never() ) |
| 92 | + ->method( 'has_scheduled_action' ); |
| 93 | + |
| 94 | + $this->action_scheduler->expects( $this->never() )->method( 'schedule_immediate' ); |
| 95 | + |
| 96 | + $this->job->schedule(); |
| 97 | + } |
| 98 | + |
| 99 | + public function test_process_items_calls_notify() { |
| 100 | + $this->notification_service->expects( $this->once() ) |
| 101 | + ->method( 'notify' ) |
| 102 | + ->with( NotificationsService::TOPIC_SHIPPING_UPDATED ) |
| 103 | + ->willReturn( true ); |
| 104 | + |
| 105 | + $this->job->handle_process_items_action(); |
| 106 | + } |
| 107 | +} |
0 commit comments