Passed
Push — master ( 785627...2140a0 )
by Petr
08:13
created

AStorageTest::getStreamLib()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace MapperTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_files\Interfaces;
8
use kalanis\kw_files_mapper\Processing\Mapper;
9
use kalanis\kw_mapper\Interfaces\IDriverSources;
10
use kalanis\kw_mapper\Interfaces\IEntryType;
11
use kalanis\kw_mapper\MapperException;
12
use kalanis\kw_mapper\Mappers\Database\ADatabase;
13
use kalanis\kw_mapper\Records\ARecord;
14
use kalanis\kw_mapper\Records\ASimpleRecord;
15
use kalanis\kw_mapper\Storage\Database;
16
use PDO;
17
18
19
abstract class AStorageTest extends CommonTestClass
20
{
21
    protected ?Database\PDO\SQLite $database = null;
22
    protected bool $skipIt = false;
23
24
    /**
25
     * @throws MapperException
26
     */
27
    protected function setUp(): void
28
    {
29
        $skipIt = getenv('SQSKIP');
30
        $this->skipIt = false !== $skipIt && boolval(intval(strval($skipIt)));
31
32
        $conf = Database\Config::init()->setTarget(
33
            IDriverSources::TYPE_PDO_SQLITE,
34
            'test_sqlite_local',
35
            ':memory:',
36
            0,
37
            null,
38
            null,
39
            ''
40
        );
41
        $conf->setParams(86000, true);
42
        Database\ConfigStorage::getInstance()->addConfig($conf);
43
        $this->database = Database\DatabaseSingleton::getInstance()->getDatabase($conf);
44
        $this->database->addAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
0 ignored issues
show
Bug introduced by
The method addAttribute() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        $this->database->/** @scrutinizer ignore-call */ 
45
                         addAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
    }
46
47
    protected function getNodeLib(): Interfaces\IProcessNodes
48
    {
49
        return new Mapper\ProcessNode(new SQLiteTestRecord());
50
    }
51
52
    protected function getNodeFailLib(): Interfaces\IProcessNodes
53
    {
54
        return new XFailProcessNode(new SQLiteTestRecord());
55
    }
56
57
    protected function getFileLib(): Interfaces\IProcessFiles
58
    {
59
        return new Mapper\ProcessFile(new SQLiteTestRecord());
60
    }
61
62
    protected function getFileFailLib(): Interfaces\IProcessFiles
63
    {
64
        return new XFailProcessFile(new SQLiteTestRecord());
65
    }
66
67
    protected function getFileFailRecLib(): Interfaces\IProcessFiles
68
    {
69
        return new Mapper\ProcessFile(new XFailSaveRecord());
70
    }
71
72
    protected function getStreamLib(): Interfaces\IProcessFileStreams
73
    {
74
        return new Mapper\ProcessStream(new SQLiteTestRecord());
75
    }
76
77
    protected function getStreamFailLib(): Interfaces\IProcessFileStreams
78
    {
79
        return new XFailProcessStream(new SQLiteTestRecord());
80
    }
81
82
    protected function getStreamFailRecLib(): Interfaces\IProcessFileStreams
83
    {
84
        return new Mapper\ProcessStream(new XFailSaveRecord());
85
    }
86
87
    protected function getDirLib(): Interfaces\IProcessDirs
88
    {
89
        return new Mapper\ProcessDir(new SQLiteTestRecord());
90
    }
91
92
    protected function getDirFailLib(): Interfaces\IProcessDirs
93
    {
94
        return new XFailProcessDir(new SQLiteTestRecord());
95
    }
96
97
    /**
98
     * @throws MapperException
99
     */
100
    protected function dataRefill(): void
101
    {
102
        $this->assertTrue($this->database->exec($this->dropTable(), []));
103
        $this->assertTrue($this->database->exec($this->basicTable(), []));
104
        $this->assertTrue($this->database->exec($this->fillTable(), []));
105
        $this->assertEquals(13, $this->database->rowCount());
106
    }
107
108
    /**
109
     * @throws MapperException
110
     */
111
    protected function dataClear(): void
112
    {
113
        $this->assertTrue($this->database->exec($this->dropTable(), []));
114
        $this->assertTrue($this->database->exec($this->basicTable(), []));
115
        $this->assertTrue($this->database->exec($this->fillTable2(), []));
116
        $this->assertEquals(1, $this->database->rowCount());
117
    }
118
119
    protected function dropTable(): string
120
    {
121
        return 'DROP TABLE IF EXISTS "my_local_data"';
122
    }
123
124
    protected function basicTable(): string
125
    {
126
        return 'CREATE TABLE IF NOT EXISTS "my_local_data" (
127
  "md_id" INTEGER PRIMARY KEY AUTOINCREMENT,
128
  "md_parent" INTEGER NULL,
129
  "md_name" TEXT NULL,
130
  "md_content" TEXT NULL,
131
  "md_created" INTEGER NULL
132
)';
133
    }
