| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- App::uses('NamedScopeBehavior', 'Tools.Model/Behavior');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- class NamedScopeBehaviorTest extends MyCakeTestCase {
- public $NamedScopeBehavior;
- public $Comment;
- public $fixtures = array('core.comment', 'core.user');
- public function setUp() {
- parent::setUp();
- $this->NamedScopeBehavior = new NamedScopeBehavior();
- $this->Comment = ClassRegistry::init('Comment');
- $this->Comment->bindModel(array('belongsTo' => array('User')), false);
- $this->Comment->displayField = 'comment';
- $this->Comment->Behaviors->load('Tools.NamedScope');
- $this->Comment->User->Behaviors->load('Tools.NamedScope');
- }
- public function testObject() {
- $this->assertTrue(is_object($this->NamedScopeBehavior));
- $this->assertInstanceOf('NamedScopeBehavior', $this->NamedScopeBehavior);
- }
- /**
- * NamedScopeBehaviorTest::testScope()
- *
- * @return void
- */
- public function testScope() {
- $result = $this->Comment->scope('active');
- $this->assertNull($result);
- $this->Comment->scope('active', array('published' => 'Y'));
- $result = $this->Comment->scope('active');
- $this->assertEquals(array('published' => 'Y'), $result);
- $this->Comment->scope('active', array('published' => 'Y', 'active' => 1));
- $result = $this->Comment->scope('active');
- $this->assertEquals(array('published' => 'Y', 'active' => 1), $result);
- $result = $this->Comment->scope();
- $this->assertEquals(array('active' => array('published' => 'Y', 'active' => 1)), $result);
- }
- /**
- * NamedScopeBehaviorTest::testBasic()
- *
- * @return void
- */
- public function testBasic() {
- $before = $this->Comment->find('count');
- $this->Comment->scope('active', array('published' => 'Y'));
- $options = array(
- 'scope' => array('active')
- );
- $after = $this->Comment->find('count', $options);
- $this->assertTrue($before > $after);
- $this->assertSame(5, $after);
- }
- /**
- * NamedScopeBehaviorTest::testCrossModel()
- *
- * @return void
- */
- public function testCrossModel() {
- $before = $this->Comment->find('count');
- $this->Comment->scope('active', array('Comment.published' => 'Y'));
- $this->Comment->User->scope('senior', array('User.id <' => '3'));
- $options = array(
- 'contain' => array('User'),
- 'scope' => array('Comment.active', 'User.senior')
- );
- $after = $this->Comment->find('count', $options);
- $this->assertTrue($before > $after);
- $this->assertSame(4, $after);
- }
- /**
- * NamedScopeBehaviorTest::testCrossModelWithAttributeScope()
- *
- * @return void
- */
- public function testCrossModelWithAttributeScope() {
- $this->Comment->scopes = array('active' => array('Comment.published' => 'Y'));
- $this->Comment->User->scopes = array('senior' => array('User.id <' => '2'));
- $this->Comment->Behaviors->load('Tools.NamedScope');
- $this->Comment->User->Behaviors->load('Tools.NamedScope');
- $options = array(
- 'contain' => array('User'),
- 'scope' => array('Comment.active', 'User.senior')
- );
- $after = $this->Comment->find('count', $options);
- $this->assertSame(2, $after);
- }
- /**
- * NamedScopeBehaviorTest::testScopedFind()
- *
- * @return void
- */
- public function testScopedFind() {
- $this->Comment->scopes = array('active' => array('Comment.published' => 'Y'));
- $this->Comment->User->scopes = array('senior' => array('User.id <' => '2'));
- $this->Comment->Behaviors->load('Tools.NamedScope');
- $this->Comment->User->Behaviors->load('Tools.NamedScope');
- $this->Comment->scopedFinds = array(
- 'activeAndSenior' => array(
- 'name' => 'Active and Senior',
- 'find' => array(
- 'virtualFields' => array(
- //'fullname' => "CONCAT(User.id, '-', User.user)"
- ),
- 'options' => array(
- 'scope' => array('Comment.active', 'User.senior'),
- 'contain' => array('User'),
- 'fields' => array('User.id', 'User.user'),
- 'order' => array('User.user' => 'ASC'),
- ),
- )
- )
- );
- $result = $this->Comment->scopedFind('activeAndSenior');
- $this->assertSame(2, count($result));
- $result = $this->Comment->scopedFind('activeAndSenior', array('type' => 'count'));
- $this->assertSame(2, $result);
- }
- /**
- * NamedScopeBehaviorTest::testScopedFindWithVirtualFields()
- *
- * @return void
- */
- public function testScopedFindWithVirtualFields() {
- $this->db = ConnectionManager::getDataSource('test');
- $this->skipIf(!($this->db instanceof Mysql), 'The virtualFields test is only compatible with Mysql.');
- $this->Comment->scopes = array('active' => array('Comment.published' => 'Y'));
- $this->Comment->User->scopes = array('senior' => array('User.id <' => '2'));
- $this->Comment->Behaviors->load('Tools.NamedScope');
- $this->Comment->User->Behaviors->load('Tools.NamedScope');
- $this->Comment->scopedFinds = array(
- 'activeAndSenior' => array(
- 'name' => 'Active and Senior',
- 'find' => array(
- 'virtualFields' => array(
- 'fullname' => "CONCAT(User.id, '-', User.user)"
- ),
- 'options' => array(
- 'scope' => array('Comment.active', 'User.senior'),
- 'contain' => array('User'),
- 'fields' => array('User.id', 'fullname'),
- 'order' => array('fullname' => 'ASC'),
- ),
- )
- )
- );
- $result = $this->Comment->scopedFind('activeAndSenior');
- $this->assertSame(2, count($result));
- $scopedFinds = $this->Comment->scopedFinds();
- $this->assertSame(array('activeAndSenior' => 'Active and Senior'), $scopedFinds);
- }
- /**
- * NamedScopeBehaviorTest::testScopedFindWithLimit()
- *
- * @return void
- */
- public function testScopedFindWithLimit() {
- $this->Comment->scopes = array('active' => array('Comment.published' => 'Y'));
- $this->Comment->User->scopes = array('senior' => array('User.id <' => '2'));
- $this->Comment->Behaviors->load('Tools.NamedScope');
- $this->Comment->User->Behaviors->load('Tools.NamedScope');
- $this->Comment->scopedFinds = array(
- 'activeAndSenior' => array(
- 'name' => 'Active and Senior',
- 'find' => array(
- 'virtualFields' => array(
- 'fullname' => "CONCAT(User.id, '-', User.user)"
- ),
- 'options' => array(
- 'scope' => array('Comment.active', 'User.senior'),
- 'contain' => array('User'),
- 'fields' => array('User.id', 'fullname'),
- 'order' => array('fullname' => 'ASC'),
- ),
- )
- )
- );
- $result = $this->Comment->scopedFind('activeAndSenior', array('options' => array('limit' => 1)));
- $this->assertSame(1, count($result));
- }
- /**
- * NamedScopeBehaviorTest::testException()
- *
- * @expectedException RuntimeException
- * @return void
- */
- public function testException() {
- $this->Comment->scopedFind('foo');
- }
- }
|