3
namespace Upside\Db\Test\SQL;
5
use Upside\Db\SQL\Expression;
7
class UpdateTest extends BaseClass
9
public function testUpdate()
11
$expected = 'UPDATE "users" SET "age" = 18';
12
$actual = $this->db->update('users')->set(['age' => 18]);
13
$this->assertEquals($expected, $actual);
16
public function testUpdateMultiple()
18
$expected = 'UPDATE "users" SET "age" = 18, "name" = \'foo\'';
19
$actual = $this->db->update('users')->set(['age' => 18, 'name' => 'foo']);
20
$this->assertEquals($expected, $actual);
23
public function testUpdateColAsCol()
25
$expected = 'UPDATE "users" SET "name" = "username"';
26
$actual = $this->db->update('users')->set([
27
'name' => function (Expression $expr) {
28
$expr->column("username");
31
$this->assertEquals($expected, $actual);
34
public function testUpdateIncrementByOne()
36
$expected = 'UPDATE "users" SET "age" = "age" + 1';
37
$actual = $this->db->update('users')->increment("age");
38
$this->assertEquals($expected, $actual);
41
public function testUpdateIncrementMultipleByOne()
43
$expected = 'UPDATE "users" SET "age" = "age" + 1, "foo" = "foo" + 1';
44
$actual = $this->db->update('users')->increment(["age", "foo"]);
45
$this->assertEquals($expected, $actual);
48
public function testUpdateIncrementByN()
50
$expected = 'UPDATE "users" SET "age" = "age" + 5';
51
$actual = $this->db->update('users')->increment("age", 5);
52
$this->assertEquals($expected, $actual);
55
public function testUpdateIncrementMultipleByN()
57
$expected = 'UPDATE "users" SET "age" = "age" + 5, "foo" = "foo" + 5';
58
$actual = $this->db->update('users')->increment(["age", "foo"], 5);
59
$this->assertEquals($expected, $actual);
62
public function testUpdateDecrementByOne()
64
$expected = 'UPDATE "users" SET "age" = "age" - 1';
65
$actual = $this->db->update('users')->decrement("age");
66
$this->assertEquals($expected, $actual);
69
public function testUpdateDecrementMultipleByOne()
71
$expected = 'UPDATE "users" SET "age" = "age" - 1, "foo" = "foo" - 1';
72
$actual = $this->db->update('users')->decrement(["age", "foo"]);
73
$this->assertEquals($expected, $actual);
76
public function testUpdateDecrementByN()
78
$expected = 'UPDATE "users" SET "age" = "age" - 5';
79
$actual = $this->db->update('users')->decrement("age", 5);
80
$this->assertEquals($expected, $actual);
83
public function testUpdateDecrementMultipleByN()
85
$expected = 'UPDATE "users" SET "age" = "age" - 5, "foo" = "foo" - 5';
86
$actual = $this->db->update('users')->decrement(["age", "foo"], 5);
87
$this->assertEquals($expected, $actual);