Test Failed
Push — master ( 50dc03...2f2ddd )
by Petr
02:45
created

SQLiteTestMapper   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 9
c 1
b 0
f 0
dl 0
loc 12
rs 10
1
<?php
2
3
namespace StorageTests\Database\Connect;
4
5
6
use Builder;
7
use CommonTestClass;
8
use kalanis\kw_mapper\Interfaces\IDriverSources;
9
use kalanis\kw_mapper\Interfaces\IEntryType;
10
use kalanis\kw_mapper\Interfaces\IQueryBuilder;
11
use kalanis\kw_mapper\MapperException;
12
use kalanis\kw_mapper\Mappers\Database\ADatabase;
13
use kalanis\kw_mapper\Records\ASimpleRecord;
14
use kalanis\kw_mapper\Search\Search;
15
use kalanis\kw_mapper\Storage\Database\Config;
16
use kalanis\kw_mapper\Storage\Database\ConfigStorage;
17
use kalanis\kw_mapper\Storage\Database\DatabaseSingleton;
18
use kalanis\kw_mapper\Storage\Database\Dialects;
19
use kalanis\kw_mapper\Storage\Database\PDO\SQLite;
20
use PDO;
21
22
23
/**
24
 * Class SqLiteTest
25
 * @package StorageTests\Database\Connect
26
 * @requires extension PDO
27
 * @requires extension pdo_sqlite
28
 */
29
class SqLiteTest extends CommonTestClass
30
{
31
    /** @var null|SQLite */
32
    protected $database = null;
33
34
    /**
35
     * @throws MapperException
36
     */
37
    protected function setUp(): void
38
    {
39
        $conf = Config::init()->setTarget(
40
                    IDriverSources::TYPE_PDO_SQLITE,
41
                    'test_sqlite_local',
42
                    ':memory:',
43
                    0,
44
                    null,
45
                    null,
46
                    ''
47
                );
48
        $conf->setParams(86000, true);
49
        ConfigStorage::getInstance()->addConfig($conf);
50
        $this->database = DatabaseSingleton::getInstance()->getDatabase($conf);
51
        $this->database->addAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
52
    }
53
54
    /**
55
     * @throws MapperException
56
     * Beware - SQLite cannot do more than one query at time - so the semicolons are unnecessary
57
     */
58
    public function testProcess(): void
59
    {
60
        $this->database->reconnect();
61
        $this->assertFalse($this->database->exec('', []));
62
        $this->database->reconnect();
63
        $this->assertEmpty($this->database->query('', []));
64
65
        $this->dataRefill();
66
67
        $query = new Builder();
68
        $query->setBaseTable('d_queued_commands');
69
        $sql = new Dialects\SQLite();
70
        $result = $this->database->query($sql->describe($query), []);
71
//var_dump($result);
72
        $this->assertNotEmpty($result, 'There MUST be table from file!');
73
74
        $query->addColumn('d_queued_commands', 'qc_id');
75
        $query->addColumn('d_queued_commands', 'qc_status');
76
        $lines = $this->database->query($sql->select($query), $query->getParams());
77
        $this->assertEquals(6, count($lines));
78
//var_dump(['full dump' => $lines]);
79
        $query->addCondition('d_queued_commands', 'qc_time_start', IQueryBuilder::OPERATION_EQ, 123456);
80
//var_dump(['query dump' => str_split($sql->select($query), 120)]);
81
        $lines = $this->database->query($sql->select($query), $query->getParams());
82
        $this->assertEquals(5, count($lines));
83
84
        $this->assertTrue($this->database->beginTransaction());
85
        $this->database->exec('INSERT INTO `d_queued_commands` (qc_id, qc_time_start, qc_time_end, qc_status, qc_command) VALUES (11, 123456, 123456789, 13, "ls -laf");', []);
86
        $this->assertTrue($this->database->commit());
87
        $this->assertNotEmpty($this->database->lastInsertId(), 'There must be last id!');
88
        $this->assertEquals(1, $this->database->rowCount());
89
        $this->assertTrue($this->database->beginTransaction());
90
        $this->database->exec('INSERT INTO `d_queued_commands` (qc_id, qc_time_start, qc_time_end, qc_status, qc_command) VALUES (12, 1234567, 123456789, 13, "ls -laf");', []);
91
        $this->assertTrue($this->database->rollBack());
92
93
        $lines = $this->database->query($sql->select($query), $query->getParams());
94
        $this->assertEquals(6, count($lines));
95
    }
96
97
    /**
98
     * @throws MapperException
99
     */
100
    public function testMapped(): void
101
    {
102
        $this->dataRefill();
103
104
        // now queries - search
105
        $search = new Search(new SQLiteTestRecord());
106
        $search->like('command', '%laf%');
107
        $this->assertEquals(4, $search->getCount());
108
109
        $records = $search->getResults();
110
        $this->assertEquals(4, count($records));
111
112
        /** @var SQLiteTestRecord $record */
113
        $record = reset($records);
114
        $this->assertEquals(5, $record->id);
115
        $this->assertEquals(123456, $record->timeStart);
116
        $this->assertEquals(12345678, $record->timeEnd);
117
        $this->assertEquals(5, $record->status);
118
119
        $search2 = new Search(new SQLiteTestRecord());
120
        $search2->exact('status', 55);
121
        $this->assertEquals(0, $search2->getCount());
122
        $this->assertEquals(0, count($search2->getResults()));
123
    }
124
125
    /**
126
     * @throws MapperException
127
     */
128
    public function testCrud(): void
129
    {
130
        $this->dataRefill();
131
132
        // create
133
        $rec1 = new SQLiteTestRecord();
134
        $rec1->id = 14;
135
        $rec1->timeStart = 12345;
136
        $rec1->timeEnd = 1234567;
137
        $rec1->status = 8;
138
        $this->assertTrue($rec1->save(true));
139
140
        // read
141
        $rec2 = new SQLiteTestRecord();
142
        $rec2->status = 8;
143
        $this->assertEquals(1, count($rec2->loadMultiple()));
144
145
        $this->assertTrue($rec2->load());
146
        $this->assertEquals(12345, $rec2->timeStart);
147
        $this->assertEquals(1234567, $rec2->timeEnd);
148
        // update
149
        $rec2->status = 9;
150
        $this->assertTrue($rec2->save());
151
152
        $rec3 = new SQLiteTestRecord();
153
        $rec3->status = 8;
154
        $this->assertEquals(0, $rec3->count());
155
156
        $rec4 = new SQLiteTestRecord();
157
        $rec4->id = 6;
158
        $this->assertTrue($rec4->load());
159
        $this->assertEquals(1234567, $rec4->timeStart);
160
        $this->assertEquals(12345678, $rec4->timeEnd);
161
162
        // delete
163
        $rec5 = new SQLiteTestRecord();
164
        $rec5->status = 9;
165
        $this->assertTrue($rec5->delete());
166
167
        // bulk update - for now via ugly hack
168
        $rec6 = new SQLiteTestRecord();
169
        $rec6->getEntry('status')->setData(5, true); // hack to set condition
170
        $rec6->timeEnd = 123; // this will be updated
171
        $rec6->getMapper()->update($rec6); // todo: another hack, change rules for insert/update in future
172
    }
173
174
    /**
175
     * @throws MapperException
176
     */
177
    protected function dataRefill(): void
178
    {
179
        $this->assertTrue($this->database->exec($this->dropTable(), []));
180
        $this->assertTrue($this->database->exec($this->basicTable(), []));
181
        $this->assertTrue($this->database->exec($this->fillTable(), []));
182
        $this->assertEquals(6, $this->database->rowCount());
183
    }
184
185
    protected function dropTable(): string
186
    {
187
        return 'DROP TABLE IF EXISTS "d_queued_commands"';
188
    }
189
190
    protected function basicTable(): string
191
    {
192
        return 'CREATE TABLE IF NOT EXISTS "d_queued_commands" (
193
  "qc_id" INT AUTO_INCREMENT NOT NULL PRIMARY KEY ,
194
  "qc_time_start" VARCHAR(20) NULL,
195
  "qc_time_end" VARCHAR(20) NULL,
196
  "qc_status" INT(1) NULL,
197
  "qc_command" TEXT NULL
198
)';
199
    }
