From 502983311d0ac9aca8129cba0f047b93c105e41d Mon Sep 17 00:00:00 2001 From: Velizar Zlatev Date: Fri, 17 May 2019 00:49:18 +0300 Subject: [PATCH 1/3] Extend MailchimpLists with methods for working with groups and interests --- src/MailchimpLists.php | 171 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) diff --git a/src/MailchimpLists.php b/src/MailchimpLists.php index 974a93c..b36e42b 100644 --- a/src/MailchimpLists.php +++ b/src/MailchimpLists.php @@ -68,6 +68,90 @@ public function getInterestCategories($list_id, $parameters = []) { return $this->request('GET', '/lists/{list_id}/interest-categories', $tokens, $parameters); } + /** + * Create new interest category associated with a list. + * + * @param string $list_id + * The ID of the list. + * @param string $title + * The title of interest category. + * @param string $type + * The type of interest category. + * @param array $parameters + * Associative array of optional request parameters. + * + * @return object + * + * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories + */ + public function addInterestCategories($list_id, $title, $type, $parameters = [], $batch = FALSE) { + $tokens = [ + 'list_id' => $list_id, + ]; + + $parameters += [ + 'title' => $title, + 'type' => $type, + ]; + + return $this->request('POST', '/lists/{list_id}/interest-categories', $tokens, $parameters); + } + + /** + * Update interest category associated with a list. + * + * @param string $list_id + * The ID of the list. + * @param string $interest_category_id + * The ID of the interest category. + * @param string $title + * The title of interest category. + * @param string $type + * The type of interest category. + * @param array $parameters + * Associative array of optional request parameters. + * + * @return object + * + * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories + */ + public function updateInterestCategories($list_id, $interest_category_id, $title, $type, $parameters = [], $batch = FALSE) { + $tokens = [ + 'list_id' => $list_id, + 'interest_category_id' => $interest_category_id, + ]; + + $parameters += [ + 'title' => $title, + 'type' => $type, + ]; + + return $this->request('PATCH', '/lists/{list_id}/interest-categories/{interest_category_id}', $tokens, $parameters); + } + + /** + * Update interest category associated with a list. + * + * @param string $list_id + * The ID of the list. + * @param string $interest_category_id + * The ID of the interest category. + * @param array $parameters + * Associative array of optional request parameters. + * + * @return object + * + * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories + */ + public function deleteInterestCategories($list_id, $interest_category_id, $parameters = [], $batch = FALSE) { + $tokens = [ + 'list_id' => $list_id, + 'interest_category_id' => $interest_category_id, + ]; + + return $this->request('DELETE', '/lists/{list_id}/interest-categories/{interest_category_id}', $tokens, $parameters); + } + /** * Gets information about all interests associated with an interest category. * @@ -91,6 +175,93 @@ public function getInterests($list_id, $interest_category_id, $parameters = []) return $this->request('GET', '/lists/{list_id}/interest-categories/{interest_category_id}/interests', $tokens, $parameters); } + /** + * Create new interest in category. + * + * @param string $list_id + * The ID of the list. + * @param string $interest_category_id + * The ID of interest category. + * @param string $name + * The name of interest. + * @param array $parameters + * Associative array of optional request parameters. + * + * @return object + * + * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories + */ + public function addInterests($list_id, $interest_category_id, $name, $parameters = [], $batch = FALSE) { + $tokens = [ + 'list_id' => $list_id, + 'interest_category_id' => $interest_category_id, + ]; + + $parameters += [ + 'name' => $name, + ]; + + return $this->request('POST', '/lists/{list_id}/interest-categories/{interest_category_id}/interests', $tokens, $parameters); + } + + /** + * Edit interest in category. + * + * @param string $list_id + * The ID of the list. + * @param string $interest_category_id + * The ID of interest category. + * @param string $interest_id + * The ID of interest. + * @param string $name + * The name of interest. + * @param array $parameters + * Associative array of optional request parameters. + * + * @return object + * + * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories + */ + public function updateInterests($list_id, $interest_category_id, $interest_id, $name, $parameters = [], $batch = FALSE) { + $tokens = [ + 'list_id' => $list_id, + 'interest_category_id' => $interest_category_id, + 'interest_id' => $interest_id, + ]; + + $parameters += [ + 'name' => $name, + ]; + + return $this->request('PATCH', '/lists/{list_id}/interest-categories/{interest_category_id}/interests/{interest_id}', $tokens, $parameters); + } + + /** + * Delete interest in category. + * + * @param string $list_id + * The ID of the list. + * @param string $interest_category_id + * The ID of interest category. + * @param string $interest_id + * The ID of interest. + * @param array $parameters + * Associative array of optional request parameters. + * + * @return object + * + * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories + */ + public function deleteInterests($list_id, $interest_category_id, $interest_id, $parameters = [], $batch = FALSE) { + $tokens = [ + 'list_id' => $list_id, + 'interest_category_id' => $interest_category_id, + 'interest_id' => $interest_id, + ]; + + return $this->request('DELETE', '/lists/{list_id}/interest-categories/{interest_category_id}/interests/{interest_id}', $tokens, $parameters); + } + /** * Gets merge fields associated with a Mailchimp list. * From b73ee78ee18c8320c4416d086bfa4a7a3cdb1a47 Mon Sep 17 00:00:00 2001 From: Velizar Zlatev Date: Wed, 20 Nov 2019 18:39:53 +0200 Subject: [PATCH 2/3] Add tests for Interest Categories --- tests/MailchimpListsTest.php | 59 ++++++++++++++++++++++++++++++++++++ tests/src/MailchimpLists.php | 41 +++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/tests/MailchimpListsTest.php b/tests/MailchimpListsTest.php index a2145e5..218d3d7 100644 --- a/tests/MailchimpListsTest.php +++ b/tests/MailchimpListsTest.php @@ -48,6 +48,65 @@ public function testGetInterestCategories() { $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories', $mc->getClient()->uri); } + /** + * Tests library functionality for adding new interest categories. + */ + public function testAddInterestCategories() { + $list_id = '57afe96172'; + $title = 'Test Interest Category'; + $type = 'checkbox'; + + $mc = new MailchimpLists(); + $mc->addInterestCategories($list_id, $title, $type); + + $this->assertEquals('POST', $mc->getClient()->method); + $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories', $mc->getClient()->uri); + + $this->assertNotEmpty($mc->getClient()->options['json']); + + $request_body = $mc->getClient()->options['json']; + + $this->assertEquals($title, $request_body->title); + $this->assertEquals($type, $request_body->type); + } + + /** + * Tests library functionality for updating interest categories. + */ + public function testUpdateInterestCategories() { + $list_id = '57afe96172'; + $title = 'Test Interest Category Edited'; + $type = 'dropdown'; + $interest_category_id = '08f0b1d7b2'; + + $mc = new MailchimpLists(); + $mc->updateInterestCategories($list_id, $interest_category_id, $title, $type); + + $this->assertEquals('PATCH', $mc->getClient()->method); + $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id, $mc->getClient()->uri); + + $this->assertNotEmpty($mc->getClient()->options['json']); + + $request_body = $mc->getClient()->options['json']; + + $this->assertEquals($title, $request_body->title); + $this->assertEquals($type, $request_body->type); + } + + /** + * Tests library functionality for deleting interest categories. + */ + public function testDeleteInterestCategories() { + $list_id = '57afe96172'; + $interest_category_id = '08f0b1d7b2'; + + $mc = new MailchimpLists(); + $mc->deleteInterestCategories($list_id, $interest_category_id); + + $this->assertEquals('DELETE', $mc->getClient()->method); + $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id, $mc->getClient()->uri); + } + /** * Tests library functionality for interests information. */ diff --git a/tests/src/MailchimpLists.php b/tests/src/MailchimpLists.php index 3aa9e1e..06a257f 100644 --- a/tests/src/MailchimpLists.php +++ b/tests/src/MailchimpLists.php @@ -86,6 +86,47 @@ public function getInterestCategories($list_id, $parameters = []) { return $response; } + /** + * @inheritdoc + */ + public function addInterestCategories($list_id, $title, $type, $parameters = [], $batch = FALSE) { + parent::addInterestCategories($list_id, $title, $type, $parameters = [], $batch = FALSE); + + $response = (object) [ + 'list_id' => $list_id, + 'id' => 'a1e9f4b7f6', + 'title' => $title, + 'type' => $type, + ]; + + return $response; + } + + /** + * @inheritdoc + */ + public function updateInterestCategories($list_id, $interest_category_id, $title, $type, $parameters = [], $batch = FALSE) { + parent::updateInterestCategories($list_id, $interest_category_id, $title, $type, $parameters = [], $batch = FALSE); + + $response = (object) [ + 'list_id' => $list_id, + 'id' => 'a1e9f4b7f6', + 'title' => $title, + 'type' => $type, + ]; + + return $response; + } + + /** + * @inheritdoc + */ + public function deleteInterestCategories($list_id, $interest_category_id, $parameters = [], $batch = FALSE) { + parent::deleteInterestCategories($list_id, $interest_category_id, $parameters = [], $batch = FALSE); + + return (!empty($list_id) && !empty($interest_category_id)); + } + /** * @inheritdoc */ From 32e58afcaeead4711a4a955bb0779a40e6096195 Mon Sep 17 00:00:00 2001 From: Velizar Zlatev Date: Wed, 20 Nov 2019 19:29:08 +0200 Subject: [PATCH 3/3] Add tests for Interest --- src/MailchimpLists.php | 2 +- tests/MailchimpListsTest.php | 58 ++++++++++++++++++++++++++++++++++++ tests/src/MailchimpLists.php | 42 ++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/MailchimpLists.php b/src/MailchimpLists.php index b36e42b..6f49d4f 100644 --- a/src/MailchimpLists.php +++ b/src/MailchimpLists.php @@ -189,7 +189,7 @@ public function getInterests($list_id, $interest_category_id, $parameters = []) * * @return object * - * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories + * @see https://mailchimp.com/developer/reference/lists/interest-categories/interests/ */ public function addInterests($list_id, $interest_category_id, $name, $parameters = [], $batch = FALSE) { $tokens = [ diff --git a/tests/MailchimpListsTest.php b/tests/MailchimpListsTest.php index 218d3d7..29f5813 100644 --- a/tests/MailchimpListsTest.php +++ b/tests/MailchimpListsTest.php @@ -121,6 +121,64 @@ public function testGetInterests() { $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id . '/interests', $mc->getClient()->uri); } + /** + * Tests library functionality for adding interests. + */ + public function testAddInterests() { + $list_id = '57afe96172'; + $interest_category_id = '08f0b1d7b2'; + $name = 'Test Interest'; + + $mc = new MailchimpLists(); + $mc->addInterests($list_id, $interest_category_id, $name); + + $this->assertEquals('POST', $mc->getClient()->method); + $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id . '/interests', $mc->getClient()->uri); + + $this->assertNotEmpty($mc->getClient()->options['json']); + + $request_body = $mc->getClient()->options['json']; + + $this->assertEquals($name, $request_body->name); + } + + /** + * Tests library functionality for updating interests. + */ + public function testUpdateInterests() { + $list_id = '57afe96172'; + $interest_category_id = '08f0b1d7b2'; + $interest_id = '9143cf3bd1'; + $name = 'Test Interest Edited'; + + $mc = new MailchimpLists(); + $mc->updateInterests($list_id, $interest_category_id, $interest_id, $name); + + $this->assertEquals('PATCH', $mc->getClient()->method); + $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id . '/interests/' . $interest_id, $mc->getClient()->uri); + + $this->assertNotEmpty($mc->getClient()->options['json']); + + $request_body = $mc->getClient()->options['json']; + + $this->assertEquals($name, $request_body->name); + } + + /** + * Tests library functionality for deleting interests. + */ + public function testDeleteInterests() { + $list_id = '57afe96172'; + $interest_category_id = '08f0b1d7b2'; + $interest_id = '9143cf3bd1'; + + $mc = new MailchimpLists(); + $mc->deleteInterests($list_id, $interest_category_id, $interest_id); + + $this->assertEquals('DELETE', $mc->getClient()->method); + $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id . '/interests/' . $interest_id, $mc->getClient()->uri); + } + /** * Tests library functionality for merge fields information. */ diff --git a/tests/src/MailchimpLists.php b/tests/src/MailchimpLists.php index 06a257f..1b4b0e9 100644 --- a/tests/src/MailchimpLists.php +++ b/tests/src/MailchimpLists.php @@ -113,6 +113,7 @@ public function updateInterestCategories($list_id, $interest_category_id, $title 'id' => 'a1e9f4b7f6', 'title' => $title, 'type' => $type, + 'interest_category_id' => $interest_category_id, ]; return $response; @@ -148,6 +149,47 @@ public function getInterests($list_id, $interest_category_id, $parameters = []) return $response; } + /** + * @inheritdoc + */ + public function addInterests($list_id, $interest_category_id, $name, $parameters = [], $batch = FALSE) { + parent::addInterests($list_id, $interest_category_id, $name, $parameters = [], $batch = FALSE); + + $response = (object) [ + 'list_id' => $list_id, + 'category_id' => $interest_category_id, + 'interest_id' => '9143cf3bd1', + 'name' => $name, + ]; + + return $response; + } + + /** + * @inheritdoc + */ + public function updateInterests($list_id, $interest_category_id, $interest_id, $name, $parameters = [], $batch = FALSE) { + parent::updateInterests($list_id, $interest_category_id, $interest_id, $name, $parameters = [], $batch = FALSE); + + $response = (object) [ + 'list_id' => $list_id, + 'category_id' => $interest_category_id, + 'interest_id' => $interest_id, + 'name' => $name, + ]; + + return $response; + } + + /** + * @inheritdoc + */ + public function deleteInterests($list_id, $interest_category_id, $interest_id, $parameters = [], $batch = FALSE) { + parent::deleteInterests($list_id, $interest_category_id, $interest_id, $parameters = [], $batch = FALSE); + + return (!empty($list_id) && !empty($interest_category_id) && !empty($interest_id)); + } + /** * @inheritdoc */