Skip to content

Commit 878da57

Browse files
author
Tadas
committed
Implementation for get campaigns endpoint.
1 parent 1d59e6c commit 878da57

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/Api/Campaigns.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,27 @@ public function cancel($campaignId)
5555

5656
return $response['body'];
5757
}
58+
59+
/**
60+
* Get collection of items
61+
* @param array $fields
62+
* @return [type]
63+
*/
64+
public function get($type = 'sent', $fields = ['*'])
65+
{
66+
// filter anything that is not an available type
67+
$type = in_array($type, ['sent', 'draft', 'outbox']) ? $type : 'sent';
68+
69+
$params = $this->prepareParams();
70+
71+
if ( ! empty($fields) && is_array($fields) && $fields != ['*']) {
72+
$params['fields'] = $fields;
73+
}
74+
75+
$response = $this->restClient->get($this->endpoint . '/' . $type, $params);
76+
77+
$entities = $this->generateCollection($response['body']);
78+
79+
return $entities;
80+
}
5881
}

tests/CampaignsTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,29 @@ public function create_campaign()
3030
$this->campaignsApi->delete($campaign->id);
3131
}
3232

33+
/** @test **/
34+
public function get_campaigns()
35+
{
36+
// see drafts
37+
$drafts = $this->campaignsApi->get('draft');
38+
39+
$this->assertContainsValue($drafts, 'status', 'draft');
40+
$this->assertDoesNotContainValue($drafts, 'status', 'sent');
41+
$this->assertDoesNotContainValue($drafts, 'status', 'outbox');
42+
43+
// see outbox
44+
$outbox = $this->campaignsApi->get('outbox');
45+
46+
$this->assertContainsValue($outbox, 'status', 'outbox');
47+
$this->assertDoesNotContainValue($outbox, 'status', 'sent');
48+
$this->assertDoesNotContainValue($outbox, 'status', 'draft');
49+
50+
// see sent
51+
$sent = $this->campaignsApi->get();
52+
53+
$this->assertContainsValue($sent, 'status', 'sent');
54+
$this->assertDoesNotContainValue($sent, 'status', 'outbox');
55+
$this->assertDoesNotContainValue($sent, 'status', 'draft');
56+
}
57+
3358
}

tests/MlTestCase.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,28 @@ protected function addSubscribers($groupId, $count = 5)
3838
return $addedSubscribers->imported;
3939
}
4040

41+
protected function assertContainsValue($list, $key, $value)
42+
{
43+
return $this->assertTrue($this->containsValue($list, $key, $value));
44+
}
45+
46+
protected function assertDoesNotContainValue($list, $key, $value)
47+
{
48+
return $this->assertFalse($this->containsValue($list, $key, $value));
49+
}
50+
51+
protected function containsValue($list, $key, $value)
52+
{
53+
$found = false;
54+
55+
foreach ($list as $item) {
56+
if ($item->$key === $value) {
57+
$found = true;
58+
break;
59+
}
60+
}
61+
62+
return $found;
63+
}
64+
4165
}

0 commit comments

Comments
 (0)