Skip to content

Commit

Permalink
Merge pull request #20634 from wincher-ab/bug/wincher-undefined-array…
Browse files Browse the repository at this point in the history
…-key

Fix get_upgrade_campaign causing undefined array key warnings
  • Loading branch information
enricobattocchi authored Oct 19, 2023
2 parents 55c136e + 8727857 commit 009c121
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/actions/wincher/wincher-account-action.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ public function check_limit() {
*/
public function get_upgrade_campaign() {
try {
$result = $this->client->get( self::UPGRADE_CAMPAIGN_URL );
$type = $result['type'];
$months = $result['months'];
$result = $this->client->get( self::UPGRADE_CAMPAIGN_URL );
$type = isset( $result['type'] ) ? $result['type'] : null;
$months = isset( $result['months'] ) ? $result['months'] : null;
$discount = isset( $result['value'] ) ? $result['value'] : null;

// We display upgrade discount only if it's a rate discount and positive months.
if ( $type === 'RATE' && $months && $months > 0 ) {
$discount = $result['value'];
// We display upgrade discount only if it's a rate discount and positive months/discount.
if ( $type === 'RATE' && $months && $discount ) {

return (object) [
'discount' => $discount,
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/actions/wincher/wincher-account-action-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,25 @@ public function test_valid_get_upgrade_campaign() {
$this->instance->get_upgrade_campaign()
);
}

/**
* Tests empty get upgrade campaign.
*
* @covers ::get_upgrade_campaign
*/
public function test_empty_get_upgrade_campaign() {
$this->client_instance
->expects( 'get' )
->with( 'https://api.wincher.com/v1/yoast/upgrade-campaign' )
->andReturn( [] );

$this->assertEquals(
(object) [
'discount' => null,
'months' => null,
'status' => 200,
],
$this->instance->get_upgrade_campaign()
);
}
}

0 comments on commit 009c121

Please sign in to comment.