PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework-1.11.12/tests/Zend/Service/Simpy/BaseTests.php

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