2013-06-21 00:32:08 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\ORM\Tests;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Queries\SQLUpdate;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for {@see SQLUpdate}
|
|
|
|
*/
|
|
|
|
class SQLUpdateTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
public static $fixture_file = 'SQLUpdateTest.yml';
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
SQLUpdateTest\TestBase::class,
|
|
|
|
SQLUpdateTest\TestChild::class
|
2013-06-21 00:32:08 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
public function testEmptyQueryReturnsNothing() {
|
|
|
|
$query = new SQLUpdate();
|
|
|
|
$this->assertSQLEquals('', $query->sql($parameters));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +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
|
|
|
|
2013-06-21 00:32:08 +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
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Check affected rows
|
|
|
|
$query->execute();
|
|
|
|
$this->assertEquals(1, DB::affected_rows());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Check item updated
|
2016-10-14 03:30:05 +02:00
|
|
|
$item = DataObject::get_one(SQLUpdateTest\TestBase::class, array('"Title"' => 'Object 1'));
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertEquals('Description 1a', $item->Description);
|
|
|
|
}
|
|
|
|
}
|