silverstripe-framework/tests/model/SQLUpdateTest.php
Damian Mooyman 8dd644d25d
API Namespace all classes
Namespace all templates
Move difflib and BBCodeParser2 to thirdparty
Remove deprecated API marked for removal in 4.0
2016-09-08 10:23:17 +12:00

64 lines
1.5 KiB
PHP

<?php
use SilverStripe\ORM\Queries\SQLUpdate;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
/**
* Tests for {@see SQLUpdate}
*
* @package framework
* @subpackage tests
*/
class SQLUpdateTest extends SapphireTest {
public static $fixture_file = 'SQLUpdateTest.yml';
protected $extraDataObjects = array(
'SQLUpdateTestBase',
'SQLUpdateChild'
);
public function testEmptyQueryReturnsNothing() {
$query = new SQLUpdate();
$this->assertSQLEquals('', $query->sql($parameters));
}
public function testBasicUpdate() {
$query = SQLUpdate::create()
->setTable('"SQLUpdateTestBase"')
->assign('"Description"', 'Description 1a')
->addWhere(array('"Title" = ?' => 'Object 1'));
$sql = $query->sql($parameters);
// Check SQL
$this->assertSQLEquals('UPDATE "SQLUpdateTestBase" SET "Description" = ? WHERE ("Title" = ?)', $sql);
$this->assertEquals(array('Description 1a', 'Object 1'), $parameters);
// Check affected rows
$query->execute();
$this->assertEquals(1, DB::affected_rows());
// Check item updated
$item = DataObject::get_one('SQLUpdateTestBase', array('"Title"' => 'Object 1'));
$this->assertEquals('Description 1a', $item->Description);
}
}
class SQLUpdateTestBase extends DataObject implements TestOnly {
private static $db = array(
'Title' => 'Varchar(255)',
'Description' => 'Text'
);
}
class SQLUpdateChild extends SQLUpdateTestBase {
private static $db = array(
'Details' => 'Varchar(255)'
);
}