/test/Services_Buzzurl/t/Services_BuzzurlTest.php

https://github.com/GunioRobot/php-services_buzzurl · PHP · 300 lines · 249 code · 26 blank · 25 comment · 0 complexity · b26616b566b66d2721eaace442914a8a MD5 · raw file

  1. <?php
  2. /**
  3. * unit test for Services_Buzzurl
  4. *
  5. * @author tell-k <ffk2005@gmail.com>
  6. * @since PHP5.2
  7. * @version $Id$
  8. */
  9. require_once dirname(__FILE__) . '/../lib/t.php';
  10. $id = 'tell-k'; //set your buzzurl_id
  11. //ex) http://buzzurl.jp/user/[youre buzzurl_id]
  12. $t = new lime_test(68, new lime_output_color());
  13. require_once 'Services/Buzzurl.php';
  14. $t->diag('getInstance');
  15. $api = Services_Buzzurl::getInstance();
  16. $t->ok($api instanceof Services_Buzzurl, 'create instance is ok');
  17. $t->diag('setVersion');
  18. $validCase = array('v1');
  19. foreach ($validCase as $case) {
  20. try {
  21. $api->setVersion($case);
  22. $t->pass('set version is ok');
  23. } catch (Exception $e) {
  24. $t->fail('set version not ok');
  25. }
  26. }
  27. $errCase = array(null, 'hoge');
  28. foreach ($errCase as $case) {
  29. try {
  30. $api->setVersion($case);
  31. $t->fail('set version exception test not ok');
  32. } catch (InvalidArgumentException $e) {
  33. $t->pass('set version exception test is ok');
  34. }
  35. }
  36. $t->diag('setFormat');
  37. $validCase = array('array', 'json');
  38. foreach ($validCase as $case) {
  39. try {
  40. $api->setFormat($case);
  41. $t->pass('set format is ok');
  42. } catch (Exception $e) {
  43. $t->fail('set format not ok');
  44. }
  45. }
  46. $errCase = array(null, 'hoge');
  47. foreach ($errCase as $case) {
  48. try {
  49. $api->setFormat($case);
  50. $t->fail('set format exception test not ok');
  51. } catch (InvalidArgumentException $e) {
  52. $t->pass('set foramat exception test is ok');
  53. }
  54. }
  55. $t->diag('getAirticles');
  56. $api->setFormat('array');
  57. $t->ok(is_array($api->getArticles($id)), 'get articles is ok');
  58. $api->setFormat('json');
  59. $t->ok(is_string($api->getArticles($id, null)), 'get articles(json) is ok');
  60. try {
  61. $api->getArticles(null);
  62. $t->fail('get articles exception test not ok');
  63. } catch (InvalidArgumentException $e) {
  64. $t->pass('get articles exception test is ok');
  65. }
  66. //please set search keywords
  67. $keywords = 'php';
  68. $api->setFormat('array');
  69. $t->ok(is_array($api->getArticles($id, 'php')), 'get articles by keywords is ok');
  70. $api->setFormat('json');
  71. $t->ok(is_string($api->getArticles($id, 'php')), 'get articles by keywords(json) is ok');
  72. $t->diag('getRecentAirticles');
  73. $api->setFormat('array');
  74. $t->ok(is_array($api->getRecentArticles()), 'get recent articles is ok');
  75. $t->ok(is_array($api->getRecentArticles(null, null, null)), 'get recent articles is ok');
  76. $t->ok(is_array($api->getRecentArticles(1)), 'get recent articles is ok');
  77. $t->ok(is_array($api->getRecentArticles(1, 1)), 'get recent articles is ok');
  78. $t->ok(is_array($api->getRecentArticles(1, 1, 1)), 'get recent articles is ok');
  79. $t->ok(is_array($api->getRecentArticles(2, 2, 2)), 'get recent articles is ok');
  80. $t->ok(is_array($api->getRecentArticles(2, 3, 3)), 'get recent articles is ok');
  81. $t->is(count($api->getRecentArticles(10)), 10, 'get recent articles count is ok');
  82. $api->setFormat('json');
  83. $t->ok(is_string($api->getRecentArticles()), 'get recent articles(json) is ok');
  84. $t->ok(is_string($api->getRecentArticles(null, null, null)), 'get recent articles(json) is ok');
  85. $t->ok(is_string($api->getRecentArticles(1)), 'get recent articles(json) is ok');
  86. $t->ok(is_string($api->getRecentArticles(1, 1)), 'get recent articles(json) is ok');
  87. $t->ok(is_string($api->getRecentArticles(1, 1, 1)), 'get recent articles(json) is ok');
  88. $errCase = array(
  89. array('num' => -1, 'of' => -1, 'threshold' => -1),
  90. array('num' => 1, 'of' => -1, 'threshold' => -1),
  91. array('num' => -1, 'of' => 1, 'threshold' => -1),
  92. array('num' => -1, 'of' => -1, 'threshold' => 1),
  93. array('num' => 'hoge', 'of' => 1, 'threshold' => 1),
  94. array('num' => 1.1, 'of' => 1, 'threshold' => 1),
  95. array('num' => '01', 'of' => 1, 'threshold' => 1),
  96. array('num' => '1a', 'of' => 1, 'threshold' => 1),
  97. );
  98. foreach ($errCase as $v) {
  99. try {
  100. $api->getRecentArticles($v['num'], $v['of'], $v['threshold']);
  101. $t->fail('get recent articles exception test not ok');
  102. } catch (InvalidArgumentException $e) {
  103. $t->pass('get recent articles exception test is ok');
  104. }
  105. }
  106. $t->diag('getReaders');
  107. $api->setFormat('array');
  108. $t->ok(is_array($api->getReaders($id)), 'get readers is ok');
  109. $api->setFormat('json');
  110. $t->ok(is_string($api->getReaders($id)), 'get readers(json) is ok');
  111. try {
  112. $api->getReaders(null);
  113. $t->fail('get readers exception test not ok');
  114. } catch (InvalidArgumentException $e) {
  115. $t->pass('get readers exception test is ok');
  116. }
  117. $t->diag('getFavaites');
  118. $api->setFormat('array');
  119. $t->ok(is_array($api->getFavarites($id)), 'get favorites is ok');
  120. $api->setFormat('json');
  121. $t->ok(is_string($api->getFavarites($id)), 'get favorites(json) is ok');
  122. try {
  123. $api->getFavarites(null);
  124. $t->fail('get favorites exception test not ok');
  125. } catch (InvalidArgumentException $e) {
  126. $t->pass('get favorites exception test is ok');
  127. }
  128. $t->diag('getPostsInfo');
  129. $url = 'http://ecnavi.jp/';
  130. $api->setFormat('array');
  131. $t->ok(is_array($api->getPostsInfo($url)), 'get posts info is ok');
  132. $api->setFormat('json');
  133. $t->ok(is_string($api->getPostsInfo($url)), 'get posts(json) info is ok');
  134. try {
  135. $api->getPostsInfo(null);
  136. $t->fail('get posts info exception test not ok');
  137. } catch (InvalidArgumentException $e) {
  138. $t->pass('get posts info exception test is ok');
  139. }
  140. $t->diag('getCounter');
  141. $url = 'http://ecnavi.jp/';
  142. $api->setFormat('array');
  143. $t->ok(is_array($api->getCounter($url)), 'get counter by url string is ok');
  144. $api->setFormat('json');
  145. $t->ok(is_string($api->getCounter($url)), 'get counter by url string(json) is ok');
  146. $url = array('http://ecnavi.jp/', 'http://buzzurl.jp/');
  147. $api->setFormat('array');
  148. $t->ok(is_array($api->getCounter($url)), 'get counter by url array is ok');
  149. $api->setFormat('json');
  150. $t->ok(is_string($api->getCounter($url)), 'get counter by url array(json) is ok');
  151. try {
  152. $api->getCounter(null);
  153. $t->fail('get counter exception test not ok');
  154. } catch (InvalidArgumentException $e) {
  155. $t->pass('get counter exception test is ok');
  156. }
  157. $t->diag('getCounterImgUrl');
  158. $url = 'http://buzzurl.jp/';
  159. $expected = 'http://api.buzzurl.jp/api/counter/v1/image/?url=http%3A%2F%2Fbuzzurl.jp%2F';
  160. $t->is($api->getCounterImgUrl($url), $expected, 'get counter is ok');
  161. try {
  162. $api->getCounterImgUrl(null);
  163. $t->fail('get counter image url exception test not ok');
  164. } catch (InvalidArgumentException $e) {
  165. $t->pass('get counter image url exception test is ok');
  166. }
  167. $t->diag('makePostData');
  168. $case = array(
  169. array(
  170. 'args' => array('url' => 'http://test.co.jp'),
  171. 'expected' => 'url=http://test.co.jp',
  172. ),
  173. array(
  174. 'args' => array('url' => 'http://test.co.jp', 'keyword' => 'test'),
  175. 'expected' => 'url=http://test.co.jp&keyword=test',
  176. ),
  177. array(
  178. 'args' => array('url' => 'http://test.co.jp', 'keyword' => array('test', 'test2')),
  179. 'expected' => 'url=http://test.co.jp&keyword=test&keyword=test2',
  180. ),
  181. array(
  182. 'args' => array('hoge' => 'http://test.co.jp', 'keyword' => array('test', 'test2')),
  183. 'expected' => 'keyword=test&keyword=test2',
  184. ),
  185. array(
  186. 'args' => array('keyword' => array('test', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8', 'test9')),
  187. 'expected' => 'keyword=test&keyword=test2&keyword=test3&keyword=test4&keyword=test5&keyword=test6&keyword=test7&keyword=test8',
  188. ),
  189. array(
  190. 'args' => array('reply' => '1'),
  191. 'expected' => 'reply=1',
  192. ),
  193. array(
  194. 'args' => array('reply' => '0'),
  195. 'expected' => 'reply=0',
  196. ),
  197. array(
  198. 'args' => array('reply' => '-1'),
  199. 'expected' => '',
  200. ),
  201. array(
  202. 'args' => array('reply' => '0.1'),
  203. 'expected' => '',
  204. ),
  205. array(
  206. 'args' => array('reply' => '1.1'),
  207. 'expected' => '',
  208. ),
  209. array(
  210. 'args' => array('reply' => 'hoge'),
  211. 'expected' => '',
  212. ),
  213. array(
  214. 'args' => array('access' => 'anonymous'),
  215. 'expected' => 'access=anonymous',
  216. ),
  217. array(
  218. 'args' => array('access' => 'private'),
  219. 'expected' => 'access=private',
  220. ),
  221. array(
  222. 'args' => array('access' => 'unknowon'),
  223. 'expected' => '',
  224. ),
  225. );
  226. foreach($case as $v) {
  227. $result = $api->makePostData($v['args']);
  228. $t->is($result, $v['expected'], 'make post data test is ok');
  229. }
  230. //$t->diag('add');
  231. //
  232. // 実際に投稿のテストをする時 addのテストのコメント合うとは外して行ってください。
  233. //
  234. //$email = ''; //<= your buzzurl login id(email)
  235. //$passwd = ''; //<= your buzzurl password
  236. //
  237. //$case = array(
  238. // array('url' => 'http://buzzurl.jp'),
  239. // array('url' => 'http://buzzurl.jp', 'title' => 'Buzzurl', 'comment' => 'buzzurl 投稿テスト', 'keyword' => array('SBM', 'social'), 'reply' => '0', 'access' => 'anonymous'),
  240. // array('url' => 'http://buzzurl.jp', 'title' => 'Buzzurl', 'comment' => 'buzzurl 投稿テスト', 'keyword' => 'tagtest', 'reply' => '1', 'access' => 'private'),
  241. // );
  242. //foreach($case as $args) {
  243. // $result = $api->add($email, $passwd, $args);
  244. // $t->is($result, true, 'add test is ok');
  245. //}
  246. $t->diag('add - exception test');
  247. try {
  248. $args = null;
  249. $api->add($email, $passwd, $args);
  250. $t->fail('add exception test is fail');
  251. } catch (Exception $e) {
  252. $t->pass('add exception test is ok');
  253. }
  254. try {
  255. $args = array();
  256. $api->add($email, $passwd, $args);
  257. $t->fail('add exception test is fail');
  258. } catch (Exception $e) {
  259. $t->pass('add exception test is ok');
  260. }
  261. try {
  262. $args = array('url' => 'http://buzzurl.jp');
  263. $api->add(null, $passwd, $args);
  264. $t->fail('add exception test is fail');
  265. } catch (Exception $e) {
  266. $t->pass('add exception test is ok');
  267. }
  268. try {
  269. $args = array('url' => 'http://buzzurl.jp');
  270. $api->add($email, null, $args);
  271. $t->fail('add exception test is fail');
  272. } catch (Exception $e) {
  273. $t->pass('add exception test is ok');
  274. }