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

PostgresTest::setUp()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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