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

Translate   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 12
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 4
c 2
b 0
f 0
dl 0
loc 12
rs 10
1
<?php
2
3
namespace MappersTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_mapper\Interfaces\IEntryType;
8
use kalanis\kw_mapper\MapperException;
9
use kalanis\kw_mapper\Mappers;
10
use kalanis\kw_mapper\Storage;
11
12
13
class TraitTest extends CommonTestClass
14
{
15
    /**
16
     * @throws MapperException
17
     */
18
    public function testContentOk(): void
19
    {
20
        $data = new Content();
21
        $data->setContentKey('lkjhgfd');
22
        $this->assertEquals('lkjhgfd', $data->getContentKey());
23
    }
24
25
    /**
26
     * @throws MapperException
27
     */
28
    public function testContentFail(): void
29
    {
30
        $data = new Content();
31
        $this->expectException(MapperException::class);
32
        $data->getContentKey();
33
    }
34
35
    /**
36
     * @param mixed $want
37
     * @param int $type
38
     * @param mixed $input
39
     * @dataProvider typesFromProvider
40
     */
41
    public function testTypesFrom($want, int $type, $input): void
42
    {
43
        $data = new Translate();
44
        $this->assertEquals($want, $data->from($type, $input));
45
    }
46
47
    public function typesFromProvider(): array
48
    {
49
        return [
50
            [false, IEntryType::TYPE_BOOLEAN, 0],
51
            [false, IEntryType::TYPE_BOOLEAN, ''],
52
            [true, IEntryType::TYPE_BOOLEAN, 7],
53
            [true, IEntryType::TYPE_BOOLEAN, '3'],
54
            [15, IEntryType::TYPE_INTEGER, 15.3],
55
            [4358, IEntryType::TYPE_INTEGER, '4358'],
56
            [18.8, IEntryType::TYPE_FLOAT, '18.8'],
57
            [18.8, IEntryType::TYPE_FLOAT, '18.8'],
58
            [['foo', 'bar'], IEntryType::TYPE_ARRAY, 'a:2:{i:0;s:3:"foo";i:1;s:3:"bar";}'],
59
            ['lkjhgdf', IEntryType::TYPE_STRING, 'lkjhgdf'],
60
        ];
61
    }
62
63
    /**
64
     * @param $want
65
     * @param int $type
66
     * @param $input
67
     * @dataProvider typesToProvider
68
     */
69
    public function testTypesTo($want, int $type, $input): void
70
    {
71
        $data = new Translate();
72
        $this->assertEquals($want, $data->to($type, $input));
73
    }
74
75
    public function typesToProvider(): array
76
    {
77
        return [
78
            ['0', IEntryType::TYPE_BOOLEAN, false],
79
            ['1', IEntryType::TYPE_BOOLEAN, true],
80
            ['15', IEntryType::TYPE_INTEGER, 15],
81
            ['18.8', IEntryType::TYPE_FLOAT, 18.8],
82
            ['a:2:{i:0;s:3:"foo";i:1;s:3:"bar";}', IEntryType::TYPE_ARRAY, ['foo', 'bar']],
83
            ['lkjhgdf', IEntryType::TYPE_STRING, 'lkjhgdf'],
84
        ];
85
    }
86
87
    public function testPks(): void
88
    {
89
        $pk = new Pk();
90
        $pk->addPrimaryKey('foo');
91
        $pk->addPrimaryKey('bar');
92
        $contains = $pk->getPrimaryKeys();
93
        $this->assertEquals(2, count($contains));
94
        $this->assertEquals('foo', reset($contains));
95
        $this->assertEquals('bar', next($contains));
96
97
        $this->assertTrue($pk->filterPrimary('baz', 'bar'));
98
        $this->assertFalse($pk->filterPrimary('uhb', 'nhz'));
99
        $this->assertFalse($pk->filterPrimary('uhb', ''));
100
    }
101
102
    public function testFks(): void
103
    {
104
        $fk = new Fk();
105
        $fk->addForeignKey('foo', '\Record', 'local', 'remote');
106
        $fk->addForeignKey('bar', '\Record', 'baz', 'remote');
107
        $contains = $fk->getForeignKeys();
108
        $this->assertEquals(2, count($contains));
109
        $entry = reset($contains);
110
        $this->assertEquals('foo', $entry->getLocalAlias());
111
        $this->assertEquals('\Record', $entry->getRemoteRecord());
112
        $this->assertEquals('local', $entry->getLocalEntryKey());
113
        $this->assertEquals('remote', $entry->getRemoteEntryKey());
114
        $entry = next($contains);
115
        $this->assertEquals('bar', $entry->getLocalAlias());
116
        $this->assertEquals('\Record', $entry->getRemoteRecord());
117
        $this->assertEquals('baz', $entry->getLocalEntryKey());
118
        $this->assertEquals('remote', $entry->getRemoteEntryKey());
119
    }
120
121
    public function testRelations(): void
122
    {
123
        $relations = new Relations();
124
        $relations->setRelation('foo', 'local');
125
        $relations->setRelation('bar', 'baz');
126
        $contains = $relations->getRelations();
127
        $this->assertEquals(2, count($contains));
128
        $this->assertEquals('local', reset($contains));
129
        $this->assertEquals('foo', key($contains));
130
        $this->assertEquals('baz', next($contains));
131
        $this->assertEquals('bar', key($contains));
132
    }
133
134
    public function testSource(): void
135
    {
136
        $source = new Source();
137
        $source->setSource('foo');
138
        $this->assertEquals('foo', $source->getSource());
139
        $source->setSource('bar');
140
        $this->assertEquals('bar', $source->getSource());
141
    }
142
143
    public function testDbTable(): void
144
    {
145
        $source = new Table();
146
        $source->setTable('foo');
147
        $this->assertEquals('foo', $source->getTable());
148
        $source->setTable('bar');
149
        $this->assertEquals('bar', $source->getTable());
150
    }
151
}
152
153
154
class Content
155
{
156
    use Mappers\File\TContent;
157
}
158
159
160
class Translate
161
{
162
    use Mappers\File\TTranslate;
163
164
    public function from(int $type, $content)
165
    {
166
        return $this->translateTypeFrom($type, $content);
167
    }
168
169
    public function to(int $type, $content)
170
    {
171
        return $this->translateTypeTo($type, $content);
172
    }
173
}
174
175
176
class Table
177
{
178
    use Mappers\Database\TTable;
179
}
180
181
182
class Pk
183
{
184
    use Mappers\TPrimaryKey;
185
}
186
187
188
class Fk
189
{
190
    use Mappers\TForeignKey;
191
}
192
193
194
class Relations
195
{
196
    use Mappers\TRelations;
197
}
198
199
200
class Source
201
{
202
    use Mappers\TSource;
203
}
204