Keine Beschreibung

ClientTest.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php
  2. require_once(dirname(dirname(__FILE__)).'/mns-autoloader.php');
  3. use AliyunMNS\Client;
  4. use AliyunMNS\Constants;
  5. use AliyunMNS\AsyncCallback;
  6. use AliyunMNS\Model\QueueAttributes;
  7. use AliyunMNS\Model\TopicAttributes;
  8. use AliyunMNS\Model\AccountAttributes;
  9. use AliyunMNS\Exception\MnsException;
  10. use AliyunMNS\Requests\CreateQueueRequest;
  11. use AliyunMNS\Requests\CreateTopicRequest;
  12. use AliyunMNS\Requests\ListQueueRequest;
  13. use AliyunMNS\Requests\ListTopicRequest;
  14. use AliyunMNS\Requests\SetAccountAttributesRequest;
  15. use AliyunMNS\Requests\GetAccountAttributesRequest;
  16. class ClientTest extends \PHPUnit_Framework_TestCase
  17. {
  18. private $accessId;
  19. private $accessKey;
  20. private $endPoint;
  21. private $client;
  22. private $queueToDelete;
  23. private $topicToDelete;
  24. public function setUp()
  25. {
  26. $ini_array = parse_ini_file(__DIR__ . "/aliyun-mns.ini");
  27. $this->endPoint = $ini_array["endpoint"];
  28. $this->accessId = $ini_array["accessid"];
  29. $this->accessKey = $ini_array["accesskey"];
  30. $this->queueToDelete = array();
  31. $this->topicToDelete = array();
  32. $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
  33. }
  34. public function tearDown()
  35. {
  36. foreach ($this->queueToDelete as $queueName)
  37. {
  38. try {
  39. $this->client->deleteQueue($queueName);
  40. } catch (\Exception $e) {
  41. }
  42. }
  43. foreach ($this->topicToDelete as $topicName)
  44. {
  45. try {
  46. $this->client->deleteTopic($topicName);
  47. } catch (\Exception $e) {
  48. }
  49. }
  50. }
  51. public function testAccountAttributes()
  52. {
  53. try
  54. {
  55. $attributes = new AccountAttributes;
  56. $attributes->setLoggingBucket("Test");
  57. $this->client->setAccountAttributes($attributes);
  58. $res = $this->client->getAccountAttributes();
  59. $this->assertTrue($res->isSucceed());
  60. $this->assertEquals("Test", $res->getAccountAttributes()->getLoggingBucket());
  61. $attributes = new AccountAttributes;
  62. $this->client->setAccountAttributes($attributes);
  63. $res = $this->client->getAccountAttributes();
  64. $this->assertTrue($res->isSucceed());
  65. $this->assertEquals("Test", $res->getAccountAttributes()->getLoggingBucket());
  66. $attributes = new AccountAttributes;
  67. $attributes->setLoggingBucket("");
  68. $this->client->setAccountAttributes($attributes);
  69. $res = $this->client->getAccountAttributes();
  70. $this->assertTrue($res->isSucceed());
  71. $this->assertEquals("", $res->getAccountAttributes()->getLoggingBucket());
  72. }
  73. catch (MnsException $e)
  74. {
  75. $this->assertEquals($e->getMnsErrorCode(), Constants::INVALID_ARGUMENT);
  76. }
  77. }
  78. public function testCreateQueueAsync()
  79. {
  80. $queueName = "testCreateQueueAsync";
  81. $request = new CreateQueueRequest($queueName);
  82. $this->queueToDelete[] = $queueName;
  83. // Async Call with callback
  84. try
  85. {
  86. $res = $this->client->createQueueAsync($request,
  87. new AsyncCallback(
  88. function($response) {
  89. $this->assertTrue($response->isSucceed());
  90. },
  91. function($e) {
  92. $this->assertTrue(FALSE, $e);
  93. }
  94. )
  95. );
  96. $res = $res->wait();
  97. $this->assertTrue($res->isSucceed());
  98. }
  99. catch (MnsException $e)
  100. {
  101. $this->assertTrue(FALSE, $e);
  102. }
  103. // Async call without callback
  104. try
  105. {
  106. $res = $this->client->createQueueAsync($request);
  107. $res = $res->wait();
  108. $this->assertTrue($res->isSucceed());
  109. }
  110. catch (MnsException $e)
  111. {
  112. $this->assertTrue(FALSE, $e);
  113. }
  114. }
  115. public function testCreateQueueSync()
  116. {
  117. $queueName = "testCreateQueueSync";
  118. // 1. create queue with InvalidArgument
  119. $attributes = new QueueAttributes;
  120. $attributes->setPollingWaitSeconds(60);
  121. $request = new CreateQueueRequest($queueName, $attributes);
  122. try
  123. {
  124. $res = $this->client->createQueue($request);
  125. $this->assertTrue(FALSE, "Should throw InvalidArgumentException");
  126. }
  127. catch (MnsException $e)
  128. {
  129. $this->assertEquals($e->getMnsErrorCode(), Constants::INVALID_ARGUMENT);
  130. }
  131. // 2. create queue
  132. $request = new CreateQueueRequest($queueName);
  133. $this->queueToDelete[] = $queueName;
  134. try
  135. {
  136. $res = $this->client->createQueue($request);
  137. $this->assertTrue($res->isSucceed());
  138. }
  139. catch (MnsException $e)
  140. {
  141. $this->assertTrue(FALSE, $e);
  142. }
  143. // 3. create queue with same attributes
  144. $request = new CreateQueueRequest($queueName);
  145. $this->queueToDelete[] = $queueName;
  146. try
  147. {
  148. $res = $this->client->createQueue($request);
  149. $this->assertTrue($res->isSucceed());
  150. }
  151. catch (MnsException $e)
  152. {
  153. $this->assertTrue(FALSE, $e);
  154. }
  155. // 4. create same queue with different attributes
  156. $attributes = new QueueAttributes;
  157. $attributes->setPollingWaitSeconds(20);
  158. $request = new CreateQueueRequest($queueName, $attributes);
  159. try
  160. {
  161. $res = $this->client->createQueue($request);
  162. $this->assertTrue(FALSE, "Should throw QueueAlreadyExistException");
  163. }
  164. catch (MnsException $e)
  165. {
  166. $this->assertEquals($e->getMnsErrorCode(), Constants::QUEUE_ALREADY_EXIST);
  167. }
  168. }
  169. public function testListQueue()
  170. {
  171. $queueNamePrefix = uniqid();
  172. $queueName1 = $queueNamePrefix . "testListQueue1";
  173. $queueName2 = $queueNamePrefix . "testListQueue2";
  174. // 1. create queue
  175. $request = new CreateQueueRequest($queueName1);
  176. $this->queueToDelete[] = $queueName1;
  177. try
  178. {
  179. $res = $this->client->createQueue($request);
  180. $this->assertTrue($res->isSucceed());
  181. }
  182. catch (MnsException $e)
  183. {
  184. $this->assertTrue(FALSE, $e);
  185. }
  186. $request = new CreateQueueRequest($queueName2);
  187. $this->queueToDelete[] = $queueName2;
  188. try
  189. {
  190. $res = $this->client->createQueue($request);
  191. $this->assertTrue($res->isSucceed());
  192. }
  193. catch (MnsException $e)
  194. {
  195. $this->assertTrue(FALSE, $e);
  196. }
  197. // 2. list queue
  198. $queueName1Found = FALSE;
  199. $queueName2Found = FALSE;
  200. $count = 0;
  201. $request = new ListQueueRequest(1, $queueNamePrefix);
  202. while ($count < 2) {
  203. try
  204. {
  205. $res = $this->client->listQueue($request);
  206. $this->assertTrue($res->isSucceed());
  207. $queueNames = $res->getQueueNames();
  208. foreach ($queueNames as $queueName) {
  209. if ($queueName == $queueName1) {
  210. $queueName1Found = TRUE;
  211. } elseif ($queueName == $queueName2) {
  212. $queueName2Found = TRUE;
  213. } else {
  214. $this->assertTrue(FALSE, $queueName . " Should not be here.");
  215. }
  216. }
  217. if ($count > 0) {
  218. $this->assertTrue($res->isFinished(), implode(", ", $queueNames));
  219. }
  220. $request->setMarker($res->getNextMarker());
  221. }
  222. catch (MnsException $e)
  223. {
  224. $this->assertTrue(FALSE, $e);
  225. }
  226. $count += 1;
  227. }
  228. $this->assertTrue($queueName1Found, $queueName1 . " Not Found!");
  229. $this->assertTrue($queueName2Found, $queueName2 . " Not Found!");
  230. }
  231. public function testListQueueAsync()
  232. {
  233. $queueNamePrefix = uniqid();
  234. $queueName1 = $queueNamePrefix . "testListQueue1";
  235. $queueName2 = $queueNamePrefix . "testListQueue2";
  236. // 1. create queue
  237. $request = new CreateQueueRequest($queueName1);
  238. $this->queueToDelete[] = $queueName1;
  239. try
  240. {
  241. $res = $this->client->createQueue($request);
  242. $this->assertTrue($res->isSucceed());
  243. }
  244. catch (MnsException $e)
  245. {
  246. $this->assertTrue(FALSE, $e);
  247. }
  248. $request = new CreateQueueRequest($queueName2);
  249. $this->queueToDelete[] = $queueName2;
  250. try
  251. {
  252. $res = $this->client->createQueue($request);
  253. $this->assertTrue($res->isSucceed());
  254. }
  255. catch (MnsException $e)
  256. {
  257. $this->assertTrue(FALSE, $e);
  258. }
  259. // 2. list queue
  260. $queueName1Found = FALSE;
  261. $queueName2Found = FALSE;
  262. $count = 0;
  263. $request = new ListQueueRequest(1, $queueNamePrefix);
  264. while ($count < 2) {
  265. try
  266. {
  267. $res = $this->client->listQueueAsync($request,
  268. new AsyncCallback(
  269. function($response) use ($count, &$request, $queueName1, $queueName2, &$queueName1Found, &$queueName2Found) {
  270. $this->assertTrue($response->isSucceed());
  271. $queueNames = $response->getQueueNames();
  272. foreach ($queueNames as $queueName) {
  273. if ($queueName == $queueName1) {
  274. $queueName1Found = TRUE;
  275. } elseif ($queueName == $queueName2) {
  276. $queueName2Found = TRUE;
  277. } else {
  278. $this->assertTrue(FALSE, $queueName . " Should not be here.");
  279. }
  280. }
  281. if ($count > 0) {
  282. $this->assertTrue($response->isFinished(), implode(", ", $queueNames));
  283. }
  284. $request->setMarker($response->getNextMarker());
  285. },
  286. function($e) {
  287. $this->assertTrue(FALSE, $e);
  288. }
  289. )
  290. );
  291. $res = $res->wait();
  292. $this->assertTrue($res->isSucceed());
  293. }
  294. catch (MnsException $e)
  295. {
  296. $this->assertTrue(FALSE, $e);
  297. }
  298. $count += 1;
  299. }
  300. $this->assertTrue($queueName1Found, $queueName1 . " Not Found!");
  301. $this->assertTrue($queueName2Found, $queueName2 . " Not Found!");
  302. }
  303. public function testDeleteQueue()
  304. {
  305. $queueName = "testDeleteQueue";
  306. // 1. create queue
  307. $request = new CreateQueueRequest($queueName);
  308. $this->queueToDelete[] = $queueName;
  309. try
  310. {
  311. $res = $this->client->createQueue($request);
  312. $this->assertTrue($res->isSucceed());
  313. }
  314. catch (MnsException $e)
  315. {
  316. $this->assertTrue(FALSE, $e);
  317. }
  318. // 2. delete queue
  319. try
  320. {
  321. $res = $this->client->deleteQueue($queueName);
  322. $this->assertTrue($res->isSucceed());
  323. }
  324. catch (MnsException $e)
  325. {
  326. $this->assertTrue(FALSE, $e);
  327. }
  328. }
  329. public function testDeleteQueueAsync()
  330. {
  331. $queueName = "testDeleteQueueAsync";
  332. // 1. create queue
  333. $request = new CreateQueueRequest($queueName);
  334. $this->queueToDelete[] = $queueName;
  335. try
  336. {
  337. $res = $this->client->createQueue($request);
  338. $this->assertTrue($res->isSucceed());
  339. }
  340. catch (MnsException $e)
  341. {
  342. $this->assertTrue(FALSE, $e);
  343. }
  344. // 2. delete Queue
  345. try
  346. {
  347. $res = $this->client->deleteQueueAsync($queueName);
  348. $res = $res->wait();
  349. $this->assertTrue($res->isSucceed());
  350. }
  351. catch (MnsException $e)
  352. {
  353. $this->assertTrue(FALSE, $e);
  354. }
  355. }
  356. public function testCreateTopicSync()
  357. {
  358. $topicName = "testCreateTopicSync";
  359. // 1. create topic with InvalidArgument
  360. $attributes = new TopicAttributes;
  361. $attributes->setMaximumMessageSize(65 * 1024);
  362. $request = new CreateTopicRequest($topicName, $attributes);
  363. try
  364. {
  365. $res = $this->client->createTopic($request);
  366. $this->assertTrue(FALSE, "Should throw InvalidArgumentException");
  367. }
  368. catch (MnsException $e)
  369. {
  370. $this->assertEquals($e->getMnsErrorCode(), Constants::INVALID_ARGUMENT);
  371. }
  372. // 2. create topic
  373. $request = new CreateTopicRequest($topicName);
  374. $this->topicToDelete[] = $topicName;
  375. try
  376. {
  377. $res = $this->client->createTopic($request);
  378. $this->assertTrue($res->isSucceed());
  379. }
  380. catch (MnsException $e)
  381. {
  382. $this->assertTrue(FALSE, $e);
  383. }
  384. // 3. create topic with same attributes
  385. $request = new CreateTopicRequest($topicName);
  386. $this->topicToDelete[] = $topicName;
  387. try
  388. {
  389. $res = $this->client->createTopic($request);
  390. $this->assertTrue($res->isSucceed());
  391. }
  392. catch (MnsException $e)
  393. {
  394. $this->assertTrue(FALSE, $e);
  395. }
  396. // 4. create same topic with different attributes
  397. $attributes = new TopicAttributes;
  398. $attributes->setMaximumMessageSize(10 * 1024);
  399. $request = new CreateTopicRequest($topicName, $attributes);
  400. try
  401. {
  402. $res = $this->client->createTopic($request);
  403. $this->assertTrue(FALSE, "Should throw TopicAlreadyExistException");
  404. }
  405. catch (MnsException $e)
  406. {
  407. $this->assertEquals($e->getMnsErrorCode(), Constants::TOPIC_ALREADY_EXIST);
  408. }
  409. }
  410. public function testListTopic()
  411. {
  412. $topicNamePrefix = uniqid();
  413. $topicName1 = $topicNamePrefix . "testListTopic1";
  414. $topicName2 = $topicNamePrefix . "testListTopic2";
  415. // 1. create Topic
  416. $request = new CreateTopicRequest($topicName1);
  417. $this->topicToDelete[] = $topicName1;
  418. try
  419. {
  420. $res = $this->client->createTopic($request);
  421. $this->assertTrue($res->isSucceed());
  422. }
  423. catch (MnsException $e)
  424. {
  425. $this->assertTrue(FALSE, $e);
  426. }
  427. $request = new CreateTopicRequest($topicName2);
  428. $this->topicToDelete[] = $topicName2;
  429. try
  430. {
  431. $res = $this->client->createTopic($request);
  432. $this->assertTrue($res->isSucceed());
  433. }
  434. catch (MnsException $e)
  435. {
  436. $this->assertTrue(FALSE, $e);
  437. }
  438. // 2. list Topic
  439. $topicName1Found = FALSE;
  440. $topicName2Found = FALSE;
  441. $count = 0;
  442. $request = new ListTopicRequest(1, $topicNamePrefix);
  443. while ($count < 2) {
  444. try
  445. {
  446. $res = $this->client->listTopic($request);
  447. $this->assertTrue($res->isSucceed());
  448. $topicNames = $res->getTopicNames();
  449. foreach ($topicNames as $topicName) {
  450. if ($topicName == $topicName1) {
  451. $topicName1Found = TRUE;
  452. } elseif ($topicName == $topicName2) {
  453. $topicName2Found = TRUE;
  454. } else {
  455. $this->assertTrue(FALSE, $topicName . " Should not be here.");
  456. }
  457. }
  458. if ($count > 0) {
  459. $this->assertTrue($res->isFinished(), implode(", ", $topicNames));
  460. }
  461. $request->setMarker($res->getNextMarker());
  462. }
  463. catch (MnsException $e)
  464. {
  465. $this->assertTrue(FALSE, $e);
  466. }
  467. $count += 1;
  468. }
  469. $this->assertTrue($topicName1Found, $topicName1 . " Not Found!");
  470. $this->assertTrue($topicName2Found, $topicName2 . " Not Found!");
  471. }
  472. }
  473. ?>