134
135
    protected function fillTable(): string
136
    {
137
        return 'INSERT INTO "my_local_data" ("md_id", "md_parent", "md_name", "md_content", "md_created") VALUES
138
( 1, null, "",           "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", null), -- /data/tree
139
-- ( 2,  1,   "data",       "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
140
-- ( 3,  2,   "tree",       "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
141
( 4,  1,   "last_one",   "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
142
( 5,  4,   ".gitkeep",   "123456", 123456789),
143
( 6,  1,   "next_one",   "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
144
( 7,  6,   "sub_one",    "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
145
( 8,  7,   ".gitkeep",   "789123", 123456789),
146
( 9,  1,   "sub",        "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
147
(10,  9,   "dummy3.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789),
148
(11,  9,   "dummy4.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789),
149
(12,  1,   "dummy1.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789),
150
(13,  1,   "dummy2.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789),
151
(14,  1,   "other1.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789),
152
(20,  1,   "other2.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789)
153
';
154
    }
155
156
    protected function fillTable2(): string
157
    {
158
        return 'INSERT INTO "my_local_data" ("md_id", "md_parent", "md_name", "md_content", "md_created") VALUES
159
( 1, null, "",           "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", null) -- /data/tree
160
';
161
    }
162
}
163
164
165
/**
166
 * Class SQLiteTestRecord
167
 * @property int $id
168
 * @property int $parentId
169
 * @property string $name
170
 * @property string $content
171
 * @property SQLiteTestRecord[] $pars
172
 */
173
class SQLiteTestRecord extends ASimpleRecord
174
{
175
    protected function addEntries(): void
176
    {
177
        $this->addEntry('id', IEntryType::TYPE_INTEGER, 1024);
178
        $this->addEntry('parentId', IEntryType::TYPE_INTEGER, 1024);
179
        $this->addEntry('name', IEntryType::TYPE_STRING, 255);
180
        $this->addEntry('content', IEntryType::TYPE_STRING, PHP_INT_MAX);
181
        $this->addEntry('created', IEntryType::TYPE_INTEGER, 999999999);
182
        $this->addEntry('pars', IEntryType::TYPE_ARRAY); // FK - makes the array of entries every time
183
        $this->setMapper(SQLiteTestMapper::class);
184
    }
185
}
186
187
188
class SQLiteTestMapper extends ADatabase
189
{
190
    protected function setMap(): void
191
    {
192
        $this->setSource('test_sqlite_local');
193
        $this->setTable('my_local_data');
194
        $this->setRelation('id', 'md_id');
195
        $this->setRelation('parentId', 'md_parent');
196
        $this->setRelation('name', 'md_name');
197
        $this->setRelation('content', 'md_content');
198
        $this->setRelation('created', 'md_created');
199
        $this->addPrimaryKey('id');
200
        $this->addForeignKey('pars', SQLiteTestRecord::class, 'parentId', 'id');
201
    }
202
}
203
204
205
class XFailSaveRecord extends SQLiteTestRecord
206
{
207
    protected function addEntries(): void
208
    {
209
        parent::addEntries();
210
        $this->setMapper(SQLiteFailSaveTestMapper::class);
211
    }
212
}
213
214
215
class SQLiteFailSaveTestMapper extends SQLiteTestMapper
216
{
217
    protected function beforeSave(ARecord $record): bool
218
    {
219
        return false;
220
    }
221
}
222
223
224
class XFailProcessDir extends Mapper\ProcessDir
225
{
226
    protected function getLookupRecord(): ARecord
227
    {
228
        throw new MapperException('mock');
229
    }
230
}
231
232
233
class XFailProcessFile extends Mapper\ProcessFile
234
{
235
    protected function getLookupRecord(): ARecord
236
    {
237
        throw new MapperException('mock');
238
    }
239
}
240
241
242
class XFailProcessStream extends Mapper\ProcessStream
243
{
244
    protected function getLookupRecord(): ARecord
245
    {
246
        throw new MapperException('mock');
247
    }
248
}
249
250
251
class XFailProcessNode extends Mapper\ProcessNode
252
{
253
    protected function getLookupRecord(): ARecord
254
    {
255
        throw new MapperException('mock');
256
    }
257
}
258