db

Форк
0
/
UpdateTest.php 
89 строк · 2.9 Кб
1
<?php
2

3
namespace Upside\Db\Test\SQL;
4

5
use Upside\Db\SQL\Expression;
6

7
class UpdateTest extends BaseClass
8
{
9
    public function testUpdate()
10
    {
11
        $expected = 'UPDATE "users" SET "age" = 18';
12
        $actual = $this->db->update('users')->set(['age' => 18]);
13
        $this->assertEquals($expected, $actual);
14
    }
15

16
    public function testUpdateMultiple()
17
    {
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);
21
    }
22

23
    public function testUpdateColAsCol()
24
    {
25
        $expected = 'UPDATE "users" SET "name" = "username"';
26
        $actual = $this->db->update('users')->set([
27
            'name' => function (Expression $expr) {
28
                $expr->column("username");
29
            },
30
        ]);
31
        $this->assertEquals($expected, $actual);
32
    }
33

34
    public function testUpdateIncrementByOne()
35
    {
36
        $expected = 'UPDATE "users" SET "age" = "age" + 1';
37
        $actual = $this->db->update('users')->increment("age");
38
        $this->assertEquals($expected, $actual);
39
    }
40

41
    public function testUpdateIncrementMultipleByOne()
42
    {
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);
46
    }
47

48
    public function testUpdateIncrementByN()
49
    {
50
        $expected = 'UPDATE "users" SET "age" = "age" + 5';
51
        $actual = $this->db->update('users')->increment("age", 5);
52
        $this->assertEquals($expected, $actual);
53
    }
54

55
    public function testUpdateIncrementMultipleByN()
56
    {
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);
60
    }
61

62
    public function testUpdateDecrementByOne()
63
    {
64
        $expected = 'UPDATE "users" SET "age" = "age" - 1';
65
        $actual = $this->db->update('users')->decrement("age");
66
        $this->assertEquals($expected, $actual);
67
    }
68

69
    public function testUpdateDecrementMultipleByOne()
70
    {
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);
74
    }
75

76
    public function testUpdateDecrementByN()
77
    {
78
        $expected = 'UPDATE "users" SET "age" = "age" - 5';
79
        $actual = $this->db->update('users')->decrement("age", 5);
80
        $this->assertEquals($expected, $actual);
81
    }
82

83
    public function testUpdateDecrementMultipleByN()
84
    {
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);
88
    }
89
}
90

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.