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

MySqlTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Importance

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