BaseTests.php 14 KB

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