📄 containable.test.php
字号:
<?php/* SVN FILE: $Id: containable.test.php 7118 2008-06-04 20:49:29Z gwoo $ *./** * Short description for file. * * Long description for file * * PHP versions 4 and 5 * * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite> * Copyright 2005-2008, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The Open Group Test Suite License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests * @package cake.tests * @subpackage cake.tests.cases.libs.model.behaviors * @since CakePHP(tm) v 1.2.0.5669 * @version $Revision: 7118 $ * @modifiedby $LastChangedBy: gwoo $ * @lastmodified $Date: 2008-06-04 13:49:29 -0700 (Wed, 04 Jun 2008) $ * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License */App::import('Core', array('AppModel', 'Model'));require_once(dirname(__FILE__) . DS . '..' . DS . 'models.php');/** * ContainableTest class * * @package cake * @subpackage cake.tests.cases.libs.model.behaviors */class ContainableTest extends CakeTestCase {/** * Fixtures associated with this test case * * @var array * @access public */ var $fixtures = array( 'core.article', 'core.article_featured', 'core.article_featureds_tags', 'core.articles_tag', 'core.attachment', 'core.category', 'core.comment', 'core.featured', 'core.tag', 'core.user' );/** * Method executed before each test * * @access public */ function startTest() { $this->User =& ClassRegistry::init('User'); $this->Article =& ClassRegistry::init('Article'); $this->User->bind(array( 'Article' => array('type' => 'hasMany'), 'ArticleFeatured' => array('type' => 'hasMany'), 'Comment' => array('type' => 'hasMany') )); $this->User->ArticleFeatured->unbindModel(array('belongsTo' => array('Category')), false); $this->User->ArticleFeatured->hasMany['Comment']['foreignKey'] = 'article_id'; $this->User->Behaviors->attach('Containable'); $this->Article->Behaviors->attach('Containable'); }/** * Method executed after each test * * @access public */ function endTest() { unset($this->Article); unset($this->User); ClassRegistry::flush(); }/** * testContainments method * * @access public * @return void */ function testContainments() { $r = $this->__containments($this->Article, array('Comment' => array('conditions' => array('Comment.user_id' => 2)))); $this->assertTrue(Set::matches('/Article/keep/Comment/conditions[Comment.user_id=2]', $r)); $r = $this->__containments($this->User, array( 'ArticleFeatured' => array( 'Featured' => array( 'id', 'Category' => 'name' ) ))); $this->assertEqual(Set::extract('/ArticleFeatured/keep/Featured/fields', $r), array('id')); $r = $this->__containments($this->Article, array( 'Comment' => array( 'User', 'conditions' => array('Comment' => array('user_id' => 2)), ), )); $this->assertTrue(Set::matches('/User', $r)); $this->assertTrue(Set::matches('/Comment', $r)); $this->assertTrue(Set::matches('/Article/keep/Comment/conditions/Comment[user_id=2]', $r)); $r = $this->__containments($this->Article, array('Comment(comment, published)' => 'Attachment(attachment)', 'User(user)')); $this->assertTrue(Set::matches('/Comment', $r)); $this->assertTrue(Set::matches('/User', $r)); $this->assertTrue(Set::matches('/Article/keep/Comment', $r)); $this->assertTrue(Set::matches('/Article/keep/User', $r)); $this->assertEqual(Set::extract('/Article/keep/Comment/fields', $r), array('comment', 'published')); $this->assertEqual(Set::extract('/Article/keep/User/fields', $r), array('user')); $this->assertTrue(Set::matches('/Comment/keep/Attachment', $r)); $this->assertEqual(Set::extract('/Comment/keep/Attachment/fields', $r), array('attachment')); $r = $this->__containments($this->Article, array('Comment' => array('limit' => 1))); $this->assertEqual(array_keys($r), array('Comment', 'Article')); $this->assertEqual(array_shift(Set::extract('/Comment/keep', $r)), array('keep' => array())); $this->assertTrue(Set::matches('/Article/keep/Comment', $r)); $this->assertEqual(array_shift(Set::extract('/Article/keep/Comment/.', $r)), array('limit' => 1)); $r = $this->__containments($this->Article, array('Comment.User')); $this->assertEqual(array_keys($r), array('User', 'Comment', 'Article')); $this->assertEqual(array_shift(Set::extract('/User/keep', $r)), array('keep' => array())); $this->assertEqual(array_shift(Set::extract('/Comment/keep', $r)), array('keep' => array('User' => array()))); $this->assertEqual(array_shift(Set::extract('/Article/keep', $r)), array('keep' => array('Comment' => array()))); }/** * testInvalidContainments method * * @access public * @return void */ function testInvalidContainments() { $this->expectError(); $r = $this->__containments($this->Article, array('Comment', 'InvalidBinding')); $this->Article->Behaviors->attach('Containable', array('notices' => false)); $r = $this->__containments($this->Article, array('Comment', 'InvalidBinding')); }/** * testBeforeFind method * * @access public * @return void */ function testBeforeFind() { $r = $this->Article->find('all', array('contain' => array('Comment'))); $this->assertFalse(Set::matches('/User', $r)); $this->assertTrue(Set::matches('/Comment', $r)); $this->assertFalse(Set::matches('/Comment/User', $r)); $r = $this->Article->find('all', array('contain' => 'Comment.User')); $this->assertTrue(Set::matches('/Comment/User', $r)); $this->assertFalse(Set::matches('/Comment/Article', $r)); $r = $this->Article->find('all', array('contain' => array('Comment' => array('User', 'Article')))); $this->assertTrue(Set::matches('/Comment/User', $r)); $this->assertTrue(Set::matches('/Comment/Article', $r)); $r = $this->Article->find('all', array('contain' => array('Comment' => array('conditions' => array('Comment.user_id' => 2))))); $this->assertFalse(Set::matches('/Comment[user_id!=2]', $r)); $this->assertTrue(Set::matches('/Comment[user_id=2]', $r)); $r = $this->Article->find('all', array('contain' => array('Comment.user_id = 2'))); $this->assertFalse(Set::matches('/Comment[user_id!=2]', $r)); $r = $this->Article->find('all', array('contain' => 'Comment.id DESC')); $ids = $descIds = Set::extract('/Comment[1]/id', $r); rsort($descIds); $this->assertEqual($ids, $descIds); $r = $this->Article->find('all', array('contain' => 'Comment')); $this->assertTrue(Set::matches('/Comment[user_id!=2]', $r)); $r = $this->Article->find('all', array('contain' => array('Comment' => array('fields' => 'comment')))); $this->assertFalse(Set::matches('/Comment/created', $r)); $this->assertTrue(Set::matches('/Comment/comment', $r)); $this->assertFalse(Set::matches('/Comment/updated', $r)); $r = $this->Article->find('all', array('contain' => array('Comment' => array('fields' => array('comment', 'updated'))))); $this->assertFalse(Set::matches('/Comment/created', $r)); $this->assertTrue(Set::matches('/Comment/comment', $r)); $this->assertTrue(Set::matches('/Comment/updated', $r)); $r = $this->Article->find('all', array('contain' => array('Comment' => array('comment', 'updated')))); $this->assertFalse(Set::matches('/Comment/created', $r)); $this->assertTrue(Set::matches('/Comment/comment', $r)); $this->assertTrue(Set::matches('/Comment/updated', $r)); $r = $this->Article->find('all', array('contain' => array('Comment(comment,updated)'))); $this->assertFalse(Set::matches('/Comment/created', $r)); $this->assertTrue(Set::matches('/Comment/comment', $r)); $this->assertTrue(Set::matches('/Comment/updated', $r)); $r = $this->Article->find('all', array('contain' => 'Comment.created')); $this->assertTrue(Set::matches('/Comment/created', $r)); $this->assertFalse(Set::matches('/Comment/comment', $r)); $r = $this->Article->find('all', array('contain' => array('User.Article(title)', 'Comment(comment)'))); $this->assertFalse(Set::matches('/Comment/Article', $r)); $this->assertFalse(Set::matches('/Comment/User', $r)); $this->assertTrue(Set::matches('/Comment/comment', $r)); $this->assertFalse(Set::matches('/Comment/created', $r)); $this->assertTrue(Set::matches('/User/Article/title', $r)); $this->assertFalse(Set::matches('/User/Article/created', $r)); $r = $this->Article->find('all', array('contain' => array())); $this->assertFalse(Set::matches('/User', $r)); $this->assertFalse(Set::matches('/Comment', $r)); $this->expectError(); $r = $this->Article->find('all', array('contain' => array('Comment' => 'NonExistingBinding'))); }/** * testContain method * * @access public * @return void */ function testContain() { $this->Article->contain('Comment.User'); $r = $this->Article->find('all'); $this->assertTrue(Set::matches('/Comment/User', $r)); $this->assertFalse(Set::matches('/Comment/Article', $r)); $r = $this->Article->find('all'); $this->assertFalse(Set::matches('/Comment/User', $r)); }/** * testFindEmbeddedNoBindings method * * @access public * @return void */ function testFindEmbeddedNoBindings() { $result = $this->Article->find('all', array('contain' => false)); $expected = array( array('Article' => array( 'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31' )), array('Article' => array( 'id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31' )), array('Article' => array( 'id' => 3, 'user_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31' )) ); $this->assertEqual($result, $expected); }/** * testFindFirstLevel method * * @access public * @return void */ function testFindFirstLevel() { $this->Article->contain('User'); $result = $this->Article->find('all', array('recursive' => 1)); $expected = array( array( 'Article' => array( 'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31' ), 'User' => array( 'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' ) ), array( 'Article' => array( 'id' => 2, 'user_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31' ), 'User' => array( 'id' => 3, 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31' ) ), array( 'Article' => array( 'id' => 3, 'user_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31' ), 'User' => array( 'id' => 1, 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31' ) ) ); $this->assertEqual($result, $expected); $this->Article->contain('User', 'Comment'); $result = $this->Article->find('all', array('recursive' => 1)); $expected = array( array( 'Article' => array( 'id' => 1, 'user_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -