Skip to content

Commit

Permalink
Moving tests and un-skipping them.
Browse files Browse the repository at this point in the history
Adding tests for proper field quoting in conditions.
Closes #2369
  • Loading branch information
markstory committed Dec 14, 2011
1 parent dd335cb commit 8d1edd7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 39 deletions.
58 changes: 58 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
Expand Up @@ -3461,4 +3461,62 @@ public function testExceptionOnBrokenConnection() {
'database' => 'imaginary'
));
}

/**
* testStatements method
*
* @return void
*/
public function testUpdateStatements() {
$this->loadFixtures('Article', 'User');
$test = ConnectionManager::getDatasource('test');

$this->Dbo = $this->getMock('Mysql', array('execute'), array($test->config));

$this->Dbo->expects($this->at(0))->method('execute')
->with("UPDATE `articles` SET `field1` = 'value1' WHERE 1 = 1");

$this->Dbo->expects($this->at(1))->method('execute')
->with("UPDATE `articles` AS `Article` LEFT JOIN `users` AS `User` ON (`Article`.`user_id` = `User`.`id`)" .
" SET `Article`.`field1` = 2 WHERE 2=2");

$this->Dbo->expects($this->at(2))->method('execute')
->with("UPDATE `articles` AS `Article` LEFT JOIN `users` AS `User` ON (`Article`.`user_id` = `User`.`id`)" .
" SET `Article`.`field1` = 'value' WHERE `index` = 'val'");

$Article = new Article();

$this->Dbo->update($Article, array('field1'), array('value1'));
$this->Dbo->update($Article, array('field1'), array('2'), '2=2');
$this->Dbo->update($Article, array('field1'), array("'value'"), array('index' => 'val'));

}

/**
* Test deletes with a mock.
*
* @return void
*/
public function testDeleteStatements() {
$this->loadFixtures('Article', 'User');
$test = ConnectionManager::getDatasource('test');

$this->Dbo = $this->getMock('Mysql', array('execute'), array($test->config));

$this->Dbo->expects($this->at(0))->method('execute')
->with("DELETE FROM `articles` WHERE 1 = 1");

$this->Dbo->expects($this->at(1))->method('execute')
->with("DELETE `Article` FROM `articles` AS `Article` LEFT JOIN `users` AS `User` ON (`Article`.`user_id` = `User`.`id`)" .
" WHERE 1 = 1");

$this->Dbo->expects($this->at(2))->method('execute')
->with("DELETE `Article` FROM `articles` AS `Article` LEFT JOIN `users` AS `User` ON (`Article`.`user_id` = `User`.`id`)" .
" WHERE 2=2");
$Article = new Article();

$this->Dbo->delete($Article);
$this->Dbo->delete($Article, true);
$this->Dbo->delete($Article, '2=2');
}
}
39 changes: 0 additions & 39 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -740,45 +740,6 @@ public function testFieldsUsingMethodCache() {
$this->assertTrue(empty(DboTestSource::$methodCache['fields']), 'Cache not empty');
}

/**
* testStatements method
*
* @return void
*/
public function testStatements() {
$this->skipIf(!$this->testDb instanceof DboMysql);

$this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'Attachment', 'ArticlesTag');
$Article = new Article();

$result = $this->testDb->update($Article, array('field1'), array('value1'));
$this->assertFalse($result);
$result = $this->testDb->getLastQuery();
$this->assertRegExp('/^\s*UPDATE\s+' . $this->testDb->fullTableName('articles') . '\s+SET\s+`field1`\s*=\s*\'value1\'\s+WHERE\s+1 = 1\s*$/', $result);

$result = $this->testDb->update($Article, array('field1'), array('2'), '2=2');
$this->assertFalse($result);
$result = $this->testDb->getLastQuery();
$this->assertRegExp('/^\s*UPDATE\s+' . $this->testDb->fullTableName('articles') . ' AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+SET\s+`Article`\.`field1`\s*=\s*2\s+WHERE\s+2\s*=\s*2\s*$/', $result);

$result = $this->testDb->delete($Article);
$this->assertTrue($result);
$result = $this->testDb->getLastQuery();
$this->assertRegExp('/^\s*DELETE\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+WHERE\s+1 = 1\s*$/', $result);

$result = $this->testDb->delete($Article, true);
$this->assertTrue($result);
$result = $this->testDb->getLastQuery();
$this->assertRegExp('/^\s*DELETE\s+`Article`\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+WHERE\s+1\s*=\s*1\s*$/', $result);

$result = $this->testDb->delete($Article, '2=2');
$this->assertTrue($result);
$result = $this->testDb->getLastQuery();
$this->assertRegExp('/^\s*DELETE\s+`Article`\s+FROM\s+' . $this->testDb->fullTableName('articles') . '\s+AS `Article`\s+LEFT JOIN\s+' . $this->testDb->fullTableName('users') . ' AS `User` ON \(`Article`.`user_id` = `User`.`id`\)\s+WHERE\s+2\s*=\s*2\s*$/', $result);

$result = $this->testDb->hasAny($Article, '1=2');
$this->assertFalse($result);
}

/**
* Test that group works without a model
Expand Down

0 comments on commit 8d1edd7

Please sign in to comment.