silverstripe-framework/tests/model/SQLUpdateTest.php

57 lines
1.4 KiB
PHP
Raw Normal View History

<?php
/**
* Tests for {@see SQLUpdate}
2014-08-15 08:53:05 +02:00
*
* @package framework
* @subpackage tests
*/
class SQLUpdateTest extends SapphireTest {
2014-08-15 08:53:05 +02:00
public static $fixture_file = 'SQLUpdateTest.yml';
protected $extraDataObjects = array(
'SQLUpdateTestBase',
'SQLUpdateChild'
);
2014-08-15 08:53:05 +02:00
public function testEmptyQueryReturnsNothing() {
$query = new SQLUpdate();
$this->assertSQLEquals('', $query->sql($parameters));
}
2014-08-15 08:53:05 +02:00
public function testBasicUpdate() {
$query = SQLUpdate::create()
->setTable('"SQLUpdateTestBase"')
->assign('"Description"', 'Description 1a')
->addWhere(array('"Title" = ?' => 'Object 1'));
$sql = $query->sql($parameters);
2014-08-15 08:53:05 +02:00
// Check SQL
$this->assertSQLEquals('UPDATE "SQLUpdateTestBase" SET "Description" = ? WHERE ("Title" = ?)', $sql);
$this->assertEquals(array('Description 1a', 'Object 1'), $parameters);
2014-08-15 08:53:05 +02:00
// Check affected rows
$query->execute();
$this->assertEquals(1, DB::affected_rows());
2014-08-15 08:53:05 +02:00
// 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)'
);
}