Skip to content

Commit

Permalink
Fix serveral tests pass regardless of whether data is valid or not
Browse files Browse the repository at this point in the history
  • Loading branch information
chinpei215 committed Aug 2, 2014
1 parent 8e40fcf commit 3d77ce5
Showing 1 changed file with 136 additions and 44 deletions.
180 changes: 136 additions & 44 deletions lib/Cake/Test/Case/Model/ModelWriteTest.php
Expand Up @@ -4124,23 +4124,36 @@ public function testSaveAllHasManyValidation() {
public function testSaveAllManyRowsTransactionNoRollback() {
$this->loadFixtures('Post');

$db = $this->getMock('DboSource', array('begin', 'connect', 'rollback', 'describe'));
$Post = new TestPost();
$Post->validate = array(
'title' => array('rule' => array('notEmpty'))
);

$db->expects($this->once())
->method('describe')
->will($this->returnValue(array()));
// If validation error occurs, rollback() should be called.
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
$db->expects($this->never())->method('commit');
$db->expects($this->once())->method('rollback');

$Post = new TestPost();
$Post->setDataSourceObject($db);

$Post->validate = array(
'title' => array('rule' => array('notEmpty'))
$data = array(
array('author_id' => 1, 'title' => 'New Fourth Post'),
array('author_id' => 1, 'title' => '')
);
$Post->saveAll($data, array('atomic' => true, 'validate' => true));

// Otherwise, commit() should be called.
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
$db->expects($this->once())->method('commit');
$db->expects($this->never())->method('rollback');

$Post->setDataSourceObject($db);

$data = array(
array('author_id' => 1, 'title' => 'New Fourth Post'),
array('author_id' => 1, 'title' => '')
array('author_id' => 1, 'title' => 'New Fifth Post')
);
$Post->saveAll($data, array('atomic' => true, 'validate' => true));
}
Expand All @@ -4151,27 +4164,43 @@ public function testSaveAllManyRowsTransactionNoRollback() {
* @return void
*/
public function testSaveAllAssociatedTransactionNoRollback() {
$testDb = ConnectionManager::getDataSource('test');
$this->loadFixtures('Post', 'Author');

$db = $this->getMock('DboSource', array('connect', 'rollback', 'describe', 'create', 'update', 'begin'));
$db->columns = $testDb->columns;
$Post = new TestPost();
$Post->Author->validate = array(
'user' => array('rule' => array('notEmpty'))
);

// If validation error occurs, rollback() should be called.
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
$db->expects($this->never())->method('commit');
$db->expects($this->once())->method('rollback');
$db->expects($this->any())->method('describe')
->will($this->returnValue(array(
'id' => array('type' => 'integer', 'length' => 11),
'title' => array('type' => 'string'),
'body' => array('type' => 'text'),
'published' => array('type' => 'string')
)));

$Post = new TestPost();
$Post->setDataSourceObject($db);
$Post->Author->setDataSourceObject($db);

$Post->Author->validate = array(
'user' => array('rule' => array('notEmpty'))
$data = array(
'Post' => array(
'title' => 'New post',
'body' => 'Content',
'published' => 'Y'
),
'Author' => array(
'user' => '',
'password' => "sekret"
)
);
$Post->saveAll($data, array('validate' => true));

// Otherwise, commit() should be called.
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
$db->expects($this->once())->method('commit');
$db->expects($this->never())->method('rollback');

$Post->setDataSourceObject($db);
$Post->Author->setDataSourceObject($db);

$data = array(
'Post' => array(
Expand All @@ -4180,7 +4209,7 @@ public function testSaveAllAssociatedTransactionNoRollback() {
'published' => 'Y'
),
'Author' => array(
'user' => '',
'user' => 'New user',
'password' => "sekret"
)
);
Expand Down Expand Up @@ -5557,23 +5586,36 @@ public function testSaveAssociatedHasManyValidation() {
public function testSaveManyTransactionNoRollback() {
$this->loadFixtures('Post');

$db = $this->getMock('DboSource', array('begin', 'connect', 'rollback', 'describe'));
$Post = new TestPost();
$Post->validate = array(
'title' => array('rule' => array('notEmpty'))
);

$db->expects($this->once())
->method('describe')
->will($this->returnValue(array()));
// If validation error occurs, rollback() should be called.
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
$db->expects($this->never())->method('commit');
$db->expects($this->once())->method('rollback');

$Post = new TestPost();
$Post->setDataSourceObject($db);

$Post->validate = array(
'title' => array('rule' => array('notEmpty'))
$data = array(
array('author_id' => 1, 'title' => 'New Fourth Post'),
array('author_id' => 1, 'title' => '')
);
$Post->saveMany($data, array('validate' => true));

// Otherwise, commit() should be called.
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
$db->expects($this->once())->method('commit');
$db->expects($this->never())->method('rollback');

$Post->setDataSourceObject($db);

$data = array(
array('author_id' => 1, 'title' => 'New Fourth Post'),
array('author_id' => 1, 'title' => '')
array('author_id' => 1, 'title' => 'New Fifth Post')
);
$Post->saveMany($data, array('validate' => true));
}
Expand All @@ -5584,27 +5626,43 @@ public function testSaveManyTransactionNoRollback() {
* @return void
*/
public function testSaveAssociatedTransactionNoRollback() {
$testDb = ConnectionManager::getDataSource('test');
$this->loadFixtures('Post', 'Author');

$db = $this->getMock('DboSource', array('connect', 'rollback', 'describe', 'create', 'begin'));
$db->columns = $testDb->columns;
$Post = new TestPost();
$Post->Author->validate = array(
'user' => array('rule' => array('notEmpty'))
);

// If validation error occurs, rollback() should be called.
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
$db->expects($this->never())->method('commit');
$db->expects($this->once())->method('rollback');
$db->expects($this->any())->method('describe')
->will($this->returnValue(array(
'id' => array('type' => 'integer', 'length' => 11),
'title' => array('type' => 'string'),
'body' => array('type' => 'text'),
'published' => array('type' => 'string')
)));

$Post = new TestPost();
$Post->setDataSourceObject($db);
$Post->Author->setDataSourceObject($db);

$Post->Author->validate = array(
'user' => array('rule' => array('notEmpty'))
$data = array(
'Post' => array(
'title' => 'New post',
'body' => 'Content',
'published' => 'Y'
),
'Author' => array(
'user' => '',
'password' => "sekret"
)
);
$Post->saveAssociated($data, array('validate' => true, 'atomic' => true));

// Otherwise, commit() should be called.
$db = $this->_getMockDboSource(array('begin', 'commit', 'rollback'));
$db->expects($this->once())->method('begin')->will($this->returnValue(true));
$db->expects($this->once())->method('commit');
$db->expects($this->never())->method('rollback');

$Post->setDataSourceObject($db);
$Post->Author->setDataSourceObject($db);

$data = array(
'Post' => array(
Expand All @@ -5613,7 +5671,7 @@ public function testSaveAssociatedTransactionNoRollback() {
'published' => 'Y'
),
'Author' => array(
'user' => '',
'user' => 'New user',
'password' => "sekret"
)
);
Expand Down Expand Up @@ -7315,4 +7373,38 @@ public function deleteMe($event) {
$Model = $event->subject;
$Model->getDataSource()->delete($Model, array($Model->alias . '.' . $Model->primaryKey => $Model->id));
}

/**
* Creates a convenient mock DboSource
*
* We cannot call several methods via mock DboSource, such as DboSource::value()
* because mock DboSource has no $_connection.
* This method helps us to avoid this problem.
*
* @param array $methods Configurable method names.
* @return DboSource
*/
protected function _getMockDboSource($methods = array()) {
$testDb = ConnectionManager::getDataSource('test');

$passthrough = array_diff(array('value', 'begin', 'rollback', 'commit', 'describe', 'lastInsertId', 'execute'), $methods);

$methods = array_merge($methods, $passthrough);
if (!in_array('connect', $methods)) {
$methods[] = 'connect'; // This will be called by DboSource::__construct().
}

$db = $this->getMock('DboSource', $methods);
$db->columns = $testDb->columns;
$db->startQuote = $testDb->startQuote;
$db->endQuote = $testDb->endQuote;

foreach ($passthrough as $method) {
$db->expects($this->any())
->method($method)
->will($this->returnCallback(array($testDb, $method)));
}

return $db;
}
}

0 comments on commit 3d77ce5

Please sign in to comment.