Skip to content

Commit

Permalink
Create/get DB & API class directly from task constructor
Browse files Browse the repository at this point in the history
to simplify code and fix psalm errors
  • Loading branch information
Jinksi committed Jun 21, 2023
1 parent 120a8c2 commit 7a64f27
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 33 deletions.
11 changes: 4 additions & 7 deletions includes/admin/tasks/class-wc-payments-task-disputes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace WooCommerce\Payments\Tasks;

use Automattic\WooCommerce\Admin\Features\OnboardingTasks\Task;
use WC_Payments_Utils;
use WCPay\Database_Cache;
use WC_Payments_Utils;
use WC_Payments_API_Client;

defined( 'ABSPATH' ) || exit;
Expand All @@ -36,13 +36,10 @@ class WC_Payments_Task_Disputes extends Task {

/**
* WC_Payments_Task_Disputes constructor.
*
* @param WC_Payments_API_Client $api_client Payments API client.
* @param Database_Cache $database_cache Database cache util.
*/
public function __construct( $api_client, $database_cache ) {
$this->api_client = $api_client;
$this->database_cache = $database_cache;
public function __construct() {
$this->api_client = \WC_Payments::create_api_client();
$this->database_cache = \WC_Payments::get_database_cache();
parent::__construct();
}

Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-payments-tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public static function init() {
*/
public static function add_task_disputes_need_response() {
// 'extended' = 'Things to do next' task list on WooCommerce > Home.
TaskLists::add_task( 'extended', new WC_Payments_Task_Disputes( \WC_Payments::create_api_client(), \WC_Payments::get_database_cache() ) );
TaskLists::add_task( 'extended', new WC_Payments_Task_Disputes() );
}
}
30 changes: 5 additions & 25 deletions tests/unit/admin/tasks/test-class-wc-payments-task-disputes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ public function set_up() {
$this->_cache = WC_Payments::get_database_cache();
$this->mock_cache = $this->createMock( WCPay\Database_Cache::class );
WC_Payments::set_database_cache( $this->mock_cache );

$this->mock_api_client = $this->getMockBuilder( 'WC_Payments_API_Client' )
->disableOriginalConstructor()
->setMethods( [ 'get_disputes' ] )
->getMock();
}

public function tear_down() {
Expand All @@ -36,10 +31,7 @@ public function tear_down() {
}

public function test_disputes_task_with_single_dispute_outside_7days() {
$disputes_task = new WC_Payments_Task_Disputes(
$this->mock_api_client,
$this->mock_cache
);
$disputes_task = new WC_Payments_Task_Disputes();
$mock_active_disputes = [
[
'wcpay_disputes_cache_id' => 21,
Expand Down Expand Up @@ -67,10 +59,7 @@ public function test_disputes_task_with_single_dispute_outside_7days() {
}

public function test_disputes_task_with_single_dispute_within_7days() {
$disputes_task = new WC_Payments_Task_Disputes(
$this->mock_api_client,
$this->mock_cache
);
$disputes_task = new WC_Payments_Task_Disputes();
$mock_active_disputes = [
[
'wcpay_disputes_cache_id' => 21,
Expand Down Expand Up @@ -102,10 +91,7 @@ public function test_disputes_task_with_single_dispute_within_7days() {
}

public function test_disputes_task_with_single_dispute_within_24h() {
$disputes_task = new WC_Payments_Task_Disputes(
$this->mock_api_client,
$this->mock_cache
);
$disputes_task = new WC_Payments_Task_Disputes();
$mock_active_disputes = [
[
'wcpay_disputes_cache_id' => 21,
Expand Down Expand Up @@ -137,10 +123,7 @@ public function test_disputes_task_with_single_dispute_within_24h() {
}

public function test_disputes_task_with_multiple_disputes_within_7days() {
$disputes_task = new WC_Payments_Task_Disputes(
$this->mock_api_client,
$this->mock_cache
);
$disputes_task = new WC_Payments_Task_Disputes();
$mock_active_disputes = [
[
'wcpay_disputes_cache_id' => 21,
Expand Down Expand Up @@ -187,10 +170,7 @@ public function test_disputes_task_with_multiple_disputes_within_7days() {
}

public function test_disputes_task_with_multiple_disputes_within_24h() {
$disputes_task = new WC_Payments_Task_Disputes(
$this->mock_api_client,
$this->mock_cache
);
$disputes_task = new WC_Payments_Task_Disputes();
$mock_active_disputes = [
[
'wcpay_disputes_cache_id' => 21,
Expand Down

0 comments on commit 7a64f27

Please sign in to comment.