Nessuna descrizione

TopicTest.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. require_once(dirname(dirname(__FILE__)).'/mns-autoloader.php');
  3. use AliyunMNS\Client;
  4. use AliyunMNS\Topic;
  5. use AliyunMNS\Constants;
  6. use AliyunMNS\AsyncCallback;
  7. use AliyunMNS\Model\TopicAttributes;
  8. use AliyunMNS\Model\MailAttributes;
  9. use AliyunMNS\Model\SmsAttributes;
  10. use AliyunMNS\Model\BatchSmsAttributes;
  11. // use AliyunMNS\Model\WebSocketAttributes;
  12. use AliyunMNS\Model\MessageAttributes;
  13. use AliyunMNS\Model\SubscriptionAttributes;
  14. use AliyunMNS\Model\UpdateSubscriptionAttributes;
  15. use AliyunMNS\Exception\MnsException;
  16. use AliyunMNS\Requests\CreateQueueRequest;
  17. use AliyunMNS\Requests\CreateTopicRequest;
  18. use AliyunMNS\Requests\GetTopicAttributeRequest;
  19. use AliyunMNS\Requests\SetTopicAttributeRequest;
  20. use AliyunMNS\Requests\PublishMessageRequest;
  21. class TopicTest extends \PHPUnit_Framework_TestCase
  22. {
  23. private $accessId;
  24. private $accessKey;
  25. private $endPoint;
  26. private $client;
  27. private $topicToDelete;
  28. public function setUp()
  29. {
  30. $ini_array = parse_ini_file(__DIR__ . "/aliyun-mns.ini");
  31. $this->endPoint = $ini_array["endpoint"];
  32. $this->accessId = $ini_array["accessid"];
  33. $this->accessKey = $ini_array["accesskey"];
  34. $this->topicToDelete = array();
  35. $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
  36. }
  37. public function tearDown()
  38. {
  39. foreach ($this->topicToDelete as $topicName)
  40. {
  41. try
  42. {
  43. $this->client->deleteTopic($topicName);
  44. }
  45. catch (\Exception $e)
  46. {
  47. }
  48. }
  49. }
  50. private function prepareTopic($topicName, $attributes = NULL)
  51. {
  52. $request = new CreateTopicRequest($topicName, $attributes);
  53. $this->topicToDelete[] = $topicName;
  54. try
  55. {
  56. $res = $this->client->createTopic($request);
  57. $this->assertTrue($res->isSucceed());
  58. }
  59. catch (MnsException $e)
  60. {
  61. $this->assertTrue(FALSE, $e);
  62. }
  63. return $this->client->getTopicRef($topicName);
  64. }
  65. private function prepareSubscription(Topic $topic, $subscriptionName)
  66. {
  67. try
  68. {
  69. $attributes = new SubscriptionAttributes($subscriptionName, 'http://127.0.0.1', 'BACKOFF_RETRY', 'XML');
  70. $topic->subscribe($attributes);
  71. }
  72. catch (MnsException $e)
  73. {
  74. }
  75. }
  76. public function testLoggingEnabled()
  77. {
  78. $topicName = "testLoggingEnabled";
  79. $topic = $this->prepareTopic($topicName);
  80. try
  81. {
  82. $attributes = new TopicAttributes;
  83. $attributes->setLoggingEnabled(false);
  84. $topic->setAttribute($attributes);
  85. $res = $topic->getAttribute();
  86. $this->assertTrue($res->isSucceed());
  87. $this->assertEquals(false, $res->getTopicAttributes()->getLoggingEnabled());
  88. $attributes = new TopicAttributes;
  89. $attributes->setLoggingEnabled(true);
  90. $topic->setAttribute($attributes);
  91. $res = $topic->getAttribute();
  92. $this->assertTrue($res->isSucceed());
  93. $this->assertEquals(true, $res->getTopicAttributes()->getLoggingEnabled());
  94. $attributes = new TopicAttributes;
  95. $topic->setAttribute($attributes);
  96. $res = $topic->getAttribute();
  97. $this->assertTrue($res->isSucceed());
  98. $this->assertEquals(true, $res->getTopicAttributes()->getLoggingEnabled());
  99. }
  100. catch (MnsException $e)
  101. {
  102. $this->assertTrue(FALSE, $e);
  103. }
  104. }
  105. public function testTopicAttributes()
  106. {
  107. $topicName = "testTopicAttributes";
  108. $topic = $this->prepareTopic($topicName);
  109. try
  110. {
  111. $res = $topic->getAttribute();
  112. $this->assertTrue($res->isSucceed());
  113. $this->assertEquals($topicName, $res->getTopicAttributes()->getTopicName());
  114. }
  115. catch (MnsException $e)
  116. {
  117. $this->assertTrue(FALSE, $e);
  118. }
  119. $maximumMessageSize = 10 * 1024;
  120. $attributes = new TopicAttributes;
  121. $attributes->setMaximumMessageSize($maximumMessageSize);
  122. try
  123. {
  124. $res = $topic->setAttribute($attributes);
  125. $this->assertTrue($res->isSucceed());
  126. }
  127. catch (MnsException $e)
  128. {
  129. $this->assertTrue(FALSE, $e);
  130. }
  131. try
  132. {
  133. $res = $topic->getAttribute();
  134. $this->assertTrue($res->isSucceed());
  135. $this->assertEquals($res->getTopicAttributes()->getMaximumMessageSize(), $maximumMessageSize);
  136. }
  137. catch (MnsException $e)
  138. {
  139. $this->assertTrue(FALSE, $e);
  140. }
  141. $this->client->deleteTopic($topicName);
  142. try
  143. {
  144. $res = $topic->getAttribute();
  145. $this->assertTrue(False, "Should throw TopicNotExistException");
  146. }
  147. catch (MnsException $e)
  148. {
  149. $this->assertEquals($e->getMnsErrorCode(), Constants::TOPIC_NOT_EXIST);
  150. }
  151. try
  152. {
  153. $res = $topic->setAttribute($attributes);
  154. $this->assertTrue(False, "Should throw TopicNotExistException");
  155. }
  156. catch (MnsException $e)
  157. {
  158. $this->assertEquals($e->getMnsErrorCode(), Constants::TOPIC_NOT_EXIST);
  159. }
  160. }
  161. public function testPublishMessage()
  162. {
  163. $topicName = "testPublishMessage" . uniqid();
  164. $messageBody = "test";
  165. $bodyMD5 = md5($messageBody);
  166. $request = new PublishMessageRequest($messageBody);
  167. $topic = $this->prepareTopic($topicName);
  168. try
  169. {
  170. $res = $topic->publishMessage($request);
  171. $this->assertTrue($res->isSucceed());
  172. $this->assertEquals(strtoupper($bodyMD5), $res->getMessageBodyMD5());
  173. }
  174. catch (MnsException $e)
  175. {
  176. $this->assertTrue(FALSE, $e);
  177. }
  178. $this->client->deleteTopic($topic->getTopicName());
  179. try
  180. {
  181. $res = $topic->publishMessage($request);
  182. $this->assertTrue(False, "Should throw TopicNotExistException");
  183. }
  184. catch (MnsException $e)
  185. {
  186. $this->assertEquals($e->getMnsErrorCode(), Constants::TOPIC_NOT_EXIST);
  187. }
  188. }
  189. public function testPublishBatchSmsMessage()
  190. {
  191. $topicName = "testPublishBatchSmsMessage" . uniqid();
  192. // now sub and send message
  193. $messageBody = "test";
  194. $bodyMD5 = md5($messageBody);
  195. $topic = $this->prepareTopic($topicName);
  196. try
  197. {
  198. $smsEndpoint = $topic->generateSmsEndpoint();
  199. $subscriptionName = 'testSubscribeSubscription' . uniqid();
  200. $attributes = new SubscriptionAttributes($subscriptionName, $smsEndpoint);
  201. $topic->subscribe($attributes);
  202. $batchSmsAttributes = new BatchSmsAttributes("陈舟锋", "SMS_15535414");
  203. $batchSmsAttributes->addReceiver("13735576932", array("name" => "phpsdk-batchsms"));
  204. $messageAttributes = new MessageAttributes(array($batchSmsAttributes));
  205. $request = new PublishMessageRequest($messageBody, $messageAttributes);
  206. $res = $topic->publishMessage($request);
  207. $this->assertTrue($res->isSucceed());
  208. $this->assertEquals(strtoupper($bodyMD5), $res->getMessageBodyMD5());
  209. echo $res->getMessageId();
  210. sleep(5);
  211. }
  212. catch (MnsException $e)
  213. {
  214. $this->assertTrue(FALSE, $e);
  215. }
  216. $this->client->deleteTopic($topic->getTopicName());
  217. }
  218. public function testPublishDirectSmsMessage()
  219. {
  220. $topicName = "testPublishDirectSmsMessage" . uniqid();
  221. // now sub and send message
  222. $messageBody = "test";
  223. $bodyMD5 = md5($messageBody);
  224. $topic = $this->prepareTopic($topicName);
  225. try
  226. {
  227. $smsEndpoint = $topic->generateSmsEndpoint();
  228. $subscriptionName = 'testSubscribeSubscription' . uniqid();
  229. $attributes = new SubscriptionAttributes($subscriptionName, $smsEndpoint);
  230. $topic->subscribe($attributes);
  231. $smsParams = array("name" => "phpsdk");
  232. $smsAttributes = new SmsAttributes("陈舟锋", "SMS_15535414", $smsParams, "13735576932");
  233. $messageAttributes = new MessageAttributes($smsAttributes);
  234. $request = new PublishMessageRequest($messageBody, $messageAttributes);
  235. $res = $topic->publishMessage($request);
  236. $this->assertTrue($res->isSucceed());
  237. $this->assertEquals(strtoupper($bodyMD5), $res->getMessageBodyMD5());
  238. echo $res->getMessageId();
  239. sleep(5);
  240. }
  241. catch (MnsException $e)
  242. {
  243. $this->assertTrue(FALSE, $e);
  244. }
  245. $this->client->deleteTopic($topic->getTopicName());
  246. }
  247. public function testPublishMailMessage()
  248. {
  249. $topicName = "testPublishMailMessage" . uniqid();
  250. // now sub and send message
  251. $messageBody = "test";
  252. $bodyMD5 = md5($messageBody);
  253. $topic = $this->prepareTopic($topicName);
  254. try
  255. {
  256. $mailEndpoint = $topic->generateMailEndpoint("liji.canglj@alibaba-inc.com");
  257. $subscriptionName = 'testSubscribeSubscription' . uniqid();
  258. $attributes = new SubscriptionAttributes($subscriptionName, $mailEndpoint);
  259. $topic->subscribe($attributes);
  260. $mailAttributes = new MailAttributes("TestSubject", "TestAccountName");
  261. $messageAttributes = new MessageAttributes($mailAttributes);
  262. $request = new PublishMessageRequest($messageBody, $messageAttributes);
  263. $res = $topic->publishMessage($request);
  264. $this->assertTrue($res->isSucceed());
  265. $this->assertEquals(strtoupper($bodyMD5), $res->getMessageBodyMD5());
  266. echo $res->getMessageId();
  267. sleep(5);
  268. }
  269. catch (MnsException $e)
  270. {
  271. $this->assertTrue(FALSE, $e);
  272. }
  273. $this->client->deleteTopic($topic->getTopicName());
  274. }
  275. public function testPublishQueueMessage()
  276. {
  277. $topicName = "testPublishQueueMessage" . uniqid();
  278. // prepare the queue
  279. $queueName = "testPublishQueueMessageQueue";
  280. $this->client->deleteQueue($queueName);
  281. $request = new CreateQueueRequest($queueName);
  282. $this->client->createQueue($request);
  283. // now sub and send message
  284. $messageBody = "test";
  285. $bodyMD5 = md5($messageBody);
  286. $topic = $this->prepareTopic($topicName);
  287. try
  288. {
  289. $queue = $this->client->getQueueRef($queueName, FALSE);
  290. $queueEndpoint = $topic->generateQueueEndpoint($queueName);
  291. //echo($queueEndpoint);
  292. $subscriptionName = 'testSubscribeSubscription' . uniqid();
  293. $attributes = new SubscriptionAttributes($subscriptionName, $queueEndpoint);
  294. $topic->subscribe($attributes);
  295. $request = new PublishMessageRequest($messageBody);
  296. $res = $topic->publishMessage($request);
  297. $this->assertTrue($res->isSucceed());
  298. $this->assertEquals(strtoupper($bodyMD5), $res->getMessageBodyMD5());
  299. $res = $queue->receiveMessage(30);
  300. $this->assertTrue(strpos($res->getMessageBody(), "<Message>" . $messageBody . "</Message>") >= 0);
  301. }
  302. catch (MnsException $e)
  303. {
  304. $this->assertTrue(FALSE, $e);
  305. }
  306. $this->client->deleteTopic($topic->getTopicName());
  307. $this->client->deleteQueue($queueName);
  308. }
  309. public function testSubscribe()
  310. {
  311. $topicName = 'testSubscribeTopic' . uniqid();
  312. $topic = $this->prepareTopic($topicName);
  313. $subscriptionName = 'testSubscribeSubscription' . uniqid();
  314. $attributes = new SubscriptionAttributes($subscriptionName, 'http://127.0.0.1', 'BACKOFF_RETRY', 'XML');
  315. try
  316. {
  317. $topic->subscribe($attributes);
  318. }
  319. catch (MnsException $e)
  320. {
  321. $this->assertTrue(FALSE, $e);
  322. }
  323. try
  324. {
  325. $attributes->setContentFormat('SIMPLIFIED');
  326. $res = $topic->subscribe($attributes);
  327. $this->assertTrue(False, "Should throw SubscriptionAlreadyExist");
  328. }
  329. catch (MnsException $e)
  330. {
  331. $this->assertEquals($e->getMnsErrorCode(), Constants::SUBSCRIPTION_ALREADY_EXIST);
  332. }
  333. $topic->unsubscribe($subscriptionName);
  334. }
  335. public function testSubscriptionAttributes()
  336. {
  337. $topicName = "testSubscriptionAttributes" . uniqid();
  338. $subscriptionName = "testSubscriptionAttributes" . uniqid();
  339. $topic = $this->prepareTopic($topicName);
  340. $this->prepareSubscription($topic, $subscriptionName);
  341. try
  342. {
  343. $res = $topic->getSubscriptionAttribute($subscriptionName);
  344. $this->assertTrue($res->isSucceed());
  345. $this->assertEquals($topicName, $res->getSubscriptionAttributes()->getTopicName());
  346. $this->assertEquals('BACKOFF_RETRY', $res->getSubscriptionAttributes()->getStrategy());
  347. }
  348. catch (MnsException $e)
  349. {
  350. $this->assertTrue(FALSE, $e);
  351. }
  352. $strategy = 'EXPONENTIAL_DECAY_RETRY';
  353. $attributes = new UpdateSubscriptionAttributes($subscriptionName);
  354. $attributes->setStrategy($strategy);
  355. try
  356. {
  357. $res = $topic->setSubscriptionAttribute($attributes);
  358. $this->assertTrue($res->isSucceed());
  359. }
  360. catch (MnsException $e)
  361. {
  362. $this->assertTrue(FALSE, $e);
  363. }
  364. try
  365. {
  366. $res = $topic->getSubscriptionAttribute($subscriptionName);
  367. $this->assertTrue($res->isSucceed());
  368. $this->assertEquals($res->getSubscriptionAttributes()->getStrategy(), $strategy);
  369. }
  370. catch (MnsException $e)
  371. {
  372. $this->assertTrue(FALSE, $e);
  373. }
  374. $topic->unsubscribe($subscriptionName);
  375. try
  376. {
  377. $res = $topic->getSubscriptionAttribute($subscriptionName);
  378. $this->assertTrue(False, "Should throw SubscriptionNotExistException");
  379. }
  380. catch (MnsException $e)
  381. {
  382. $this->assertEquals($e->getMnsErrorCode(), Constants::SUBSCRIPTION_NOT_EXIST);
  383. }
  384. try
  385. {
  386. $res = $topic->setSubscriptionAttribute($attributes);
  387. $this->assertTrue(False, "Should throw SubscriptionNotExistException");
  388. }
  389. catch (MnsException $e)
  390. {
  391. $this->assertEquals($e->getMnsErrorCode(), Constants::SUBSCRIPTION_NOT_EXIST);
  392. }
  393. }
  394. public function testListSubscriptions()
  395. {
  396. $topicName = "testListSubscriptionsTopic" . uniqid();
  397. $subscriptionNamePrefix = uniqid();
  398. $subscriptionName1 = $subscriptionNamePrefix . "testListTopic1";
  399. $subscriptionName2 = $subscriptionNamePrefix . "testListTopic2";
  400. // 1. create Topic and Subscriptions
  401. $topic = $this->prepareTopic($topicName);
  402. $this->prepareSubscription($topic, $subscriptionName1);
  403. $this->prepareSubscription($topic, $subscriptionName2);
  404. // 2. list subscriptions
  405. $subscriptionName1Found = FALSE;
  406. $subscriptionName2Found = FALSE;
  407. $count = 0;
  408. $marker = '';
  409. while ($count < 2) {
  410. try
  411. {
  412. $res = $topic->listSubscription(1, $subscriptionNamePrefix, $marker);
  413. $this->assertTrue($res->isSucceed());
  414. $subscriptionNames = $res->getSubscriptionNames();
  415. foreach ($subscriptionNames as $subscriptionName)
  416. {
  417. if ($subscriptionName == $subscriptionName1)
  418. {
  419. $subscriptionName1Found = TRUE;
  420. }
  421. elseif ($subscriptionName == $subscriptionName2)
  422. {
  423. $subscriptionName2Found = TRUE;
  424. }
  425. else
  426. {
  427. $this->assertTrue(FALSE, $subscriptionName . " Should not be here.");
  428. }
  429. }
  430. if ($count > 0)
  431. {
  432. $this->assertTrue($res->isFinished(), implode(", ", $subscriptionNames));
  433. }
  434. $marker = $res->getNextMarker();
  435. }
  436. catch (MnsException $e)
  437. {
  438. $this->assertTrue(FALSE, $e);
  439. }
  440. $count += 1;
  441. }
  442. $this->assertTrue($subscriptionName1Found, $subscriptionName1 . " Not Found!");
  443. $this->assertTrue($subscriptionName2Found, $subscriptionName2 . " Not Found!");
  444. }
  445. }
  446. ?>