/tests/cases/models/PostTest.php

https://github.com/pointlessjon/sphere · PHP · 423 lines · 323 code · 99 blank · 1 comment · 0 complexity · af74a4ed0e4dabdafffd6685b8460650 MD5 · raw file

  1. <?php
  2. namespace app\tests\cases\models;
  3. use app\tests\mocks\models\MockPost as Post;
  4. use lithium\data\Connections;
  5. use lithium\data\model\Query;
  6. use lithium\util\Inflector;
  7. class PostTest extends \lithium\test\Unit {
  8. public function setUp() {}
  9. public function tearDown() {
  10. Post::all()->delete();
  11. }
  12. public function testSave() {
  13. $post = Post::create(array('title' => 'the title', 'content' => 'the content'));
  14. $expected = Inflector::slug($post->title);
  15. $save = $post->save();
  16. $result = strstr($post->_id, $expected);
  17. $this->assertTrue($result);
  18. $data = $post->data();
  19. unset($data['_id']);
  20. $new = Post::create($data);
  21. $new->save($data);
  22. $this->assertNotEqual($post->_id, $new->_id);
  23. $expected = $content = 'updated content';
  24. $post->save(compact('content'));
  25. $post = Post::first($post->_id);
  26. $this->assertEqual($expected, $post->content);
  27. $post = Post::create(array('title' => 'sm', 'content' => 'title too short'));
  28. $title = $post->title;
  29. $post->save();
  30. $this->assertNotEqual($title, $post->_id);
  31. $post = Post::create(array(
  32. 'title' => 'test',
  33. 'content' => 'testing tags',
  34. 'tags' => 'one,Tag Two, ∆three, '
  35. ));
  36. $post->save();
  37. $result = $post->tags->data();
  38. $expected = array('one','Tag-Two','three');
  39. $this->assertEqual($expected, $result);
  40. $expected = 'pointlessjon';
  41. $result = $post->user_id;
  42. $this->assertEqual($expected, $result);
  43. $post = Post::create(array(
  44. 'title' => 'forcing user id',
  45. 'content' => 'shtuff',
  46. 'user_id' => 'lithosphere'
  47. ));
  48. $post->save();
  49. $expected = 'lithosphere';
  50. $result = $post->user_id;
  51. $this->assertEqual($expected, $result);
  52. }
  53. public function testPostUser() {
  54. $post = Post::create(array(
  55. 'title' => 'my title',
  56. 'content' => 'jurassic park 3 sucked'
  57. ));
  58. $post->save();
  59. $user = $post->user();
  60. $expected = 'pointlessjon';
  61. $result = $user->_id;
  62. $this->assertEqual($expected, $result);
  63. // second time tests use of model cache
  64. $user = $post->user();
  65. $expected = 'pointlessjon';
  66. $result = $user->_id;
  67. $this->assertEqual($expected, $result);
  68. $post = Post::create(array(
  69. 'title' => 'new title',
  70. 'content' => 'titanic 2 was ok',
  71. 'user_id' => 'gwoo'
  72. ));
  73. $post->save();
  74. $user = $post->user();
  75. $expected = 'gwoo';
  76. $result = $user->_id;
  77. $this->assertEqual($expected, $result);
  78. $post = Post::create(array(
  79. 'title' => 'how i do this?',
  80. 'content' => 'LOL',
  81. 'user_id' => 'mork'
  82. ));
  83. $post->save();
  84. $user = $post->user();
  85. $expected = null;
  86. $result = $user;
  87. $this->assertEqual($expected, $result);
  88. }
  89. public function testComment() {
  90. $user = array(
  91. '_id' => 'gwoo',
  92. 'email' => 'gwoo@example.com'
  93. );
  94. $post = Post::create(array('title' => 'another title', 'content' => 'the content'));
  95. $post->save();
  96. $data = array('content' => 'cool') + compact('user');
  97. $args = null;
  98. $result = $post->comment(compact('data', 'args'));
  99. $expected = 1;
  100. $result = $post->comments->count();
  101. $this->assertEqual($expected, $result);
  102. $comment = $post->comments->first();
  103. $expected = 'cool';
  104. $result = $comment['content'];
  105. $this->assertEqual($expected, $result);
  106. $expected = $user;
  107. $result = $comment['user']->data();
  108. $this->assertEqual($expected, $result);
  109. $data = array('content' => 'super cool') + compact('user');
  110. $args = array('0');
  111. $result = $post->comment(compact('data', 'args'));
  112. $this->assertTrue($result);
  113. $comment = $post->comments->first();
  114. $expected = 1;
  115. $result = $comment['comment_count'];
  116. $this->assertEqual($expected, $result);
  117. $expected = 1;
  118. $result = $post->comments->first()->comments->count();
  119. $this->assertEqual($expected, $result);
  120. $comments = $post->comments->first()->comments->data();
  121. $expected = 'super cool';
  122. $result = $comments[0]['content'];
  123. $this->assertEqual($expected, $result);
  124. $data = array('content' => 'nice') + compact('user');
  125. $args = array('0');
  126. $result = $post->comment(compact('data', 'args'));
  127. $this->assertTrue($result);
  128. $comment = $post->comments->first();
  129. $expected = 2;
  130. $result = $comment['comment_count'];
  131. $this->assertEqual($expected, $result);
  132. $expected = 2;
  133. $result = $post->comments->first()->comments->count();
  134. $this->assertEqual($expected, $result);
  135. $comments = $post->comments->first()->comments->data();
  136. $expected = 'nice';
  137. $result = $comments[1]['content'];
  138. $this->assertEqual($expected, $result);
  139. $expected = 'gwoo';
  140. $result = $comments[1]['user']['_id'];
  141. $this->assertEqual($expected, $result);
  142. $expected = 'gwoo@example.com';
  143. $result = $comments[1]['user']['email'];
  144. $this->assertEqual($expected, $result);
  145. $data = array('content' => 'super nice');
  146. $args = array('0', '0');
  147. $result = $post->comment(compact('data', 'args'));
  148. $this->assertTrue($result);
  149. $comment = $post->comments->first();
  150. $expected = 3;
  151. $result = $comment['comment_count'];
  152. $this->assertEqual($expected, $result);
  153. $expected = 2;
  154. $result = $post->comments->first()->comments->count();
  155. $this->assertEqual($expected, $result);
  156. $comments = $post->comments->first()->comments->data();
  157. $expected = 'super nice';
  158. $result = $comments[0]['comments'][0]['content'];
  159. $this->assertEqual($expected, $result);
  160. $expected = 'pointlessjon';
  161. $result = $comments[0]['comments'][0]['user']['_id'];
  162. $this->assertEqual($expected, $result);
  163. $expected = 'pointlessjon@example.com';
  164. $result = $comments[0]['comments'][0]['user']['email'];
  165. $this->assertEqual($expected, $result);
  166. }
  167. public function testEndorsePost() {
  168. $post = Post::create(array(
  169. 'title' => 'another title',
  170. 'content' => 'the content',
  171. 'user_id' => 'albert'
  172. ));
  173. $author = array(
  174. '_id' => 'albert', 'email' => 'albert@example.com'
  175. );
  176. $this->assertTrue($post->save());
  177. $expected = 0;
  178. $this->assertEqual($expected, $post->rating);
  179. $expected = array();
  180. $result = $post->endorsements->data();
  181. $this->assertEqual($expected, $result);
  182. $authorEndorsed = $post->endorse(compact('author'));
  183. $this->assertTrue($authorEndorsed);
  184. $expected = 0;
  185. $this->assertEqual($expected, $post->rating);
  186. $expected = array();
  187. $result = $post->endorsements->data();
  188. $this->assertEqual($expected, $result);
  189. $this->assertTrue($post->endorse());
  190. $expected = 1;
  191. $this->assertEqual($expected, $post->rating);
  192. $expected = array('pointlessjon');
  193. $result = $post->endorsements->data();
  194. $this->assertEqual($expected, $result);
  195. $this->assertTrue($post->endorse());
  196. $expected = 1;
  197. $this->assertEqual($expected, $post->rating);
  198. $expected = array('pointlessjon');
  199. $result = $post->endorsements->data();
  200. $author = array(
  201. '_id' => 'gwoo', 'email' => 'gwoo@example.com'
  202. );
  203. $endorsed = $post->endorse(compact('author'));
  204. $this->assertTrue($endorsed);
  205. $expected = 2;
  206. $this->assertEqual($expected, $post->rating);
  207. $expected = array(
  208. 'pointlessjon','gwoo'
  209. );
  210. $result = $post->endorsements->data();
  211. $this->assertEqual($expected, $result);
  212. }
  213. public function testEndorseComment() {
  214. $post = Post::create(array(
  215. 'title' => 'another title',
  216. 'content' => 'the content',
  217. 'user_id' => 'albert'
  218. ));
  219. $author = array(
  220. '_id' => 'albert', 'email' => 'albert@example.com'
  221. );
  222. $this->assertTrue($post->save());
  223. $data = array('content' => 'nice one!', 'user' => $author);
  224. $comment = $post->comment(compact('data'));
  225. $this->assertTrue($comment);
  226. $expected = 0;
  227. $result = $post->comments->first()->rating;
  228. $this->assertEqual($expected, $result);
  229. $expected = array();
  230. $result = $post->comments->first()->endorsements->data();
  231. $this->assertEqual($expected, $result);
  232. $args = array('0');
  233. $authorEndorsed = $post->endorse(compact('author','args'));
  234. $this->assertTrue($authorEndorsed);
  235. $expected = 0;
  236. $result = $post->comments->first()->rating;
  237. $this->assertEqual($expected, $result);
  238. $expected = array();
  239. $result = $post->comments->first()->endorsements->data();
  240. $this->assertEqual($expected, $result);
  241. $endorse = $post->endorse(compact('args'));
  242. $this->assertTrue($endorse);
  243. $expected = 1;
  244. $result = $post->comments->first()->rating;
  245. $this->assertEqual($expected, $result);
  246. $expected = array('pointlessjon');
  247. $result = $post->comments->first()->endorsements->data();
  248. $this->assertEqual($expected, $result);
  249. $endorsedAgain = $post->endorse(compact('args'));
  250. $this->assertTrue($endorsedAgain);
  251. $expected = 1;
  252. $result = $post->comments->first()->rating;
  253. $this->assertEqual($expected, $result);
  254. $expected = array('pointlessjon');
  255. $result = $post->comments->first()->endorsements->data();
  256. $this->assertEqual($expected, $result);
  257. $author = array(
  258. '_id' => 'gwoo', 'email' => 'gwoo@example.com'
  259. );
  260. $endorsed = $post->endorse(compact('author','args'));
  261. $this->assertTrue($endorsed);
  262. $expected = 2;
  263. $result = $post->comments->first()->rating;
  264. $this->assertEqual($expected, $result);
  265. $expected = array(
  266. 'pointlessjon','gwoo'
  267. );
  268. $result = $post->comments->first()->endorsements->data();
  269. $this->assertEqual($expected, $result);
  270. $comment = $post->comment(compact('data', 'args'));
  271. $this->assertTrue($comment);
  272. $expected = 0;
  273. $result = $post->comments->first()->comments->first()->rating;
  274. $this->assertEqual($expected, $result);
  275. $expected = array();
  276. $result = $post->comments->first()->comments->first()->endorsements->data();
  277. $this->assertEqual($expected, $result);
  278. $author = array(
  279. '_id' => 'albert', 'email' => 'albert@example.com'
  280. );
  281. $args = array('0','0');
  282. $authorEndorsed = $post->endorse(compact('args','author'));
  283. $this->assertTrue($authorEndorsed);
  284. $expected = 0;
  285. $result = $post->comments->first()->comments->first()->rating;
  286. $this->assertEqual($expected, $result);
  287. $expected = array();
  288. $result = $post->comments->first()->comments->first()->endorsements->data();
  289. $this->assertEqual($expected, $result);
  290. $endorse = $post->endorse(compact('args'));
  291. $this->assertTrue($endorse);
  292. $expected = 1;
  293. $result = $post->comments->first()->comments->first()->rating;
  294. $this->assertEqual($expected, $result);
  295. $expected = array('pointlessjon');
  296. $result = $post->comments->first()->comments->first()->endorsements->data();
  297. $this->assertEqual($expected, $result);
  298. $endorsedAgain = $post->endorse(compact('args'));
  299. $this->assertTrue($endorsedAgain);
  300. $expected = 1;
  301. $result = $post->comments->first()->comments->first()->rating;
  302. $this->assertEqual($expected, $result);
  303. $expected = array('pointlessjon');
  304. $result = $post->comments->first()->comments->first()->endorsements->data();
  305. $this->assertEqual($expected, $result);
  306. $author = array(
  307. '_id' => 'gwoo', 'email' => 'gwoo@example.com'
  308. );
  309. $endorsed = $post->endorse(compact('author','args'));
  310. $this->assertTrue($endorsed);
  311. $expected = 2;
  312. $result = $post->comments->first()->comments->first()->rating;
  313. $this->assertEqual($expected, $result);
  314. $expected = array(
  315. 'pointlessjon','gwoo'
  316. );
  317. $result = $post->comments->first()->comments->first()->endorsements->data();
  318. $this->assertEqual($expected, $result);
  319. }
  320. public function testRating() {
  321. $post = Post::create(array('title' => 'unrated', 'content' => '[REDACTED]'));
  322. }
  323. }
  324. ?>