PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/TestOfLinkMySQLDAO.php

https://github.com/dagda/ThinkUp
PHP | 290 lines | 203 code | 38 blank | 49 comment | 5 complexity | 3e18a6ac3c4fbc41d25c3270940d165b MD5 | raw file
  1. <?php
  2. require_once dirname(__FILE__).'/init.tests.php';
  3. require_once THINKUP_ROOT_PATH.'webapp/_lib/extlib/simpletest/autorun.php';
  4. require_once THINKUP_ROOT_PATH.'webapp/config.inc.php';
  5. /**
  6. * Test Of Link DAO
  7. * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
  8. * @author christoffer Viken <christoffer[at]viken[dot]me>
  9. */
  10. class TestOfLinkMySQLDAO extends ThinkUpUnitTestCase {
  11. /**
  12. * Constructor
  13. */
  14. public function __construct() {
  15. $this->UnitTestCase('LinkMySQLDAO class test');
  16. }
  17. /**
  18. * Constructs a database and populates it.
  19. */
  20. public function setUp() {
  21. parent::setUp();
  22. $this->DAO = new LinkMySQLDAO();
  23. //Insert test links (not images, not expanded)
  24. $counter = 0;
  25. while ($counter < 40) {
  26. $post_id = $counter + 80;
  27. $pseudo_minute = str_pad(($counter), 2, "0", STR_PAD_LEFT);
  28. $q = "INSERT INTO tu_links (url, title, clicks, post_id, network, is_image) ";
  29. $q .= " VALUES ('http://example.com/".$counter."', 'Link $counter', 0, $post_id, 'twitter', 0);";
  30. PDODAO::$PDO->exec($q);
  31. $counter++;
  32. }
  33. //Insert test links (images from Flickr, not expanded)
  34. $counter = 0;
  35. while ($counter < 5) {
  36. $post_id = $counter + 80;
  37. $pseudo_minute = str_pad(($counter), 2, "0", STR_PAD_LEFT);
  38. $q = "INSERT INTO tu_links (url, title, clicks, post_id, network, is_image) ";
  39. $q .= "VALUES ('http://flic.kr/p/".$counter."', 'Link $counter', 0, $post_id, 'twitter', 1);";
  40. PDODAO::$PDO->exec($q);
  41. $counter++;
  42. }
  43. //Insert test links with errors (images from Flickr, not expanded)
  44. $counter = 0;
  45. while ($counter < 5) {
  46. $post_id = $counter + 80;
  47. $pseudo_minute = str_pad(($counter), 2, "0", STR_PAD_LEFT);
  48. $q = "INSERT INTO tu_links (url, title, clicks, post_id, network, is_image, error) ";
  49. $q .= "VALUES ('http://flic.kr/p/".$counter."', 'Link $counter', 0, $post_id, 'twitter', 1, ";
  50. $q .= "'Generic test error message, Photo not found');";
  51. PDODAO::$PDO->exec($q);
  52. $counter++;
  53. }
  54. //Insert several of the same shortened link
  55. $counter = 0;
  56. while ($counter < 5) {
  57. $post_id = $counter + 80;
  58. $pseudo_minute = str_pad(($counter), 2, "0", STR_PAD_LEFT);
  59. $q = "INSERT INTO tu_links (url, title, clicks, post_id, network, is_image, error) ";
  60. $q .= "VALUES ('http://bit.ly/beEEfs', 'Link $counter', 0, $post_id, 'twitter', 1, '');";
  61. $this->db->exec($q);
  62. $counter++;
  63. }
  64. //Insert several posts
  65. $counter = 0;
  66. while ($counter < 5) {
  67. $post_id = $counter + 80;
  68. $user_id = ($counter * 5) + 2;
  69. $pseudo_minute = str_pad(($counter), 2, "0", STR_PAD_LEFT);
  70. $q = "INSERT INTO tu_posts ( ";
  71. $q .= " post_id, author_user_id, author_username, author_fullname ";
  72. $q .= " ) ";
  73. $q .= "VALUES ('$post_id', $user_id, 'user$counter', 'User$counter Name$counter' ";
  74. $q .= " );";
  75. $this->db->exec($q);
  76. $counter++;
  77. }
  78. $q = "INSERT INTO tu_follows (";
  79. $q .= " follower_id, user_id, active ";
  80. $q .= " ) ";
  81. $q .= " VALUES ";
  82. $q .= " (2, 7, 1), ";
  83. $q .= " (2, 22, 1), ";
  84. $q .= " (2, 17, 1), ";
  85. $q .= " (2, 12, 0), ";
  86. $q .= " (27, 2, 1), ";
  87. $q .= " (18, 22, 0), ";
  88. $q .= " (12, 22, 1) ";
  89. $this->db->exec($q);
  90. }
  91. /**
  92. * Destructs the database, so it can be reconstructed for next test
  93. */
  94. public function tearDown() {
  95. parent::tearDown();
  96. $this->DAO = null;
  97. }
  98. /**
  99. * Test Of Insert Method
  100. */
  101. public function testInsert(){
  102. $result = $this->DAO->insert(
  103. 'http://example.com/test',
  104. 'http://very.long.domain.that.nobody.would.bother.to.type.com/index.php', 'Very Long URL', '12345678901',
  105. 'twitter', false);
  106. //Is insert ID returned?
  107. $this->assertEqual($result, 56);
  108. //OK now check it
  109. $result = $this->DAO->getLinkByUrl('http://example.com/test');
  110. $this->assertIsA($result, "Link");
  111. $this->assertEqual($result->url, 'http://example.com/test');
  112. $this->assertEqual($result->expanded_url,
  113. 'http://very.long.domain.that.nobody.would.bother.to.type.com/index.php');
  114. $this->assertEqual($result->title, 'Very Long URL');
  115. $this->assertEqual($result->post_id, 12345678901);
  116. $this->assertEqual($result->network, 'twitter');
  117. $this->assertFalse($result->is_image);
  118. }
  119. /**
  120. * Test Of saveExpandedUrl method
  121. */
  122. public function testSaveExpandedUrl() {
  123. $linkstoexpand = $this->DAO->getLinksToExpand();
  124. $link = $linkstoexpand[0];
  125. $this->DAO->saveExpandedUrl($link, "http://expandedurl.com");
  126. $updatedlink = $this->DAO->getLinkByUrl($link);
  127. $this->assertEqual($updatedlink->expanded_url, "http://expandedurl.com");
  128. $this->DAO->saveExpandedUrl($link, "http://expandedurl1.com", 'my title');
  129. $updatedlink = $this->DAO->getLinkByUrl($link);
  130. $this->assertEqual($updatedlink->expanded_url, "http://expandedurl1.com");
  131. $this->assertEqual($updatedlink->title, "my title");
  132. $this->DAO->saveExpandedUrl($link, "http://expandedurl2.com", 'my title1', 1);
  133. $updatedlink = $this->DAO->getLinkByUrl($link);
  134. $this->assertEqual($updatedlink->expanded_url, "http://expandedurl2.com");
  135. $this->assertEqual($updatedlink->title, "my title1");
  136. $this->assertTrue($updatedlink->is_image);
  137. }
  138. /**
  139. * Test Of saveExpansionError Method
  140. */
  141. public function testSaveExpansionError() {
  142. $linktogeterror = $this->DAO->getLinkById(10);
  143. $this->assertEqual($linktogeterror->error, '');
  144. $this->DAO->saveExpansionError($linktogeterror->url, "This is expansion error text");
  145. $linkthathaserror = $this->DAO->getLinkById(10);
  146. $this->assertEqual($linkthathaserror->error, "This is expansion error text");
  147. }
  148. /**
  149. * Test Of update Method
  150. */
  151. public function testUpdate(){
  152. $result = $this->DAO->insert(
  153. 'http://example.com/test',
  154. 'http://very.long.domain.that.nobody.would.bother.to.type.com/index.php',
  155. 'Very Long URL',
  156. 15000, 'twitter'
  157. );
  158. $this->assertEqual($result, 56);
  159. $result = $this->DAO->update(
  160. 'http://example.com/test',
  161. 'http://very.long.domain.that.nobody.would.bother.to.type.com/image.png',
  162. 'Even Longer URL',
  163. 15001, 'twitter',
  164. true
  165. );
  166. $this->assertEqual($result, 1);
  167. //OK now check it
  168. $result = $this->DAO->getLinkByUrl('http://example.com/test');
  169. $this->assertIsA($result, "Link");
  170. $this->assertEqual($result->url, 'http://example.com/test');
  171. $this->assertEqual($result->expanded_url,
  172. 'http://very.long.domain.that.nobody.would.bother.to.type.com/image.png');
  173. $this->assertEqual($result->title, 'Even Longer URL');
  174. $this->assertEqual($result->post_id, 15001);
  175. $this->assertEqual($result->id, 56);
  176. }
  177. /**
  178. * Test Of getLinksByFriends Method
  179. */
  180. public function testGetLinksByFriends(){
  181. $result = $this->DAO->getLinksByFriends(2, 'twitter');
  182. $this->assertIsA($result, "array");
  183. $this->assertEqual(count($result), 12);
  184. $posts = array(
  185. 80=>array('pid'=>80, 'uid'=>2, 'fr'=>true),
  186. 81=>array('pid'=>81, 'uid'=>7, 'fr'=>true),
  187. 82=>array('pid'=>82, 'uid'=>12, 'fr'=>false),
  188. 83=>array('pid'=>83, 'uid'=>17, 'fr'=>true),
  189. 84=>array('pid'=>84, 'uid'=>22, 'fr'=>true)
  190. );
  191. foreach($result as $key=>$val){
  192. $this->assertIsA($val, "link");
  193. $this->assertIsA($val->container_post, "Post");
  194. $num = $val->post_id;
  195. $pid = $posts[$num]['pid'];
  196. $uid = $posts[$num]['uid'];
  197. $this->assertEqual($val->container_post->post_id, $pid);
  198. $this->assertEqual($val->container_post->author_user_id, $uid);
  199. $this->assertTrue($posts[$num]['fr']);
  200. }
  201. }
  202. /**
  203. * Test Of getPhotosByFriends Method
  204. */
  205. public function testGetPhotosByFriends(){
  206. $result = $this->DAO->getPhotosByFriends(2, 'twitter');
  207. $this->assertIsA($result, "array");
  208. $this->assertEqual(count($result), 9);
  209. $posts = array(
  210. 80=>array('pid'=>80, 'uid'=>2, 'fr'=>true),
  211. 81=>array('pid'=>81, 'uid'=>7, 'fr'=>true),
  212. 82=>array('pid'=>82, 'uid'=>12, 'fr'=>false),
  213. 83=>array('pid'=>83, 'uid'=>17, 'fr'=>true),
  214. 84=>array('pid'=>84, 'uid'=>22, 'fr'=>true)
  215. );
  216. foreach($result as $key=>$val){
  217. $this->assertIsA($val, "link");
  218. $this->assertIsA($val->container_post, "Post");
  219. $this->assertTrue($val->is_image);
  220. $num = $val->post_id;
  221. $pid = $posts[$num]['pid'];
  222. $uid = $posts[$num]['uid'];
  223. $this->assertEqual($val->container_post->post_id, $pid);
  224. $this->assertEqual($val->container_post->author_user_id, $uid);
  225. $this->assertTrue($posts[$num]['fr']);
  226. }
  227. }
  228. /**
  229. * Test Of getLinksToExpand Method
  230. */
  231. public function testGetLinksToExpand() {
  232. $linkstoexpand = $this->DAO->getLinksToExpand();
  233. $this->assertEqual(count($linkstoexpand), 46);
  234. $this->assertIsA($linkstoexpand, "array");
  235. }
  236. /**
  237. * Test Of getLinkByID
  238. */
  239. public function testGetLinkById() {
  240. $link = $this->DAO->getLinkById(1);
  241. $this->assertEqual($link->id, 1);
  242. $this->assertEqual($link->url, 'http://example.com/0');
  243. }
  244. /**
  245. * Test Of getLinksToExpandByURL Method
  246. */
  247. public function testGetLinksToExpandByURL() {
  248. $flickrlinkstoexpand = $this->DAO->getLinksToExpandByUrl('http://flic.kr/');
  249. $this->assertEqual(count($flickrlinkstoexpand), 5);
  250. $this->assertIsA($flickrlinkstoexpand, "array");
  251. }
  252. }