NamedScopeBehaviorTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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::testBasic()
  23. *
  24. * @return void
  25. */
  26. public function testBasic() {
  27. $before = $this->Comment->find('count');
  28. $this->Comment->scope('active', array('published' => 'Y'));
  29. $options = array(
  30. 'scope' => array('active')
  31. );
  32. $after = $this->Comment->find('count', $options);
  33. $this->assertTrue($before > $after);
  34. $this->assertSame(5, $after);
  35. }
  36. /**
  37. * NamedScopeBehaviorTest::testCrossModel()
  38. *
  39. * @return void
  40. */
  41. public function testCrossModel() {
  42. $before = $this->Comment->find('count');
  43. $this->Comment->scope('active', array('Comment.published' => 'Y'));
  44. $this->Comment->User->scope('senior', array('User.id <' => '3'));
  45. $options = array(
  46. 'contain' => array('User'),
  47. 'scope' => array('Comment.active', 'User.senior')
  48. );
  49. $after = $this->Comment->find('count', $options);
  50. $this->assertTrue($before > $after);
  51. $this->assertSame(4, $after);
  52. }
  53. }