NamedScopeBehaviorTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. App::uses('NamedScopeBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class NamedScopeBehaviorTest extends MyCakeTestCase {
  5. public $NamedScopeBehavior;
  6. public $Comment;
  7. public $fixtures = array('core.comment', 'core.user');
  8. public function setUp() {
  9. parent::setUp();
  10. $this->NamedScopeBehavior = new NamedScopeBehavior();
  11. $this->Comment = ClassRegistry::init('Comment');
  12. $this->Comment->bindModel(array('belongsTo' => array('User')), false);
  13. $this->Comment->displayField = 'comment';
  14. $this->Comment->Behaviors->load('Tools.NamedScope');
  15. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  16. }
  17. public function testObject() {
  18. $this->assertTrue(is_object($this->NamedScopeBehavior));
  19. $this->assertInstanceOf('NamedScopeBehavior', $this->NamedScopeBehavior);
  20. }
  21. /**
  22. * NamedScopeBehaviorTest::testScope()
  23. *
  24. * @return void
  25. */
  26. public function testScope() {
  27. $result = $this->Comment->scope('active');
  28. $this->assertNull($result);
  29. $this->Comment->scope('active', array('published' => 'Y'));
  30. $result = $this->Comment->scope('active');
  31. $this->assertEquals(array('published' => 'Y'), $result);
  32. $this->Comment->scope('active', array('published' => 'Y', 'active' => 1));
  33. $result = $this->Comment->scope('active');
  34. $this->assertEquals(array('published' => 'Y', 'active' => 1), $result);
  35. $result = $this->Comment->scope();
  36. $this->assertEquals(array('active' => array('published' => 'Y', 'active' => 1)), $result);
  37. }
  38. /**
  39. * NamedScopeBehaviorTest::testBasic()
  40. *
  41. * @return void
  42. */
  43. public function testBasic() {
  44. $before = $this->Comment->find('count');
  45. $this->Comment->scope('active', array('published' => 'Y'));
  46. $options = array(
  47. 'scope' => array('active')
  48. );
  49. $after = $this->Comment->find('count', $options);
  50. $this->assertTrue($before > $after);
  51. $this->assertSame(5, $after);
  52. }
  53. /**
  54. * NamedScopeBehaviorTest::testCrossModel()
  55. *
  56. * @return void
  57. */
  58. public function testCrossModel() {
  59. $before = $this->Comment->find('count');
  60. $this->Comment->scope('active', array('Comment.published' => 'Y'));
  61. $this->Comment->User->scope('senior', array('User.id <' => '3'));
  62. $options = array(
  63. 'contain' => array('User'),
  64. 'scope' => array('Comment.active', 'User.senior')
  65. );
  66. $after = $this->Comment->find('count', $options);
  67. $this->assertTrue($before > $after);
  68. $this->assertSame(4, $after);
  69. }
  70. /**
  71. * NamedScopeBehaviorTest::testCrossModelWithAttributeScope()
  72. *
  73. * @return void
  74. */
  75. public function testCrossModelWithAttributeScope() {
  76. $this->Comment->scopes = array('active' => array('Comment.published' => 'Y'));
  77. $this->Comment->User->scopes = array('senior' => array('User.id <' => '2'));
  78. $this->Comment->Behaviors->load('Tools.NamedScope');
  79. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  80. $options = array(
  81. 'contain' => array('User'),
  82. 'scope' => array('Comment.active', 'User.senior')
  83. );
  84. $after = $this->Comment->find('count', $options);
  85. $this->assertSame(2, $after);
  86. }
  87. /**
  88. * NamedScopeBehaviorTest::testScopedFind()
  89. *
  90. * @return void
  91. */
  92. public function testScopedFind() {
  93. $this->Comment->scopes = array('active' => array('Comment.published' => 'Y'));
  94. $this->Comment->User->scopes = array('senior' => array('User.id <' => '2'));
  95. $this->Comment->Behaviors->load('Tools.NamedScope');
  96. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  97. $this->Comment->scopedFinds = array(
  98. 'activeAndSenior' => array(
  99. 'name' => 'Active and Senior',
  100. 'find' => array(
  101. 'virtualFields' => array(
  102. //'fullname' => "CONCAT(User.id, '-', User.user)"
  103. ),
  104. 'options' => array(
  105. 'scope' => array('Comment.active', 'User.senior'),
  106. 'contain' => array('User'),
  107. 'fields' => array('User.id', 'User.user'),
  108. 'order' => array('User.user' => 'ASC'),
  109. ),
  110. )
  111. )
  112. );
  113. $result = $this->Comment->scopedFind('activeAndSenior');
  114. $this->assertSame(2, count($result));
  115. $result = $this->Comment->scopedFind('activeAndSenior', array('type' => 'count'));
  116. $this->assertSame(2, $result);
  117. }
  118. /**
  119. * NamedScopeBehaviorTest::testScopedFindWithVirtualFields()
  120. *
  121. * @return void
  122. */
  123. public function testScopedFindWithVirtualFields() {
  124. $this->Comment->scopes = array('active' => array('Comment.published' => 'Y'));
  125. $this->Comment->User->scopes = array('senior' => array('User.id <' => '2'));
  126. $this->Comment->Behaviors->load('Tools.NamedScope');
  127. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  128. $this->Comment->scopedFinds = array(
  129. 'activeAndSenior' => array(
  130. 'name' => 'Active and Senior',
  131. 'find' => array(
  132. 'virtualFields' => array(
  133. 'fullname' => "CONCAT(User.id, '-', User.user)"
  134. ),
  135. 'options' => array(
  136. 'scope' => array('Comment.active', 'User.senior'),
  137. 'contain' => array('User'),
  138. 'fields' => array('User.id', 'fullname'),
  139. 'order' => array('fullname' => 'ASC'),
  140. ),
  141. )
  142. )
  143. );
  144. $result = $this->Comment->scopedFind('activeAndSenior');
  145. $this->assertSame(2, count($result));
  146. $scopedFinds = $this->Comment->scopedFinds();
  147. $this->assertSame(array('activeAndSenior' => 'Active and Senior'), $scopedFinds);
  148. }
  149. /**
  150. * NamedScopeBehaviorTest::testScopedFindWithLimit()
  151. *
  152. * @return void
  153. */
  154. public function testScopedFindWithLimit() {
  155. $this->Comment->scopes = array('active' => array('Comment.published' => 'Y'));
  156. $this->Comment->User->scopes = array('senior' => array('User.id <' => '2'));
  157. $this->Comment->Behaviors->load('Tools.NamedScope');
  158. $this->Comment->User->Behaviors->load('Tools.NamedScope');
  159. $this->Comment->scopedFinds = array(
  160. 'activeAndSenior' => array(
  161. 'name' => 'Active and Senior',
  162. 'find' => array(
  163. 'virtualFields' => array(
  164. 'fullname' => "CONCAT(User.id, '-', User.user)"
  165. ),
  166. 'options' => array(
  167. 'scope' => array('Comment.active', 'User.senior'),
  168. 'contain' => array('User'),
  169. 'fields' => array('User.id', 'fullname'),
  170. 'order' => array('fullname' => 'ASC'),
  171. ),
  172. )
  173. )
  174. );
  175. $result = $this->Comment->scopedFind('activeAndSenior', array('options' => array('limit' => 1)));
  176. $this->assertSame(1, count($result));
  177. }
  178. /**
  179. * NamedScopeBehaviorTest::testException()
  180. *
  181. * @expectedException RuntimeException
  182. * @return void
  183. */
  184. public function testException() {
  185. $this->Comment->scopedFind('foo');
  186. }
  187. }