SimpyTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Simpy
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * @see Zend_Service_Simpy
  28. */
  29. require_once 'Zend/Service/Simpy.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service_Simpy
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Service_SimpyTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Simpy service consumer
  41. *
  42. * @var Zend_Service_Simpy
  43. */
  44. protected $_simpy;
  45. /**
  46. * Sample link data
  47. *
  48. * @var array
  49. */
  50. protected $_link = array(
  51. 'title' => 'Zend Framework',
  52. 'href' => 'http://framework.zend.com',
  53. 'accessType' => 'public',
  54. 'tags' => array('zend', 'framework', 'php'),
  55. 'urlNickname' => 'Zend Framework web site',
  56. 'note' => 'This web site rules!'
  57. );
  58. /**
  59. * Sample note data
  60. *
  61. * @var array
  62. */
  63. protected $_note = array(
  64. 'title' => 'Test Note',
  65. 'tags' => array('test'),
  66. 'description' => 'This is a test note.'
  67. );
  68. public function setUp()
  69. {
  70. sleep(5);
  71. $this->_simpy = new Zend_Service_Simpy('syapizend', 'mgt37ge');
  72. /**
  73. * Until Simpy includes time zone support, timestamp values must be
  74. * tested against the time zone in use by Simpy's servers
  75. */
  76. date_default_timezone_set('America/New_York');
  77. }
  78. public function testException()
  79. {
  80. try {
  81. $this->_simpy->deleteNote(null);
  82. $this->fail('Exception not thrown');
  83. } catch (Zend_Service_Exception $e) {
  84. // success
  85. }
  86. }
  87. public function testSaveLink()
  88. {
  89. try {
  90. extract($this->_link);
  91. /**
  92. * Delete the link if it already exists and bypass the exception
  93. * that would be thrown as a result
  94. */
  95. try {
  96. $this->_simpy->deleteLink($href);
  97. } catch (Zend_Service_Exception $e) {
  98. // ignore exception
  99. }
  100. /**
  101. * @see Zend_Service_Simpy_Link
  102. */
  103. require_once 'Zend/Service/Simpy/Link.php';
  104. $this->_simpy->saveLink(
  105. $title,
  106. $href,
  107. Zend_Service_Simpy_Link::ACCESSTYPE_PUBLIC,
  108. $tags,
  109. $urlNickname,
  110. $note
  111. );
  112. } catch (Zend_Service_Exception $e) {
  113. $this->fail('Could not save link: ' . $e->getMessage());
  114. }
  115. }
  116. public function testGetLinks()
  117. {
  118. $linkSet = $this->_simpy->getLinks();
  119. $this->assertEquals(
  120. $linkSet->getLength(),
  121. 1,
  122. 'Link set does not have expected size'
  123. );
  124. $link = $linkSet->getIterator()->current();
  125. $date = date('Y-m-d');
  126. extract($this->_link);
  127. $this->assertEquals(
  128. $link->getAccessType(),
  129. $accessType,
  130. 'Access type does not match'
  131. );
  132. $this->assertEquals(
  133. $link->getUrl(),
  134. $href,
  135. 'URL does not match'
  136. );
  137. $this->assertEquals(
  138. substr($link->getModDate(), 0, 10),
  139. $date,
  140. 'Mod date does not match'
  141. );
  142. $this->assertEquals(
  143. substr($link->getAddDate(), 0, 10),
  144. $date,
  145. 'Add date does not match'
  146. );
  147. $this->assertEquals(
  148. $link->getTitle(),
  149. $title,
  150. 'Title does not match'
  151. );
  152. $this->assertEquals(
  153. $link->getNickname(),
  154. $urlNickname,
  155. 'Nickname does not match'
  156. );
  157. $this->assertEquals(
  158. $link->getTags(),
  159. $tags,
  160. 'Tags do not match'
  161. );
  162. $this->assertEquals(
  163. $link->getNote(),
  164. $note,
  165. 'Note does not match'
  166. );
  167. }
  168. public function testLinkQuery()
  169. {
  170. $date = date('Y-m-d');
  171. /**
  172. * @see Zend_Service_Simpy_LinkQuery
  173. */
  174. require_once 'Zend/Service/Simpy/LinkQuery.php';
  175. $linkQuery = new Zend_Service_Simpy_LinkQuery;
  176. $linkQuery->setQueryString($this->_link['title']);
  177. $linkQuery->setBeforeDate($date);
  178. $this->assertNull(
  179. $linkQuery->getDate(),
  180. 'Date has been initialized'
  181. );
  182. $linkQuery->setAfterDate($date);
  183. $this->assertNull(
  184. $linkQuery->getDate(),
  185. 'Date has been initialized'
  186. );
  187. $linkQuery->setDate($date);
  188. $this->assertNull(
  189. $linkQuery->getBeforeDate(),
  190. 'Before date has retained its value'
  191. );
  192. $this->assertNull(
  193. $linkQuery->getAfterDate(),
  194. 'After date has retained its value'
  195. );
  196. $linkQuery->setLimit(1);
  197. $this->assertEquals(
  198. $linkQuery->getLimit(),
  199. 1,
  200. 'Limit was not set'
  201. );
  202. $linkQuery->setLimit(array());
  203. $this->assertNull(
  204. $linkQuery->getLimit(),
  205. 'Invalid limit value was accepted'
  206. );
  207. $linkSet = $this->_simpy->getLinks($linkQuery);
  208. $this->assertEquals(
  209. $linkSet->getLength(),
  210. 1,
  211. 'Link set does not have the expected size'
  212. );
  213. }
  214. public function testGetTags()
  215. {
  216. $tagSet = $this->_simpy->getTags();
  217. $this->assertEquals(
  218. $tagSet->getLength(),
  219. count($this->_link['tags']),
  220. 'Tag set does not have the expected size'
  221. );
  222. foreach ($tagSet as $tag) {
  223. $this->assertContains(
  224. $tag->getTag(),
  225. $this->_link['tags'],
  226. 'Tag ' . $tag->getTag() . ' does not exist'
  227. );
  228. $this->assertEquals(
  229. $tag->getCount(),
  230. 1,
  231. 'Tag count does not match'
  232. );
  233. }
  234. }
  235. public function testRenameTag()
  236. {
  237. $fromTag = 'framework';
  238. $toTag = 'frameworks';
  239. $tagsArray = $this->_getTagsArray();
  240. $this->assertContains(
  241. $fromTag,
  242. $tagsArray,
  243. 'Source tag was not found'
  244. );
  245. $this->assertNotContains(
  246. $toTag,
  247. $tagsArray,
  248. 'Destination tag already exists'
  249. );
  250. $this->_simpy->renameTag($fromTag, $toTag);
  251. $tagsArray = $this->_getTagsArray();
  252. $this->assertContains(
  253. $toTag,
  254. $tagsArray,
  255. 'Destination tag was not found'
  256. );
  257. }
  258. public function testSplitTag()
  259. {
  260. $fromTag = 'frameworks';
  261. $toTag1 = 'framework';
  262. $toTag2 = 'frameworks';
  263. $tagsArray = $this->_getTagsArray();
  264. $this->assertContains(
  265. $fromTag,
  266. $tagsArray,
  267. 'Source tag was not found'
  268. );
  269. $this->_simpy->splitTag($fromTag, $toTag1, $toTag2);
  270. $tagsArray = $this->_getTagsArray();
  271. $this->assertContains(
  272. $toTag1,
  273. $tagsArray,
  274. 'First destination tag was not found'
  275. );
  276. $this->assertContains(
  277. $toTag2,
  278. $tagsArray,
  279. 'Second destination tag was not found'
  280. );
  281. }
  282. public function testMergeTags()
  283. {
  284. $fromTag1 = 'framework';
  285. $fromTag2 = 'frameworks';
  286. $toTag = 'framework';
  287. $tagsArray = $this->_getTagsArray();
  288. $this->assertContains(
  289. $fromTag1,
  290. $tagsArray,
  291. 'First source tag was not found'
  292. );
  293. $this->assertContains(
  294. $fromTag2,
  295. $tagsArray,
  296. 'Second source tag was not found'
  297. );
  298. $this->_simpy->mergeTags($fromTag1, $fromTag2, $toTag);
  299. $tagsArray = $this->_getTagsArray();
  300. $this->assertContains(
  301. $toTag,
  302. $tagsArray,
  303. 'Destination tag was not found'
  304. );
  305. }
  306. public function testRemoveTag()
  307. {
  308. $tag = 'zend';
  309. $tagsArray = $this->_getTagsArray();
  310. $this->assertContains(
  311. $tag,
  312. $tagsArray,
  313. 'Tag to remove was not found'
  314. );
  315. $this->_simpy->removeTag($tag);
  316. $tagsArray = $this->_getTagsArray();
  317. $this->assertNotContains(
  318. $tag,
  319. $tagsArray,
  320. 'Tag was not removed'
  321. );
  322. }
  323. public function testDeleteLink()
  324. {
  325. $this->_simpy->deleteLink($this->_link['href']);
  326. $linkSet = $this->_simpy->getLinks();
  327. $this->assertEquals(
  328. $linkSet->getLength(),
  329. 0,
  330. 'Link was not deleted'
  331. );
  332. }
  333. public function testSaveNote()
  334. {
  335. try {
  336. extract($this->_note);
  337. $this->_simpy->saveNote(
  338. $title,
  339. $tags,
  340. $description
  341. );
  342. } catch (Zend_Service_Exception $e) {
  343. $this->fail('Could not save note: ' . $e->getMessage());
  344. }
  345. }
  346. public function testGetNotes()
  347. {
  348. $date = date('Y-m-d');
  349. $noteSet = $this->_simpy->getNotes();
  350. $this->assertEquals(
  351. $noteSet->getLength(),
  352. 1,
  353. 'Note set does not have the expected size'
  354. );
  355. $note = $noteSet->getIterator()->current();
  356. extract($this->_note);
  357. $this->assertEquals(
  358. $note->getAccessType(),
  359. 'private',
  360. 'Access type does not match'
  361. );
  362. $this->assertEquals(
  363. $note->getUri(),
  364. 'http://www.simpy.com/simpy/NoteDetails.do?noteId=' . $note->getId(),
  365. 'URI does not match'
  366. );
  367. $this->assertEquals(
  368. $note->getTitle(),
  369. $title,
  370. 'Title does not match'
  371. );
  372. $this->assertEquals(
  373. $note->getTags(),
  374. $tags,
  375. 'Tags do not match'
  376. );
  377. $this->assertEquals(
  378. $note->getDescription(),
  379. $description,
  380. 'Description does not match'
  381. );
  382. $this->assertEquals(
  383. $note->getAddDate(),
  384. $date,
  385. 'Add date does not match'
  386. );
  387. $this->assertEquals(
  388. $note->getModDate(),
  389. $date,
  390. 'Mod date does not match'
  391. );
  392. }
  393. public function testDeleteNote()
  394. {
  395. $noteSet = $this->_simpy->getNotes();
  396. $noteId = $noteSet->getIterator()->current()->getId();
  397. $this->_simpy->deleteNote($noteId);
  398. $noteSet = $this->_simpy->getNotes();
  399. foreach ($noteSet as $note) {
  400. $this->assertNotEquals(
  401. $note->getId(),
  402. $noteId,
  403. 'Note was not deleted'
  404. );
  405. }
  406. }
  407. public function testGetWatchlists()
  408. {
  409. $watchlistSet = $this->_simpy->getWatchlists();
  410. $watchlist = $watchlistSet->getIterator()->current();
  411. $this->assertEquals(
  412. $watchlistSet->getLength(),
  413. 1,
  414. 'Watchlist set does not have the expected size'
  415. );
  416. $this->assertNotNull(
  417. $watchlist,
  418. 'Watchlist is invalid'
  419. );
  420. }
  421. public function testGetWatchlist()
  422. {
  423. $watchlist = $this->_simpy->getWatchlist('1331');
  424. $this->assertEquals(
  425. $watchlist->getId(),
  426. '1331',
  427. 'ID does not match'
  428. );
  429. $this->assertEquals(
  430. $watchlist->getName(),
  431. 'Test Watchlist',
  432. 'Name does not match'
  433. );
  434. $this->assertEquals(
  435. $watchlist->getDescription(),
  436. 'This is a watchlist for testing purposes. Please do not remove it.',
  437. 'Description does not match'
  438. );
  439. $this->assertEquals(
  440. $watchlist->getAddDate(),
  441. 'Fri Dec 08 21:40:56 EST 2006',
  442. 'Add date does not match'
  443. );
  444. $this->assertEquals(
  445. $watchlist->getNewLinks(),
  446. 0,
  447. 'New link count does not match'
  448. );
  449. $this->assertEquals(
  450. $watchlist->getUsers(),
  451. array('otis'),
  452. 'User list does not match'
  453. );
  454. }
  455. public function testWatchlistFilters()
  456. {
  457. $filterSet = $this->_simpy->getWatchlist('1331')->getFilters();
  458. $this->assertEquals(
  459. $filterSet->getLength(),
  460. 1,
  461. 'Filter set does not have the expected size'
  462. );
  463. $filter = $filterSet->getIterator()->current();
  464. $this->assertEquals(
  465. $filter->getName(),
  466. 'Test Filter',
  467. 'Name does not match'
  468. );
  469. $this->assertEquals(
  470. $filter->getQuery(),
  471. 'zend',
  472. 'Query does not match'
  473. );
  474. }
  475. protected function _getTagsArray()
  476. {
  477. $tagSet = $this->_simpy->getTags();
  478. $tagArray = array();
  479. foreach ($tagSet as $tag) {
  480. $tagArray[] = $tag->getTag();
  481. }
  482. return $tagArray;
  483. }
  484. }
  485. class Zend_Service_SimpyTest_Skip extends PHPUnit_Framework_TestCase
  486. {
  487. public function setUp()
  488. {
  489. $this->markTestSkipped('Zend_Service_Simpy tests not enabled in TestConfiguration.php');
  490. }
  491. public function testNothing()
  492. {
  493. }
  494. }