Skip to content

Commit a98ca65

Browse files
committed
Add Tests
1 parent 34e1329 commit a98ca65

File tree

2 files changed

+117
-8
lines changed

2 files changed

+117
-8
lines changed

tests/Unit/Jobs/ProductNotificationJobTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ public function test_schedule_schedules_immediate_job() {
6868
$topic = 'product.create';
6969
$id = 1;
7070

71+
$this->notification_service->expects( $this->once() )->method( 'is_enabled' )->willReturn( true );
72+
7173
$this->action_scheduler->expects( $this->once() )
7274
->method( 'has_scheduled_action' )
73-
->with( self::PROCESS_ITEM_HOOK, [ [ $id, $topic ] ] )
75+
->with( self::PROCESS_ITEM_HOOK, [ $id, $topic ] )
7476
->willReturn( false );
7577

7678
$this->action_scheduler->expects( $this->once() )
@@ -87,26 +89,26 @@ public function test_schedule_doesnt_schedules_immediate_job_if_already_schedule
8789
$id = 1;
8890
$topic = 'product.create';
8991

92+
$this->notification_service->expects( $this->once() )->method( 'is_enabled' )->willReturn( true );
93+
9094
$this->action_scheduler->expects( $this->once() )
9195
->method( 'has_scheduled_action' )
92-
->with( self::PROCESS_ITEM_HOOK, [ [ $id, $topic ] ] )
96+
->with( self::PROCESS_ITEM_HOOK, [ $id, $topic ] )
9397
->willReturn( true );
9498

9599
$this->action_scheduler->expects( $this->never() )->method( 'schedule_immediate' );
96100

97101
$this->job->schedule( [ $id, $topic ] );
98102
}
99103

100-
public function test_schedule_doesnt_schedules_immediate_job_if_filtered() {
104+
public function test_schedule_doesnt_schedules_immediate_job_if_not_enabled() {
101105
$id = 1;
102106
$topic = 'product.create';
103107

104-
add_filter( 'woocommerce_gla_product_notification_job_can_schedule', '__return_false' );
108+
$this->notification_service->expects( $this->once() )->method( 'is_enabled' )->willReturn( false );
105109

106-
$this->action_scheduler->expects( $this->once() )
107-
->method( 'has_scheduled_action' )
108-
->with( self::PROCESS_ITEM_HOOK, [ [ $id, $topic ] ] )
109-
->willReturn( false );
110+
$this->action_scheduler->expects( $this->never() )
111+
->method( 'has_scheduled_action' );
110112

111113
$this->action_scheduler->expects( $this->never() )->method( 'schedule_immediate' );
112114

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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

Comments
 (0)