From 878da577421fa0e94a1812fc03ed781ab36a7e44 Mon Sep 17 00:00:00 2001 From: Tadas Date: Wed, 2 Nov 2016 14:14:54 +0200 Subject: [PATCH] Implementation for get campaigns endpoint. --- src/Api/Campaigns.php | 23 +++++++++++++++++++++++ tests/CampaignsTest.php | 25 +++++++++++++++++++++++++ tests/MlTestCase.php | 24 ++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/src/Api/Campaigns.php b/src/Api/Campaigns.php index 29dea80..96d0f5e 100644 --- a/src/Api/Campaigns.php +++ b/src/Api/Campaigns.php @@ -55,4 +55,27 @@ public function cancel($campaignId) return $response['body']; } + + /** + * Get collection of items + * @param array $fields + * @return [type] + */ + public function get($type = 'sent', $fields = ['*']) + { + // filter anything that is not an available type + $type = in_array($type, ['sent', 'draft', 'outbox']) ? $type : 'sent'; + + $params = $this->prepareParams(); + + if ( ! empty($fields) && is_array($fields) && $fields != ['*']) { + $params['fields'] = $fields; + } + + $response = $this->restClient->get($this->endpoint . '/' . $type, $params); + + $entities = $this->generateCollection($response['body']); + + return $entities; + } } \ No newline at end of file diff --git a/tests/CampaignsTest.php b/tests/CampaignsTest.php index f25f2eb..fccfb9b 100644 --- a/tests/CampaignsTest.php +++ b/tests/CampaignsTest.php @@ -30,4 +30,29 @@ public function create_campaign() $this->campaignsApi->delete($campaign->id); } + /** @test **/ + public function get_campaigns() + { + // see drafts + $drafts = $this->campaignsApi->get('draft'); + + $this->assertContainsValue($drafts, 'status', 'draft'); + $this->assertDoesNotContainValue($drafts, 'status', 'sent'); + $this->assertDoesNotContainValue($drafts, 'status', 'outbox'); + + // see outbox + $outbox = $this->campaignsApi->get('outbox'); + + $this->assertContainsValue($outbox, 'status', 'outbox'); + $this->assertDoesNotContainValue($outbox, 'status', 'sent'); + $this->assertDoesNotContainValue($outbox, 'status', 'draft'); + + // see sent + $sent = $this->campaignsApi->get(); + + $this->assertContainsValue($sent, 'status', 'sent'); + $this->assertDoesNotContainValue($sent, 'status', 'outbox'); + $this->assertDoesNotContainValue($sent, 'status', 'draft'); + } + } \ No newline at end of file diff --git a/tests/MlTestCase.php b/tests/MlTestCase.php index 2bf787a..94241fb 100644 --- a/tests/MlTestCase.php +++ b/tests/MlTestCase.php @@ -38,4 +38,28 @@ protected function addSubscribers($groupId, $count = 5) return $addedSubscribers->imported; } + protected function assertContainsValue($list, $key, $value) + { + return $this->assertTrue($this->containsValue($list, $key, $value)); + } + + protected function assertDoesNotContainValue($list, $key, $value) + { + return $this->assertFalse($this->containsValue($list, $key, $value)); + } + + protected function containsValue($list, $key, $value) + { + $found = false; + + foreach ($list as $item) { + if ($item->$key === $value) { + $found = true; + break; + } + } + + return $found; + } + } \ No newline at end of file