ChangesTest::testChangesCreateWithFilter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 21
rs 9.7666
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace TBolier\RethinkQL\IntegrationTest\Operation;
5
6
use TBolier\RethinkQL\Response\Cursor;
7
use TBolier\RethinkQL\Response\ResponseInterface;
8
9
class ChangesTest extends AbstractTableTest
10
{
11
    /**
12
     * @throws \Exception
13
     */
14
    public function testChangesCreate(): void
15
    {
16
        /** @var Cursor $feed */
17
        $feed = $this->r()
18
            ->table('tabletest')
19
            ->changes()
20
            ->run();
21
22
        $this->insertDocument(1);
23
        $this->insertDocument(2);
24
        $this->insertDocument(3);
25
        $this->insertDocument(4);
26
        $this->insertDocument(5);
27
28
        /** @var ResponseInterface $res */
29
        $i = 1;
30
        foreach ($feed as $change) {
31
            $old_val = $change['old_val'];
32
            $new_val = $change['new_val'];
33
34
            $this->assertEmpty($old_val);
35
            $this->assertEquals($i, $new_val['id']);
36
37
            if ($i === 5) {
38
                break;
39
            }
40
41
            $i++;
42
        }
43
    }
44
45
    /**
46
     * @throws \Exception
47
     */
48
    public function testChangesUpdates(): void
49
    {
50
        /** @var Cursor $feed */
51
        $feed = $this->r()
52
            ->table('tabletest')
53
            ->changes()
54
            ->run();
55
56
        $this->insertDocument(777);
57
58
        $this->r()->table('tabletest')->filter(['id' => 777])->update(['description' => 'cool!'])->run();
59
60
        $i = 777;
61
        foreach ($feed as $change) {
62
            $old_val = $change['old_val'];
63
            $new_val = $change['new_val'];
64
65
            if ($old_val !== null) {
66
                $this->assertEquals('A document description.', $old_val['description']);
67
                $this->assertEquals('cool!', $new_val['description']);
68
69
                break;
70
            }
71
72
            $this->assertEmpty($old_val);
73
            $this->assertEquals($i, $new_val['id']);
74
        }
75
    }
76
77
    /**
78
     * @throws \Exception
79
     */
80
    public function testChangesWithOptions(): void
81
    {
82
        /** @var Cursor $feed */
83
        $feed = $this->r()
84
            ->table('tabletest')
85
            ->changes(['squash' => true])
86
            ->run();
87
88
        $this->insertDocument(1);
89
        $this->r()->table('tabletest')->filter(['id' => 1])->update(['description' => 'cool!'])->run();
90
91
        $change = $feed->current();
92
        $old_val = $change['old_val'];
93
        $new_val = $change['new_val'];
94
95
        $this->assertEmpty($old_val);
96
        $this->assertEquals(1, $new_val['id']);
97
        $this->assertEquals('cool!', $new_val['description']);
98
    }
99
100
    /**
101
     * @throws \Exception
102
     */
103
    public function testChangesCreateWithFilter(): void
104
    {
105
        /** @var Cursor $feed */
106
        $feed = $this->r()
107
            ->table('tabletest')
108
            ->filter(['id' => 4])
109
            ->changes()
110
            ->run();
111
112
        $this->insertDocument(1);
113
        $this->insertDocument(2);
114
        $this->insertDocument(3);
115
        $this->insertDocument(4);
116
        $this->insertDocument(5);
117
118
        $change = $feed->current();
119
        $old_val = $change['old_val'];
120
        $new_val = $change['new_val'];
121
122
        $this->assertEmpty($old_val);
123
        $this->assertEquals(4, $new_val['id']);
124
    }
125
}
126