2
0

BaseTests.php 14 KB

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