PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/Tests/Acl/AclVoteManagerTest.php

https://github.com/wowo/FOSCommentBundle
PHP | 256 lines | 175 code | 53 blank | 28 comment | 0 complexity | c67c057918d8d5ddf9048dd003be1d36 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the FOSCommentBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace FOS\CommentBundle\Tests\Acl;
  11. use FOS\CommentBundle\Acl\AclVoteManager;
  12. use FOS\CommentBundle\Model\VoteInterface;
  13. use FOS\CommentBundle\Model\VoteManagerInterface;
  14. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  15. /**
  16. * Tests the functionality provided by Acl\AclVoteManager.
  17. *
  18. * @author Tim Nagel <tim@nagel.com.au>
  19. */
  20. class AclVoteManagerTest extends \PHPUnit_Framework_TestCase
  21. {
  22. protected $realManager;
  23. protected $voteSecurity;
  24. protected $commentSecurity;
  25. protected $vote;
  26. public function setUp()
  27. {
  28. $this->realManager = $this->getMock('FOS\CommentBundle\Model\VoteManagerInterface');
  29. $this->voteSecurity = $this->getMock('FOS\CommentBundle\Acl\VoteAclInterface');
  30. $this->commentSecurity = $this->getMock('FOS\CommentBundle\Acl\CommentAclInterface');
  31. $this->vote = $this->getMock('FOS\CommentBundle\Model\VoteInterface');
  32. }
  33. /**
  34. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  35. */
  36. public function testFindVoteById()
  37. {
  38. $id = 1;
  39. $expectedResult = $this->vote;
  40. $this->realManager->expects($this->once())
  41. ->method('findVoteById')
  42. ->with($id)
  43. ->will($this->returnValue($expectedResult));
  44. $this->voteSecurity->expects($this->once())
  45. ->method('canView')
  46. ->with($this->vote)
  47. ->will($this->returnValue(false));
  48. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  49. $manager->findVoteById($id);
  50. }
  51. public function testFindVoteByIdAllowed()
  52. {
  53. $id = 1;
  54. $expectedResult = $this->vote;
  55. $this->realManager->expects($this->once())
  56. ->method('findVoteById')
  57. ->with($id)
  58. ->will($this->returnValue($expectedResult));
  59. $this->voteSecurity->expects($this->once())
  60. ->method('canView')
  61. ->with($this->vote)
  62. ->will($this->returnValue(true));
  63. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  64. $result = $manager->findVoteById($id);
  65. $this->assertEquals($expectedResult, $result);
  66. }
  67. /**
  68. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  69. */
  70. public function testFindVoteBy()
  71. {
  72. $conditions = array('id' => 1);
  73. $expectedResult = $this->vote;
  74. $this->realManager->expects($this->once())
  75. ->method('findVoteBy')
  76. ->with($conditions)
  77. ->will($this->returnValue($expectedResult));
  78. $this->voteSecurity->expects($this->once())
  79. ->method('canView')
  80. ->with($this->vote)
  81. ->will($this->returnValue(false));
  82. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  83. $manager->findVoteBy($conditions);
  84. }
  85. public function testFindVoteByAllowed()
  86. {
  87. $conditions = array('id' => 1);
  88. $expectedResult = $this->vote;
  89. $this->realManager->expects($this->once())
  90. ->method('findVoteBy')
  91. ->with($conditions)
  92. ->will($this->returnValue($expectedResult));
  93. $this->voteSecurity->expects($this->once())
  94. ->method('canView')
  95. ->with($this->vote)
  96. ->will($this->returnValue(true));
  97. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  98. $result = $manager->findVoteBy($conditions);
  99. $this->assertEquals($expectedResult, $result);
  100. }
  101. /**
  102. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  103. */
  104. public function testFindVotesByComment()
  105. {
  106. $comment = $this->getMock('FOS\CommentBundle\Model\VotableCommentInterface');
  107. $expectedResult = array($this->vote);
  108. $this->realManager->expects($this->once())
  109. ->method('findVotesByComment')
  110. ->with($comment)
  111. ->will($this->returnValue($expectedResult));
  112. $this->voteSecurity->expects($this->once())
  113. ->method('canView')
  114. ->with($this->vote)
  115. ->will($this->returnValue(false));
  116. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  117. $manager->findVotesByComment($comment);
  118. }
  119. public function testFindVotesByCommentAllowed()
  120. {
  121. $comment = $this->getMock('FOS\CommentBundle\Model\VotableCommentInterface');
  122. $expectedResult = array($this->vote);
  123. $this->realManager->expects($this->once())
  124. ->method('findVotesByComment')
  125. ->with($comment)
  126. ->will($this->returnValue($expectedResult));
  127. $this->voteSecurity->expects($this->once())
  128. ->method('canView')
  129. ->with($this->vote)
  130. ->will($this->returnValue(true));
  131. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  132. $result = $manager->findVotesByComment($comment);
  133. $this->assertEquals($expectedResult, $result);
  134. }
  135. /**
  136. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  137. */
  138. public function testAddVoteNoCreate()
  139. {
  140. $comment = $this->getMock('FOS\CommentBundle\Model\VotableCommentInterface');
  141. $this->realManager->expects($this->never())
  142. ->method('addVote');
  143. $this->voteSecurity->expects($this->once())
  144. ->method('canCreate')
  145. ->will($this->returnValue(false));
  146. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  147. $manager->addVote($this->vote, $comment);
  148. }
  149. /**
  150. * @expectedException Symfony\Component\Security\Core\Exception\AccessDeniedException
  151. */
  152. public function testAddVoteNoViewComment()
  153. {
  154. $comment = $this->getMock('FOS\CommentBundle\Model\VotableCommentInterface');
  155. $this->realManager->expects($this->never())
  156. ->method('addVote');
  157. $this->voteSecurity->expects($this->once())
  158. ->method('canCreate')
  159. ->will($this->returnValue(true));
  160. $this->commentSecurity->expects($this->once())
  161. ->method('canView')
  162. ->with($comment)
  163. ->will($this->returnValue(false));
  164. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  165. $manager->addVote($this->vote, $comment);
  166. }
  167. public function testAddVote()
  168. {
  169. $comment = $this->getMock('FOS\CommentBundle\Model\VotableCommentInterface');
  170. $this->realManager->expects($this->once())
  171. ->method('addVote')
  172. ->with($this->vote,
  173. $comment);
  174. $this->voteSecurity->expects($this->once())
  175. ->method('canCreate')
  176. ->will($this->returnValue(true));
  177. $this->commentSecurity->expects($this->once())
  178. ->method('canView')
  179. ->with($comment)
  180. ->will($this->returnValue(true));
  181. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  182. $manager->addVote($this->vote, $comment);
  183. }
  184. public function testGetClass()
  185. {
  186. $class = 'Hello\Hello';
  187. $this->realManager->expects($this->once())
  188. ->method('getClass')
  189. ->will($this->returnValue($class));
  190. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  191. $result = $manager->getClass();
  192. $this->assertEquals($class, $result);
  193. }
  194. public function testCreateVote()
  195. {
  196. $this->realManager->expects($this->once())
  197. ->method('createVote')
  198. ->will($this->returnValue($this->vote));
  199. $manager = new AclVoteManager($this->realManager, $this->voteSecurity, $this->commentSecurity);
  200. $result = $manager->createVote();
  201. $this->assertEquals($this->vote, $result);
  202. }
  203. }