silverstripe-framework/tests/model/SQLInsertTest.php
Damian Mooyman d8e9af8af8 API New Database abstraction layer. Ticket #7429
Database abstraction broken up into controller, connector, query builder, and schema manager, each independently configurable via YAML / Injector
Creation of new DBQueryGenerator for database specific generation of SQL
Support for parameterised queries, move of code base to use these over escaped conditions
Refactor of SQLQuery into separate query classes for each of INSERT UPDATE DELETE and SELECT
Support for PDO
Installation process upgraded to use new ORM
SS_DatabaseException created to handle database errors, maintaining details of raw sql and parameter details for user code designed interested in that data.
Renamed DB static methods to conform correctly to naming conventions (e.g. DB::getConn -> DB::get_conn)
3.2 upgrade docs
Performance Optimisation and simplification of code to use more concise API
API Ability for database adapters to register extensions to ConfigureFromEnv.php
2014-07-09 18:04:05 +12:00

95 lines
3.2 KiB
PHP

<?php
/**
* Tests for {@see SQLInsert}
*
* @package framework
* @subpackage tests
*/
class SQLInsertTest extends SapphireTest {
protected $extraDataObjects = array(
'SQLInsertTestBase'
);
public function testEmptyQueryReturnsNothing() {
$query = new SQLInsert();
$this->assertSQLEquals('', $query->sql($parameters));
}
public function testBasicInsert() {
$query = SQLInsert::create()
->setInto('"SQLInsertTestBase"')
->assign('"Title"', 'My Object')
->assign('"HasFun"', 1)
->assign('"Age"', 10)
->assign('"Description"', 'No description');
$sql = $query->sql($parameters);
// Only test this case if using the default query builder
if(get_class(DB::get_conn()->getQueryBuilder()) === 'DBQueryBuilder') {
$this->assertSQLEquals(
'INSERT INTO "SQLInsertTestBase" ("Title", "HasFun", "Age", "Description") VALUES (?, ?, ?, ?)',
$sql
);
}
$this->assertEquals(array('My Object', 1, 10, 'No description'), $parameters);
$query->execute();
$this->assertEquals(1, DB::affected_rows());
// Check inserted object is correct
$firstObject = DataObject::get_one('SQLInsertTestBase', array('"Title"' => 'My Object'), false);
$this->assertNotEmpty($firstObject);
$this->assertEquals($firstObject->Title, 'My Object');
$this->assertNotEmpty($firstObject->HasFun);
$this->assertEquals($firstObject->Age, 10);
$this->assertEquals($firstObject->Description, 'No description');
}
public function testMultipleRowInsert() {
$query = SQLInsert::create('"SQLInsertTestBase"');
$query->addRow(array(
'"Title"' => 'First Object',
'"Age"' => 10, // Can't insert non-null values into only one row in a multi-row insert
'"Description"' => 'First the worst' // Nullable field, can be present in some rows
));
$query->addRow(array(
'"Title"' => 'Second object',
'"Age"' => 12
));
$sql = $query->sql($parameters);
// Only test this case if using the default query builder
if(get_class(DB::get_conn()->getQueryBuilder()) === 'DBQueryBuilder') {
$this->assertSQLEquals(
'INSERT INTO "SQLInsertTestBase" ("Title", "Age", "Description") VALUES (?, ?, ?), (?, ?, ?)',
$sql
);
}
$this->assertEquals(array('First Object', 10, 'First the worst', 'Second object', 12, null), $parameters);
$query->execute();
$this->assertEquals(2, DB::affected_rows());
// Check inserted objects are correct
$firstObject = DataObject::get_one('SQLInsertTestBase', array('"Title"' => 'First Object'), false);
$this->assertNotEmpty($firstObject);
$this->assertEquals($firstObject->Title, 'First Object');
$this->assertEquals($firstObject->Age, 10);
$this->assertEquals($firstObject->Description, 'First the worst');
$secondObject = DataObject::get_one('SQLInsertTestBase', array('"Title"' => 'Second object'), false);
$this->assertNotEmpty($secondObject);
$this->assertEquals($secondObject->Title, 'Second object');
$this->assertEquals($secondObject->Age, 12);
$this->assertEmpty($secondObject->Description);
}
}
class SQLInsertTestBase extends DataObject implements TestOnly {
private static $db = array(
'Title' => 'Varchar(255)',
'HasFun' => 'Boolean',
'Age' => 'Int',
'Description' => 'Text',
);
}