Passed
Push — master ( 9ccabb...39bdf1 )
by Petr
07:56
created

XTestMapper   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 13
rs 10
wmc 1
1
<?php
2
3
namespace KwTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_mapper\Interfaces\IDriverSources;
8
use kalanis\kw_mapper\Interfaces\IEntryType;
9
use kalanis\kw_mapper\MapperException;
10
use kalanis\kw_mapper\Mappers\Database\ADatabase;
11
use kalanis\kw_mapper\Records\ARecord;
12
use kalanis\kw_mapper\Records\ASimpleRecord;
13
use kalanis\kw_mapper\Storage\Database\Config;
14
use kalanis\kw_mapper\Storage\Database\ConfigStorage;
15
use kalanis\kw_mapper\Storage\Database\DatabaseSingleton;
16
use kalanis\kw_mapper\Storage\Database\PDO\SQLite;
17
use PDO;
18
19
20
abstract class AKwTests extends CommonTestClass
21
{
22
    /** @var null|SQLite */
23
    protected $database = null;
24
    protected $filled = false;
25
26
    protected function loadedRec($id): ARecord
27
    {
28
        $this->dataRefill();
29
        $rec = new XTestRecord();
30
        $rec->id = $id;
31
        $rec->load();
32
        return $rec;
33
    }
34
35
    /**
36
     * @param bool $force
37
     * @throws MapperException
38
     */
39
    protected function dataRefill($force = false): void
40
    {
41
        if (!$this->filled || $force) {
42
            $conf = Config::init()->setTarget(
43
                IDriverSources::TYPE_PDO_SQLITE,
44
                'test_sqlite_local',
45
                ':memory:',
46
                0,
47
                null,
48
                null,
49
                ''
50
            );
51
            $conf->setParams(12000, true);
52
            ConfigStorage::getInstance()->addConfig($conf);
53
            $this->database = DatabaseSingleton::getInstance()->getDatabase($conf);
54
            $this->database->addAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
55
            $this->database->reconnect();
56
            $this->assertTrue($this->database->exec($this->dropTable(), []));
57
            $this->assertTrue($this->database->exec($this->basicTable(), []));
58
            $this->assertTrue($this->database->exec($this->fillTable(), []));
59
            $this->assertEquals(9, $this->database->rowCount());
60
            $this->filled = true;
61
        }
62
    }
63
64
    protected function dropTable(): string
65
    {
66
        return 'DROP TABLE IF EXISTS "x_testing_rows"';
67
    }
68
69
    protected function basicTable(): string
70
    {
71
        return 'CREATE TABLE IF NOT EXISTS "x_testing_rows" (
72
  "xtr_id" INT AUTO_INCREMENT NOT NULL PRIMARY KEY ,
73
  "xtr_name" VARCHAR(20) NULL,
74
  "xtr_target" VARCHAR(20) NULL,
75
  "xtr_counter" INT(10) NULL,
76
  "xtr_flight" INT(1) NULL,
77
  "xtr_enabled" INT(1) NULL
78
)';
79
    }
80
81
    protected function fillTable(): string
82
    {
83
        return 'INSERT INTO "x_testing_rows" ("xtr_id", "xtr_name", "xtr_target", "xtr_counter", "xtr_flight", "xtr_enabled") VALUES
84
(1, "dave", "any", 123, 0, 1),
85
(2, "john", "one", 456, 0, 0),
86
(3, "emil", "any", 789, 1, 1),
87
(4, "josh", "any", 101, 1, 0),
88
(5, "ewan", "one", 112, 0, 0),
89
(6, "kami", "any", 131, 1, 0),
90
(7, "chuck", "one", 415, 0, 1),
91
(8, "phil", "any", 161, 1, 1),
92
(9, "wayne", "any", 718, 0, 0)
93
';
94
    }
95
}
96
97
98
/**
99
 * Class XTestRecord
100
 * @property int id
101
 * @property string name
102
 * @property string target
103
 * @property int counter
104
 * @property int flight
105
 * @property int enabled
106
 */
107
class XTestRecord extends ASimpleRecord
108
{
109
    protected function addEntries(): void
110
    {
111
        $this->addEntry('id', IEntryType::TYPE_INTEGER, 64);
112
        $this->addEntry('name', IEntryType::TYPE_STRING, 10);
113
        $this->addEntry('target', IEntryType::TYPE_SET, ['one', 'any']);
114
        $this->addEntry('counter', IEntryType::TYPE_INTEGER, 9999);
115
        $this->addEntry('flight', IEntryType::TYPE_INTEGER, 2);
116
        $this->addEntry('enabled', IEntryType::TYPE_INTEGER, 2);
117
        $this->setMapper(XTestMapper::class);
118
    }
119
120
    public function getName(): string
121
    {
122
        return (string) $this->offsetGet('name');
123
    }
124
}
125
126
127
class XTestMapper extends ADatabase
128
{
129
    protected function setMap(): void
130
    {
131
        $this->setSource('test_sqlite_local');
132
        $this->setTable('x_testing_rows');
133
        $this->setRelation('id', 'xtr_id');
134
        $this->setRelation('name', 'xtr_name');
135
        $this->setRelation('target', 'xtr_target');
136
        $this->setRelation('counter', 'xtr_counter');
137
        $this->setRelation('flight', 'xtr_flight');
138
        $this->setRelation('enabled', 'xtr_enabled');
139
        $this->addPrimaryKey('id');
140
    }
141
}
142