📄 model.test.php
字号:
$result = $TestModel->find('list', array('fields' => array('Post.title', 'Post.body'))); $expected = array('First Post' => 'First Post Body', 'Second Post' => 'Second Post Body', 'Third Post' => 'Third Post Body'); $this->assertEqual($result, $expected); $result = $TestModel->find('list', array('fields' => array('Post.id', 'Post.title', 'Author.user'), 'recursive' => 1)); $expected = array('mariano' => array(1 => 'First Post', 3 => 'Third Post'), 'larry' => array(2 => 'Second Post')); $this->assertEqual($result, $expected); $TestModel =& new User(); $result = $TestModel->find('list', array('fields' => array('User.user', 'User.password'))); $expected = array('mariano' => '5f4dcc3b5aa765d61d8327deb882cf99', 'nate' => '5f4dcc3b5aa765d61d8327deb882cf99', 'larry' => '5f4dcc3b5aa765d61d8327deb882cf99', 'garrett' => '5f4dcc3b5aa765d61d8327deb882cf99'); $this->assertEqual($result, $expected); $TestModel =& new ModifiedAuthor(); $result = $TestModel->find('list', array('fields' => array('Author.id', 'Author.user'))); $expected = array(1 => 'mariano (CakePHP)', 2 => 'nate (CakePHP)', 3 => 'larry (CakePHP)', 4 => 'garrett (CakePHP)'); $this->assertEqual($result, $expected); }/** * testRecordExists method * * @access public * @return void */ function testRecordExists() { $this->loadFixtures('User'); $TestModel =& new User(); $this->assertFalse($TestModel->exists()); $TestModel->read(null, 1); $this->assertTrue($TestModel->exists()); $TestModel->create(); $this->assertFalse($TestModel->exists()); $TestModel->id = 4; $this->assertTrue($TestModel->exists()); $TestModel =& new TheVoid(); $this->assertFalse($TestModel->exists()); $TestModel->id = 5; $this->assertFalse($TestModel->exists()); }/** * testFindField method * * @access public * @return void */ function testFindField() { $this->loadFixtures('User'); $TestModel =& new User(); $TestModel->id = 1; $result = $TestModel->field('user'); $this->assertEqual($result, 'mariano'); $result = $TestModel->field('User.user'); $this->assertEqual($result, 'mariano'); $TestModel->id = false; $result = $TestModel->field('user', array('user' => 'mariano')); $this->assertEqual($result, 'mariano'); $result = $TestModel->field('COUNT(*) AS count', true); $this->assertEqual($result, 4); $result = $TestModel->field('COUNT(*)', true); $this->assertEqual($result, 4); }/** * testFindUnique method * * @access public * @return void */ function testFindUnique() { $this->loadFixtures('User'); $TestModel =& new User(); $this->assertFalse($TestModel->isUnique(array('user' => 'nate'))); $TestModel->id = 2; $this->assertTrue($TestModel->isUnique(array('user' => 'nate'))); $this->assertFalse($TestModel->isUnique(array('user' => 'nate', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99'))); }/** * testUpdateExisting method * * @access public * @return void */ function testUpdateExisting() { $this->loadFixtures('User', 'Article', 'Comment'); $TestModel =& new User(); $TestModel->create(); $TestModel->save(array('User' => array('user' => 'some user', 'password' => 'some password'))); $this->assertTrue(is_int($TestModel->id) || (intval($TestModel->id) === 5)); $id = $TestModel->id; $TestModel->save(array('User' => array('user' => 'updated user'))); $this->assertEqual($TestModel->id, $id); $result = $TestModel->findById($id); $this->assertEqual($result['User']['user'], 'updated user'); $this->assertEqual($result['User']['password'], 'some password'); $Article =& new Article(); $Comment =& new Comment(); $data = array('Comment' => array('id' => 1, 'comment' => 'First Comment for First Article'), 'Article' => array('id' => 2, 'title' => 'Second Article')); $result = $Article->save($data); $this->assertTrue($result); $result = $Comment->save($data); $this->assertTrue($result); }/** * testUpdateMultiple method * * @access public * @return void */ function testUpdateMultiple() { $this->loadFixtures('Comment', 'Article', 'User', 'Attachment'); $TestModel =& new Comment(); $result = Set::extract($TestModel->find('all'), '{n}.Comment.user_id'); $expected = array('2', '4', '1', '1', '1', '2'); $this->assertEqual($result, $expected); $TestModel->updateAll(array('Comment.user_id' => 5), array('Comment.user_id' => 2)); $result = Set::combine($TestModel->find('all'), '{n}.Comment.id', '{n}.Comment.user_id'); $expected = array(1 => 5, 2 => 4, 3 => 1, 4 => 1, 5 => 1, 6 => 5); $this->assertEqual($result, $expected); }/** * testUpdateWithCalculation method * * @access public * @return void */ function testUpdateWithCalculation() { Configure::write('foo', true); $this->loadFixtures('DataTest'); $model =& new DataTest(); $result = $model->saveAll(array( array('count' => 5, 'float' => 1.1), array('count' => 3, 'float' => 1.2), array('count' => 4, 'float' => 1.3), array('count' => 1, 'float' => 2.0), )); $this->assertTrue($result); $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); $this->assertEqual($result, array(5, 3, 4, 1)); $this->assertTrue($model->updateAll(array('count' => 'count + 2'))); $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); $this->assertEqual($result, array(7, 5, 6, 3)); $this->assertTrue($model->updateAll(array('DataTest.count' => 'DataTest.count - 1'))); $result = Set::extract('/DataTest/count', $model->find('all', array('fields' => 'count'))); $this->assertEqual($result, array(6, 4, 5, 2)); Configure::write('foo', false); }/** * testBindUnbind method * * @access public * @return void */ function testBindUnbind() { $this->loadFixtures('User', 'Comment', 'FeatureSet'); $TestModel =& new User(); $result = $TestModel->hasMany; $expected = array(); $this->assertEqual($result, $expected); $result = $TestModel->bindModel(array('hasMany' => array('Comment'))); $this->assertTrue($result); $result = $TestModel->find('all', array('fields' => 'User.id, User.user')); $expected = array( array('User' => array('id' => '1', 'user' => 'mariano'), 'Comment' => array( array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'), array('id' => '4', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'), array('id' => '5', 'article_id' => '2', 'user_id' => '1', 'comment' => 'First Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'))), array('User' => array('id' => '2', 'user' => 'nate'), 'Comment' => array( array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'), array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31'))), array('User' => array('id' => '3', 'user' => 'larry'), 'Comment' => array()), array('User' => array('id' => '4', 'user' => 'garrett'), 'Comment' => array( array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31')))); $this->assertEqual($result, $expected); $TestModel->resetAssociations(); $result = $TestModel->hasMany; $this->assertEqual($result, array()); $result = $TestModel->bindModel(array('hasMany' => array('Comment')), false); $this->assertTrue($result); $result = $TestModel->find('all', array('fields' => 'User.id, User.user')); $expected = array( array('User' => array('id' => '1', 'user' => 'mariano'), 'Comment' => array( array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'), array('id' => '4', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'), array('id' => '5', 'article_id' => '2', 'user_id' => '1', 'comment' => 'First Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'))), array('User' => array('id' => '2', 'user' => 'nate'), 'Comment' => array( array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'), array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31'))), array('User' => array('id' => '3', 'user' => 'larry'), 'Comment' => array()), array('User' => array('id' => '4', 'user' => 'garrett'), 'Comment' => array( array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31')))); $this->assertEqual($result, $expected); $result = $TestModel->hasMany; $expected = array('Comment' => array('className' => 'Comment', 'foreignKey' => 'user_id', 'conditions' => null, 'fields' => null, 'order' => null, 'limit' => null, 'offset' => null, 'dependent' => null, 'exclusive' => null, 'finderQuery' => null, 'counterQuery' => null) ); $this->assertEqual($result, $expected); $result = $TestModel->unbindModel(array('hasMany' => array('Comment'))); $this->assertTrue($result); $result = $TestModel->hasMany; $expected = array(); $this->assertEqual($result, $expected); $result = $TestModel->find('all', array('fields' => 'User.id, User.user')); $expected = array( array('User' => array('id' => '1', 'user' => 'mariano')), array('User' => array('id' => '2', 'user' => 'nate')), array('User' => array('id' => '3', 'user' => 'larry')), array('User' => array('id' => '4', 'user' => 'garrett'))); $this->assertEqual($result, $expected); $result = $TestModel->findAll(null, 'User.id, User.user'); $expected = array( array('User' => array('id' => '1', 'user' => 'mariano'), 'Comment' => array( array('id' => '3', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'), array('id' => '4', 'article_id' => '1', 'user_id' => '1', 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'), array('id' => '5', 'article_id' => '2', 'user_id' => '1', 'comment' => 'First Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'))), array('User' => array('id' => '2', 'user' => 'nate'), 'Comment' => array( array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'), array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -