| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?php
- App::uses('NamedScopeBehavior', 'Tools.Model/Behavior');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- class NamedScopeBehaviorTest extends MyCakeTestCase {
- public $NamedScopeBehavior;
- public $Comment;
- public $fixtures = ['core.comment', 'core.user'];
- public function setUp() {
- parent::setUp();
- $this->NamedScopeBehavior = new NamedScopeBehavior();
- $this->Comment = ClassRegistry::init('Comment');
- $this->Comment->bindModel(['belongsTo' => ['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', ['published' => 'Y']);
- $result = $this->Comment->scope('active');
- $this->assertEquals(['published' => 'Y'], $result);
- $this->Comment->scope('active', ['published' => 'Y', 'active' => 1]);
- $result = $this->Comment->scope('active');
- $this->assertEquals(['published' => 'Y', 'active' => 1], $result);
- $result = $this->Comment->scope();
- $this->assertEquals(['active' => ['published' => 'Y', 'active' => 1]], $result);
- }
- /**
- * NamedScopeBehaviorTest::testBasic()
- *
- * @return void
- */
- public function testBasic() {
- $before = $this->Comment->find('count');
- $this->Comment->scope('active', ['published' => 'Y']);
- $options = [
- 'scope' => ['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', ['Comment.published' => 'Y']);
- $this->Comment->User->scope('senior', ['User.id <' => '3']);
- $options = [
- 'contain' => ['User'],
- 'scope' => ['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 = ['active' => ['Comment.published' => 'Y']];
- $this->Comment->User->scopes = ['senior' => ['User.id <' => '2']];
- $this->Comment->Behaviors->load('Tools.NamedScope');
- $this->Comment->User->Behaviors->load('Tools.NamedScope');
- $options = [
- 'contain' => ['User'],
- 'scope' => ['Comment.active', 'User.senior']
- ];
- $after = $this->Comment->find('count', $options);
- $this->assertSame(2, $after);
- }
- /**
- * NamedScopeBehaviorTest::testScopedFind()
- *
- * @return void
- */
- public function testScopedFind() {
- $this->Comment->scopes = ['active' => ['Comment.published' => 'Y']];
- $this->Comment->User->scopes = ['senior' => ['User.id <' => '2']];
- $this->Comment->Behaviors->load('Tools.NamedScope');
- $this->Comment->User->Behaviors->load('Tools.NamedScope');
- $this->Comment->scopedFinds = [
- 'activeAndSenior' => [
- 'name' => 'Active and Senior',
- 'find' => [
- 'virtualFields' => [
- //'fullname' => "CONCAT(User.id, '-', User.user)"
- ],
- 'options' => [
- 'scope' => ['Comment.active', 'User.senior'],
- 'contain' => ['User'],
- 'fields' => ['User.id', 'User.user'],
- 'order' => ['User.user' => 'ASC'],
- ],
- ]
- ]
- ];
- $result = $this->Comment->scopedFind('activeAndSenior');
- $this->assertSame(2, count($result));
- $result = $this->Comment->scopedFind('activeAndSenior', ['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 = ['active' => ['Comment.published' => 'Y']];
- $this->Comment->User->scopes = ['senior' => ['User.id <' => '2']];
- $this->Comment->Behaviors->load('Tools.NamedScope');
- $this->Comment->User->Behaviors->load('Tools.NamedScope');
- $this->Comment->scopedFinds = [
- 'activeAndSenior' => [
- 'name' => 'Active and Senior',
- 'find' => [
- 'virtualFields' => [
- 'fullname' => "CONCAT(User.id, '-', User.user)"
- ],
- 'options' => [
- 'scope' => ['Comment.active', 'User.senior'],
- 'contain' => ['User'],
- 'fields' => ['User.id', 'fullname'],
- 'order' => ['fullname' => 'ASC'],
- ],
- ]
- ]
- ];
- $result = $this->Comment->scopedFind('activeAndSenior');
- $this->assertSame(2, count($result));
- $scopedFinds = $this->Comment->scopedFinds();
- $this->assertSame(['activeAndSenior' => 'Active and Senior'], $scopedFinds);
- }
- /**
- * NamedScopeBehaviorTest::testScopedFindWithLimit()
- *
- * @return void
- */
- public function testScopedFindWithLimit() {
- $this->Comment->scopes = ['active' => ['Comment.published' => 'Y']];
- $this->Comment->User->scopes = ['senior' => ['User.id <' => '2']];
- $this->Comment->Behaviors->load('Tools.NamedScope');
- $this->Comment->User->Behaviors->load('Tools.NamedScope');
- $this->Comment->scopedFinds = [
- 'activeAndSenior' => [
- 'name' => 'Active and Senior',
- 'find' => [
- 'virtualFields' => [
- 'fullname' => "CONCAT(User.id, '-', User.user)"
- ],
- 'options' => [
- 'scope' => ['Comment.active', 'User.senior'],
- 'contain' => ['User'],
- 'fields' => ['User.id', 'fullname'],
- 'order' => ['fullname' => 'ASC'],
- ],
- ]
- ]
- ];
- $result = $this->Comment->scopedFind('activeAndSenior', ['options' => ['limit' => 1]]);
- $this->assertSame(1, count($result));
- }
- /**
- * NamedScopeBehaviorTest::testScopedFindOverwrite()
- *
- * @return void
- */
- public function testScopedFindOverwrite() {
- $this->Comment->scopes = ['active' => ['Comment.published' => 'Y']];
- $this->Comment->Behaviors->load('Tools.NamedScope');
- $this->Comment->User->Behaviors->load('Tools.NamedScope');
- $this->Comment->scopedFinds = [
- 'active' => [
- 'name' => 'Active Comentators',
- 'find' => [
- 'type' => 'all',
- 'virtualFields' => [
- 'fullname' => "CONCAT(User.id, '-', User.user)"
- ],
- 'options' => [
- 'scope' => ['Comment.active'],
- 'contain' => ['User'],
- 'fields' => ['User.id', 'fullname'],
- 'order' => ['fullname' => 'ASC'],
- 'limit' => 5
- ]
- ]
- ]
- ];
- $result = $this->Comment->scopedFind('active', ['options' => ['limit' => 2]]);
- $this->assertSame(2, count($result));
- $result = $this->Comment->scopedFind('active', ['type' => 'count']);
- $this->assertSame(5, $result);
- $result = $this->Comment->scopedFind('active', ['type' => 'first', 'options' => ['fields' => ['User.id', 'User.created'], 'order' => ['User.id' => 'DESC']]]);
- $expected = [
- 'User' => [
- 'id' => 4,
- 'created' => '2007-03-17 01:22:23'
- ]
- ];
- $this->assertEquals($expected, $result);
- }
- /**
- * NamedScopeBehaviorTest::testException()
- *
- * @expectedException RuntimeException
- * @return void
- */
- public function testException() {
- $this->Comment->scopedFind('foo');
- }
- }
|