2013-06-21 00:32:08 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
|
|
|
use SilverStripe\ORM\Connect\DBQueryBuilder;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Queries\SQLInsert;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\ORM\Tests\SQLInsertTest\SQLInsertTestBase;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for {@see SQLInsert}
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class SQLInsertTest extends SapphireTest
|
|
|
|
{
|
2013-06-21 00:32:08 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
SQLInsertTestBase::class
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testEmptyQueryReturnsNothing()
|
|
|
|
{
|
|
|
|
$query = new SQLInsert();
|
|
|
|
$this->assertSQLEquals('', $query->sql($parameters));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
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::class) {
|
|
|
|
$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());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Check inserted object is correct
|
|
|
|
$firstObject = DataObject::get_one(SQLInsertTestBase::class, 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');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
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::class) {
|
|
|
|
$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());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
// Check inserted objects are correct
|
|
|
|
$firstObject = DataObject::get_one(SQLInsertTestBase::class, 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');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$secondObject = DataObject::get_one(SQLInsertTestBase::class, array('"Title"' => 'Second object'), false);
|
|
|
|
$this->assertNotEmpty($secondObject);
|
|
|
|
$this->assertEquals($secondObject->Title, 'Second object');
|
|
|
|
$this->assertEquals($secondObject->Age, 12);
|
|
|
|
$this->assertEmpty($secondObject->Description);
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|