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

XFailProcessStream   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 5
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A getLookupRecord() 0 3 1
1
<?php
2
3
namespace MapperNoRootTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_files\Interfaces;
8
use kalanis\kw_files_mapper\Processing\MapperNoRoot;
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 MapperNoRoot\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 MapperNoRoot\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 MapperNoRoot\ProcessFile(new XFailSaveRecord());
70
    }
71
72
    protected function getStreamLib(): Interfaces\IProcessFileStreams
73
    {
74
        return new MapperNoRoot\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 MapperNoRoot\ProcessStream(new XFailSaveRecord());
85
    }
86
87
    protected function getDirLib(): Interfaces\IProcessDirs
88
    {
89
        return new MapperNoRoot\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(12, $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
    }
116
117
    protected function dropTable(): string
118
    {
119
        return 'DROP TABLE IF EXISTS "my_local_data"';
120
    }
121
122
    protected function basicTable(): string
123
    {
124
        return 'CREATE TABLE IF NOT EXISTS "my_local_data" (
125
  "md_id" INTEGER PRIMARY KEY AUTOINCREMENT,
126
  "md_parent" INTEGER NULL,
127
  "md_name" TEXT NULL,
128
  "md_content" TEXT NULL,
129
  "md_created" INTEGER NULL
130
)';
131
    }
132
133
    protected function fillTable(): string
134
    {
135
        return 'INSERT INTO "my_local_data" ("md_id", "md_parent", "md_name", "md_content", "md_created") VALUES
136
-- ( 1, null, "",           "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", null), -- /data/tree
137
-- ( 2,  1,   "data",       "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
138
-- ( 3,  2,   "tree",       "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
139
( 4,  null,   "last_one",   "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
140
( 5,  4,   ".gitkeep",   "123456", 123456789),
141
( 6,  null,   "next_one",   "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
142
( 7,  6,   "sub_one",    "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
143
( 8,  7,   ".gitkeep",   "789123", 123456789),
144
( 9,  null,   "sub",        "' . Interfaces\IProcessNodes::STORAGE_NODE_KEY . '", 123456789),
145
(10,  9,   "dummy3.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789),
146
(11,  9,   "dummy4.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789),
147
(12,  null,   "dummy1.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789),
148
(13,  null,   "dummy2.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789),
149
(14,  null,   "other1.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789),
150
(20,  null,   "other2.txt", "qwertzuiopasdfghjklyxcvbnm0123456789", 123456789)
151
';
152
    }
153
}
154
155
156
/**
157
 * Class SQLiteTestRecord
158
 * @property int $id
159
 * @property int $parentId
160
 * @property string $name
161
 * @property string $content
162
 * @property SQLiteTestRecord[] $pars
163
 */
164
class SQLiteTestRecord extends ASimpleRecord
165
{
166
    protected function addEntries(): void
167
    {
168
        $this->addEntry('id', IEntryType::TYPE_INTEGER, 1024);
169
        $this->addEntry('parentId', IEntryType::TYPE_INTEGER, 1024);
170
        $this->addEntry('name', IEntryType::TYPE_STRING, 255);
171
        $this->addEntry('content', IEntryType::TYPE_STRING, PHP_INT_MAX);
172
        $this->addEntry('created', IEntryType::TYPE_INTEGER, 999999999);
173
        $this->addEntry('pars', IEntryType::TYPE_ARRAY); // FK - makes the array of entries every time
174
        $this->setMapper(SQLiteTestMapper::class);
175
    }
176
}
177
178
179
class SQLiteTestMapper extends ADatabase
180
{
181
    protected function setMap(): void
182
    {
183
        $this->setSource('test_sqlite_local');
184
        $this->setTable('my_local_data');
185
        $this->setRelation('id', 'md_id');
186
        $this->setRelation('parentId', 'md_parent');
187
        $this->setRelation('name', 'md_name');
188
        $this->setRelation('content', 'md_content');
189
        $this->setRelation('created', 'md_created');
190
        $this->addPrimaryKey('id');
191
        $this->addForeignKey('pars', SQLiteTestRecord::class, 'parentId', 'id');
192
    }
193
}
194
195
196
class XFailSaveRecord extends SQLiteTestRecord
197
{
198
    protected function addEntries(): void
199
    {
200
        parent::addEntries();
201
        $this->setMapper(SQLiteFailSaveTestMapper::class);
202
    }
203
}
204
205
206
class SQLiteFailSaveTestMapper extends SQLiteTestMapper
207
{
208
    protected function beforeSave(ARecord $record): bool
209
    {
210
        return false;
211
    }
212
}
213
214
215
class XFailProcessDir extends MapperNoRoot\ProcessDir
216
{
217
    protected function getLookupRecord(): ARecord
218
    {
219
        throw new MapperException('mock');
220
    }
221
}
222
223
224
class XFailProcessFile extends MapperNoRoot\ProcessFile
225
{
226
    protected function getLookupRecord(): ARecord
227
    {
228
        throw new MapperException('mock');
229
    }
230
}
231
232
233
class XFailProcessStream extends MapperNoRoot\ProcessStream
234
{
235
    protected function getLookupRecord(): ARecord
236
    {
237
        throw new MapperException('mock');
238
    }
239
}
240
241
242
class XFailProcessNode extends MapperNoRoot\ProcessNode
243
{
244
    protected function getLookupRecord(): ARecord
245
    {
246
        throw new MapperException('mock');
247
    }
248
}
249