Skip to content

Commit

Permalink
Add campaign type property to campaign class
Browse files Browse the repository at this point in the history
  • Loading branch information
Nima committed Jan 3, 2023
1 parent 1687416 commit 8ef6532
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 66 deletions.
2 changes: 1 addition & 1 deletion plugins/woocommerce/src/Admin/API/MarketingCampaigns.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function get_items( $request ) {
public function prepare_item_for_response( $item, $request ) {
$data = [
'id' => $item->get_id(),
'channel' => $item->get_channel()->get_slug(),
'channel' => $item->get_type()->get_channel()->get_slug(),
'title' => $item->get_title(),
'manage_url' => $item->get_manage_url(),
];
Expand Down
47 changes: 15 additions & 32 deletions plugins/woocommerce/src/Admin/Marketing/MarketingCampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@

namespace Automattic\WooCommerce\Admin\Marketing;

use JsonSerializable;

/**
* MarketingCampaign class
*
* @since x.x.x
*/
class MarketingCampaign implements JsonSerializable {
class MarketingCampaign {
/**
* The unique identifier.
*
Expand All @@ -23,11 +21,11 @@ class MarketingCampaign implements JsonSerializable {
protected $id;

/**
* The marketing channel that this campaign belongs to.
* The marketing campaign type.
*
* @var MarketingChannelInterface
* @var MarketingCampaignType
*/
protected $channel;
protected $type;

/**
* Title of the marketing campaign.
Expand All @@ -53,15 +51,15 @@ class MarketingCampaign implements JsonSerializable {
/**
* MarketingCampaign constructor.
*
* @param string $id The marketing campaign's unique identifier.
* @param MarketingChannelInterface $channel The marketing channel that this campaign belongs to.
* @param string $title The title of the marketing campaign.
* @param string $manage_url The URL to the channel's campaign management page.
* @param Price|null $cost The cost of the marketing campaign with the currency.
* @param string $id The marketing campaign's unique identifier.
* @param MarketingCampaignType $type The marketing campaign type.
* @param string $title The title of the marketing campaign.
* @param string $manage_url The URL to the channel's campaign management page.
* @param Price|null $cost The cost of the marketing campaign with the currency.
*/
public function __construct( string $id, MarketingChannelInterface $channel, string $title, string $manage_url, Price $cost = null ) {
public function __construct( string $id, MarketingCampaignType $type, string $title, string $manage_url, Price $cost = null ) {
$this->id = $id;
$this->channel = $channel;
$this->type = $type;
$this->title = $title;
$this->manage_url = $manage_url;
$this->cost = $cost;
Expand All @@ -77,12 +75,12 @@ public function get_id(): string {
}

/**
* Returns the marketing channel that this campaign belongs to.
* Returns the marketing campaign type.
*
* @return MarketingChannelInterface
* @return MarketingCampaignType
*/
public function get_channel(): MarketingChannelInterface {
return $this->channel;
public function get_type(): MarketingCampaignType {
return $this->type;
}

/**
Expand Down Expand Up @@ -111,19 +109,4 @@ public function get_manage_url(): string {
public function get_cost(): ?Price {
return $this->cost;
}

/**
* Serialize the marketing campaign data.
*
* @return array
*/
public function jsonSerialize() {
return [
'id' => $this->get_id(),
'channel' => $this->get_channel()->get_slug(),
'title' => $this->get_title(),
'manage_url' => $this->get_manage_url(),
'cost' => $this->get_cost(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Automattic\WooCommerce\Tests\Admin\Marketing;

use Automattic\WooCommerce\Admin\Marketing\MarketingCampaign;
use Automattic\WooCommerce\Admin\Marketing\MarketingChannelInterface;
use Automattic\WooCommerce\Admin\Marketing\MarketingCampaignType;
use Automattic\WooCommerce\Admin\Marketing\Price;
use WC_Unit_Test_Case;

Expand All @@ -13,15 +13,15 @@
class MarketingCampaignTest extends WC_Unit_Test_Case {

/**
* @testdox `get_id`, `get_title`, `get_manage_url`, and `get_cost` return the class properties set by the constructor.
* @testdox `get_id`, `get_type`, `get_title`, `get_manage_url`, and `get_cost` return the class properties set by the constructor.
*/
public function test_get_methods_return_properties() {
$test_channel_1 = $this->createMock( MarketingChannelInterface::class );
$test_channel_1->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' );
$test_campaign_type_1 = $this->createMock( MarketingCampaignType::class );

$marketing_campaign = new MarketingCampaign( '1234', $test_channel_1, 'Ad #1234', 'https://example.com/manage-campaigns', new Price( '1000', 'USD' ) );
$marketing_campaign = new MarketingCampaign( '1234', $test_campaign_type_1, 'Ad #1234', 'https://example.com/manage-campaigns', new Price( '1000', 'USD' ) );

$this->assertEquals( '1234', $marketing_campaign->get_id() );
$this->assertEquals( $test_campaign_type_1, $marketing_campaign->get_type() );
$this->assertEquals( 'Ad #1234', $marketing_campaign->get_title() );
$this->assertEquals( 'https://example.com/manage-campaigns', $marketing_campaign->get_manage_url() );
$this->assertNotNull( $marketing_campaign->get_cost() );
Expand All @@ -33,36 +33,10 @@ public function test_get_methods_return_properties() {
* @testdox `cost` property can be null.
*/
public function test_cost_can_be_null() {
$test_channel_1 = $this->createMock( MarketingChannelInterface::class );
$test_campaign_type_1 = $this->createMock( MarketingCampaignType::class );

$marketing_campaign = new MarketingCampaign( '1234', $test_channel_1, 'Ad #1234', 'https://example.com/manage-campaigns' );
$marketing_campaign = new MarketingCampaign( '1234', $test_campaign_type_1, 'Ad #1234', 'https://example.com/manage-campaigns' );

$this->assertNull( $marketing_campaign->get_cost() );
}

/**
* @testdox It can be serialized to JSON including all its properties.
*/
public function test_can_be_serialized_to_json() {
$test_channel_1 = $this->createMock( MarketingChannelInterface::class );
$test_channel_1->expects( $this->any() )->method( 'get_slug' )->willReturn( 'test-channel-1' );

$marketing_campaign = new MarketingCampaign( '1234', $test_channel_1, 'Ad #1234', 'https://example.com/manage-campaigns', new Price( '1000', 'USD' ) );

$json = wp_json_encode( $marketing_campaign );
$this->assertNotEmpty( $json );
$this->assertEqualSets(
[
'id' => $marketing_campaign->get_id(),
'channel' => 'test-channel-1',
'title' => $marketing_campaign->get_title(),
'manage_url' => $marketing_campaign->get_manage_url(),
'cost' => [
'value' => $marketing_campaign->get_cost()->get_value(),
'currency' => $marketing_campaign->get_cost()->get_currency(),
],
],
json_decode( $json, true )
);
}
}

0 comments on commit 8ef6532

Please sign in to comment.