/tests/csrest_subscribers_test.php

http://github.com/campaignmonitor/createsend-php · PHP · 224 lines · 166 code · 58 blank · 0 comment · 0 complexity · dc7e4c478ab2ad0fa4ac7bd49c79d201 MD5 · raw file

  1. <?php
  2. require_once __DIR__.'/../vendor/autoload.php';
  3. require_once __DIR__.'/../vendor/simpletest/simpletest/autorun.php';
  4. @Mock::generate('CS_REST_Log');
  5. @Mock::generate('CS_REST_NativeJsonSerialiser');
  6. @Mock::generate('CS_REST_CurlTransport');
  7. class CS_REST_ApiKeyTestSubscribers extends CS_REST_TestSubscribers {
  8. var $auth = array('api_key' => 'not a real api key');
  9. }
  10. class CS_REST_OAuthTestSubscribers extends CS_REST_TestSubscribers {
  11. var $auth = array(
  12. 'access_token' => '7y872y3872i3eh',
  13. 'refresh_token' => 'kjw8qjd9ow8jo');
  14. }
  15. abstract class CS_REST_TestSubscribers extends CS_REST_TestBase {
  16. var $list_id = 'not a real list id';
  17. var $list_base_route;
  18. function set_up_inner() {
  19. $this->list_base_route = $this->base_route.'subscribers/'.$this->list_id;
  20. $this->wrapper = new CS_REST_Subscribers($this->list_id, $this->auth, $this->protocol, $this->log_level,
  21. $this->api_host, $this->mock_log, $this->mock_serialiser, $this->mock_transport);
  22. }
  23. function testadd() {
  24. $raw_result = '';
  25. $call_options = $this->get_call_options($this->list_base_route.'.json', 'POST');
  26. $subscriber = array (
  27. 'Email' => 'test@test.com',
  28. 'Name' => 'Widget Man!',
  29. 'CustomFields' => array(array(1,2), array(3,4)),
  30. 'ConsentToTrack' => 'yes'
  31. );
  32. $this->general_test_with_argument('add', $subscriber, $call_options,
  33. $raw_result, $raw_result, 'subscriber was serialised to this');
  34. }
  35. function testupdate() {
  36. $raw_result = '';
  37. $email = 'test@test.com';
  38. $serialised_subscriber = 'subscriber data';
  39. $call_options = $this->get_call_options(
  40. $this->list_base_route.'.json?email='.urlencode($email), 'PUT');
  41. $subscriber = array (
  42. 'Email' => 'test2@test.com',
  43. 'Name' => 'Widget Man!',
  44. 'CustomFields' => array(array(1,2), array(3,4)),
  45. 'ConsentToTrack' => 'unchanged',
  46. );
  47. $transport_result = array (
  48. 'code' => 200,
  49. 'response' => $raw_result
  50. );
  51. $expected_result = new CS_REST_Wrapper_Result($raw_result, 200);
  52. $call_options['data'] = $serialised_subscriber;
  53. $this->setup_transport_and_serialisation($transport_result, $call_options,
  54. $raw_result, $raw_result, $serialised_subscriber,
  55. $subscriber, 200);
  56. $result = $this->wrapper->update($email, $subscriber);
  57. $this->assertIdentical($expected_result, $result);
  58. }
  59. function testimport() {
  60. $raw_result = 'the import result';
  61. $response_code = 200;
  62. $resubscribe = true;
  63. $queueSubscriptionBasedAutoResponders = true;
  64. $restartSubscriptionBasedAutoResponders = false;
  65. $call_options = $this->get_call_options($this->list_base_route.'/import.json', 'POST');
  66. $subscribers = array(
  67. array (
  68. 'Email' => 'test@test.com',
  69. 'Name' => 'Widget Man!',
  70. 'CustomFields' => array(array(1,2), array(3,4)),
  71. 'ConsentToTrack' => 'no',
  72. ),
  73. array (
  74. 'Email' => 'test@test.com',
  75. 'Name' => 'Widget Man!',
  76. 'CustomFields' => array(array(1,2), array(3,4)),
  77. 'ConsentToTrack' => 'yes',
  78. )
  79. );
  80. $data = array(
  81. 'Resubscribe' => $resubscribe,
  82. 'QueueSubscriptionBasedAutoResponders' => $queueSubscriptionBasedAutoResponders,
  83. 'Subscribers' => $subscribers,
  84. 'RestartSubscriptionBasedAutoresponders' => $restartSubscriptionBasedAutoResponders
  85. );
  86. $transport_result = array (
  87. 'code' => $response_code,
  88. 'response' => $raw_result
  89. );
  90. $expected_result = new CS_REST_Wrapper_Result($raw_result, $response_code);
  91. $call_options['data'] = 'subscribers were serialised to this';
  92. $this->setup_transport_and_serialisation($transport_result, $call_options,
  93. $raw_result, $raw_result, 'subscribers were serialised to this',
  94. $data, $response_code);
  95. $result = $this->wrapper->import($subscribers, $resubscribe, $queueSubscriptionBasedAutoResponders);
  96. $this->assertIdentical($expected_result, $result);
  97. }
  98. function testget() {
  99. $raw_result = 'subscriber details';
  100. $deserialised = array(1,2,34,5);
  101. $response_code = 200;
  102. $email = 'test@test.com';
  103. $tracking_pref = 'true';
  104. $call_options = $this->get_call_options(
  105. $this->list_base_route.'.json?email='.urlencode($email).'&includeTrackingPreference='.$tracking_pref, 'GET');
  106. $transport_result = array (
  107. 'code' => $response_code,
  108. 'response' => $raw_result
  109. );
  110. $expected_result = new CS_REST_Wrapper_Result($deserialised, $response_code);
  111. $this->setup_transport_and_serialisation($transport_result, $call_options,
  112. $deserialised, $raw_result, NULL, NULL, $response_code);
  113. $result = $this->wrapper->get($email, true);
  114. $this->assertIdentical($expected_result, $result);
  115. }
  116. function testget_history() {
  117. $raw_result = 'subscriber history';
  118. $deserialised = array(1,2,34,5);
  119. $response_code = 200;
  120. $email = 'test@test.com';
  121. $call_options = $this->get_call_options(
  122. $this->list_base_route.'/history.json?email='.urlencode($email), 'GET');
  123. $transport_result = array (
  124. 'code' => $response_code,
  125. 'response' => $raw_result
  126. );
  127. $expected_result = new CS_REST_Wrapper_Result($deserialised, $response_code);
  128. $this->setup_transport_and_serialisation($transport_result, $call_options,
  129. $deserialised, $raw_result, NULL, NULL, $response_code);
  130. $result = $this->wrapper->get_history($email);
  131. $this->assertIdentical($expected_result, $result);
  132. }
  133. function testunsubscribe() {
  134. $raw_result = '';
  135. $response_code = 200;
  136. $email = 'test@test.com';
  137. $call_options = $this->get_call_options($this->list_base_route.'/unsubscribe.json', 'POST');
  138. $subscriber = array('EmailAddress' => $email);
  139. $transport_result = array (
  140. 'code' => $response_code,
  141. 'response' => $raw_result
  142. );
  143. $expected_result = new CS_REST_Wrapper_Result($raw_result, $response_code);
  144. $call_options['data'] = 'subscriber was serialised to this';
  145. $this->setup_transport_and_serialisation($transport_result, $call_options,
  146. $raw_result, $raw_result,
  147. 'subscriber was serialised to this', $subscriber, $response_code);
  148. $result = $this->wrapper->unsubscribe($email);
  149. $this->assertIdentical($expected_result, $result);
  150. }
  151. function testdelete() {
  152. $raw_result = '';
  153. $response_code = 200;
  154. $email = 'test@test.com';
  155. $call_options = $this->get_call_options($this->list_base_route.'.json?email='.urlencode($email), 'DELETE');
  156. $transport_result = array (
  157. 'code' => $response_code,
  158. 'response' => $raw_result
  159. );
  160. $expected_result = new CS_REST_Wrapper_Result($raw_result, $response_code);
  161. $this->setup_transport_and_serialisation($transport_result, $call_options,
  162. $raw_result, $raw_result, NULL, NULL, $response_code);
  163. $result = $this->wrapper->delete($email);
  164. $this->assertIdentical($expected_result, $result);
  165. }
  166. }