200
201
    protected function fillTable(): string
202
    {
203
        return 'INSERT INTO "d_queued_commands" ("qc_id", "qc_time_start", "qc_time_end", "qc_status", "qc_command") VALUES
204
( 5, 123456,  12345678,  5, "ls -laf"),
205
( 6, 1234567, 12345678,  5, "ls -laf"),
206
( 7, 123456,  12345678, 11, "ls -laf"),
207
( 8, 123456,  12345678, 11, "ls -laf"),
208
( 9, 123456,  12345678, 12, "ls -alF"),
209
(10, 123456,  12345678, 14, null)
210
';
211
    }
212
}
213
214
215
/**
216
 * Class SQLiteTestRecord
217
 * @property int id
218
 * @property int timeStart
219
 * @property int timeEnd
220
 * @property int status
221
 * @property string command
222
 */
223
class SQLiteTestRecord extends ASimpleRecord
224
{
225
    protected function addEntries(): void
226
    {
227
        $this->addEntry('id', IEntryType::TYPE_INTEGER, 64);
228
        $this->addEntry('timeStart', IEntryType::TYPE_INTEGER, 99999999);
229
        $this->addEntry('timeEnd', IEntryType::TYPE_INTEGER, 99999999);
230
        $this->addEntry('status', IEntryType::TYPE_INTEGER, 64);
231
        $this->addEntry('command', IEntryType::TYPE_STRING, 250);
232
        $this->setMapper('\StorageTests\Database\Connect\SQLiteTestMapper');
233
    }
234
}
235
236
237
class SQLiteTestMapper extends ADatabase
238
{
239
    protected function setMap(): void
240
    {
241
        $this->setSource('test_sqlite_local');
242
        $this->setTable('d_queued_commands');
243
        $this->setRelation('id', 'qc_id');
244
        $this->setRelation('timeStart', 'qc_time_start');
245
        $this->setRelation('timeEnd', 'qc_time_end');
246
        $this->setRelation('status', 'qc_status');
247
        $this->setRelation('command', 'qc_command');
248
        $this->addPrimaryKey('id');
249
    }
250
}
251