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}
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class SQLUpdateTest extends SapphireTest
|
|
|
|
{
|
|
|
|
public static $fixture_file = 'SQLUpdateTest.yml';
|
|
|
|
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
SQLUpdateTest\TestBase::class,
|
2024-03-19 23:49:57 +01:00
|
|
|
SQLUpdateTest\TestChild::class,
|
|
|
|
SQLUpdateTest\TestOther::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
public function testEmptyQueryReturnsNothing()
|
|
|
|
{
|
|
|
|
$query = new SQLUpdate();
|
|
|
|
$this->assertSQLEquals('', $query->sql($parameters));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicUpdate()
|
|
|
|
{
|
|
|
|
$query = SQLUpdate::create()
|
|
|
|
->setTable('"SQLUpdateTestBase"')
|
|
|
|
->assign('"Description"', 'Description 1a')
|
2020-04-20 19:58:09 +02:00
|
|
|
->addWhere(['"Title" = ?' => 'Object 1']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$sql = $query->sql($parameters);
|
|
|
|
|
|
|
|
// Check SQL
|
|
|
|
$this->assertSQLEquals('UPDATE "SQLUpdateTestBase" SET "Description" = ? WHERE ("Title" = ?)', $sql);
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->assertEquals(['Description 1a', 'Object 1'], $parameters);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Check affected rows
|
|
|
|
$query->execute();
|
|
|
|
$this->assertEquals(1, DB::affected_rows());
|
|
|
|
|
|
|
|
// Check item updated
|
2020-04-20 19:58:09 +02:00
|
|
|
$item = DataObject::get_one(SQLUpdateTest\TestBase::class, ['"Title"' => 'Object 1']);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals('Description 1a', $item->Description);
|
|
|
|
}
|
2024-03-19 23:49:57 +01:00
|
|
|
|
|
|
|
public function testUpdateWithJoin()
|
|
|
|
{
|
|
|
|
$query = SQLUpdate::create()
|
|
|
|
->setTable('"SQLUpdateTestBase"')
|
|
|
|
->assign('"SQLUpdateTestBase"."Description"', 'Description 2a')
|
|
|
|
->addInnerJoin('SQLUpdateTestOther', '"SQLUpdateTestOther"."Description" = "SQLUpdateTestBase"."Description"');
|
|
|
|
$sql = $query->sql($parameters);
|
|
|
|
|
|
|
|
// Check SQL
|
|
|
|
$this->assertSQLEquals('UPDATE "SQLUpdateTestBase" INNER JOIN "SQLUpdateTestOther" ON "SQLUpdateTestOther"."Description" = "SQLUpdateTestBase"."Description" SET "SQLUpdateTestBase"."Description" = ?', $sql);
|
|
|
|
$this->assertEquals(['Description 2a'], $parameters);
|
|
|
|
|
|
|
|
// Check affected rows
|
|
|
|
$query->execute();
|
|
|
|
$this->assertEquals(1, DB::affected_rows());
|
|
|
|
|
|
|
|
// Check item updated
|
|
|
|
$item = DataObject::get_one(SQLUpdateTest\TestBase::class, ['"Title"' => 'Object 2']);
|
|
|
|
$this->assertEquals('Description 2a', $item->Description);
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|