Completed
Push — master ( a98c98...92b268 )
by Bartko
02:00
created

AdapterAbstract::testHandleNestedTransaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTreeTest\Integration\Adapter;
6
7
use StefanoTree\NestedSet\Adapter\AdapterInterface;
8
use StefanoTreeTest\IntegrationTestCase;
9
10
abstract class AdapterAbstract extends IntegrationTestCase
11
{
12
    /**
13
     * @var AdapterInterface
14
     */
15
    protected $adapter;
16
17
    protected $adapterCanQuoteIdentifier = true;
18
19
    protected function setUp()
20
    {
21
        $this->adapter = null;
22
        parent::setUp();
23
    }
24
25
    protected function tearDown()
26
    {
27
        $this->adapter = null;
28
        parent::tearDown();
29
    }
30
31
    /**
32
     * @return AdapterInterface
33
     */
34
    abstract protected function getAdapter(): AdapterInterface;
35
36
    protected function getDataSet()
37
    {
38
        return $this->createMySQLXMLDataSet(__DIR__.'/../_files/NestedSet/initDataSet.xml');
39
    }
40
41
    public function testIsInTransaction()
42
    {
43
        $adapter = $this->getAdapter();
44
45
        $this->assertFalse($adapter->isInTransaction());
46
        $adapter->beginTransaction();
47
        $this->assertTrue($adapter->isInTransaction());
48
        $adapter->rollbackTransaction();
49
    }
50
51
    public function testCommitTransaction()
52
    {
53
        $adapter = $this->getAdapter();
54
55
        $this->assertFalse($adapter->isInTransaction());
56
        $adapter->beginTransaction();
57
        $this->assertTrue($adapter->isInTransaction());
58
        $adapter->commitTransaction();
59
        $this->assertFalse($adapter->isInTransaction());
60
    }
61
62
    public function testRollbackTransaction()
63
    {
64
        $adapter = $this->getAdapter();
65
66
        $this->assertFalse($adapter->isInTransaction());
67
        $adapter->beginTransaction();
68
        $this->assertTrue($adapter->isInTransaction());
69
        $adapter->rollbackTransaction();
70
        $this->assertFalse($adapter->isInTransaction());
71
    }
72
73
    public function testHandleNestedTransaction()
74
    {
75
        $adapter = $this->getAdapter();
76
        if (!$adapter->canHandleNestedTransaction()) {
77
            $this->markTestSkipped('Adapter does not support nested transaction');
78
79
            return;
80
        }
81
        $adapter->beginTransaction();
82
        $this->assertTrue($adapter->isInTransaction());
83
        $adapter->beginTransaction();
84
        $adapter->commitTransaction();
85
        $this->assertTrue($adapter->isInTransaction());
86
        $adapter->commitTransaction();
87
        $this->assertFalse($adapter->isInTransaction());
88
    }
89
90
    public function testQuoteIdentifier()
91
    {
92
        $a = $this->getAdapter();
93
94
        if ($this->adapterCanQuoteIdentifier) {
95
            if ('pgsql' == TEST_STEFANO_DB_VENDOR) {
96
                $this->assertEquals(
97
                    '"simple"',
98
                    $a->quoteIdentifier('simple')
99
                );
100
101
                $this->assertEquals(
102
                    '"more"."complex"',
103
                    $a->quoteIdentifier('more.complex')
104
                );
105
            } else {
106
                $this->assertEquals(
107
                    '`simple`',
108
                    $a->quoteIdentifier('simple')
109
                );
110
111
                $this->assertEquals(
112
                    '`more`.`complex`',
113
                    $a->quoteIdentifier('more.complex')
114
                );
115
            }
116
        } else {
117
            $this->assertEquals(
118
                'more.complex',
119
                $a->quoteIdentifier('more.complex'));
120
        }
121
    }
122
123
    public function testExecuteInsertSQL()
124
    {
125
        $a = $this->getAdapter();
126
127
        $sql = 'INSERT INTO tree_traversal (name, lft, rgt, parent_id, level)'
128
            .' VALUES(:name, :lft, :rgt, :parent_id, :level)';
129
130
        $data = array(
131
            'name' => 'ahoj',
132
            'lft' => 123,
133
            'rgt' => 456,
134
            'parent_id' => 10,
135
            'level' => 789,
136
        );
137
138
        $id = $a->executeInsertSQL($sql, $data);
139
        $this->assertEquals(26, $id);
140
141
        $result = $a->executeSelectSQL('SELECT * FROM tree_traversal WHERE tree_traversal_id = 26');
142
        $this->assertEquals(
143
            array(
144
                0 => array_merge($data, array('tree_traversal_id' => 26)),
145
            ), $result);
146
    }
147
148
    public function testExecuteSQL()
149
    {
150
        $a = $this->getAdapter();
151
152
        $sql = 'UPDATE tree_traversal SET name = :name WHERE tree_traversal_id = :id';
153
154
        $data = array(
155
            'name' => 'testujeme parne valce',
156
            'id' => 10,
157
        );
158
159
        $a->executeSQL($sql, $data);
160
161
        $result = $a->executeSelectSQL(
162
            'SELECT tree_traversal_id AS id, name FROM tree_traversal WHERE tree_traversal_id = :id',
163
            array('id' => $data['id'])
164
        );
165
166
        $this->assertEquals(
167
            array(
168
                0 => $data,
169
            ), $result);
170
    }
171
}
172