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

FileFormatsTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 54
c 1
b 0
f 0
dl 0
loc 119
rs 10
1
<?php
2
3
namespace StorageTests\Files;
4
5
6
use CommonTestClass;
7
use kalanis\kw_mapper\Interfaces\IEntryType;
8
use kalanis\kw_mapper\MapperException;
9
use kalanis\kw_mapper\Storage\File\Formats;
10
11
12
class FileFormatsTest extends CommonTestClass
13
{
14
    /**
15
     * @throws MapperException
16
     */
17
    public function testFactoryNoClass(): void
18
    {
19
        $factory = Formats\Factory::getInstance();
20
        $this->expectException(MapperException::class);
21
        $factory->getFormatClass('undefined');
22
    }
23
24
    /**
25
     * @throws MapperException
26
     */
27
    public function testFactoryWrongClass(): void
28
    {
29
        $factory = Formats\Factory::getInstance();
30
        $this->expectException(MapperException::class);
31
        $factory->getFormatClass('\kalanis\kw_mapper\Adapters\MappedStdClass');
32
    }
33
34
    /**
35
     * @throws MapperException
36
     */
37
    public function testFactoryRun(): void
38
    {
39
        $factory = Formats\Factory::getInstance();
40
        $className = '\kalanis\kw_mapper\Storage\File\Formats\SinglePage';
41
        $class = $factory->getFormatClass($className);
42
        $this->assertInstanceOf('\kalanis\kw_mapper\Interfaces\IFileFormat', $class);
43
        $this->assertEquals($class, $factory->getFormatClass($className));
44
    }
45
46
    /**
47
     * @throws MapperException
48
     */
49
    public function testSinglePage(): void
50
    {
51
        $data = ['foo-bar-baz-anf-bvt-xcu-xdh'];
52
        $format = new Formats\SinglePage();
53
        $this->assertEquals($data, $format->unpack($format->pack($data)));
54
    }
55
56
    /**
57
     * @throws MapperException
58
     */
59
    public function testSeparated(): void
60
    {
61
        $data = $this->typesToProvider();
62
        $format = new Formats\SeparatedElements();
63
        $format->setDelimiters('#');
64
        $this->assertEquals([
65
            ['0', '1', '', ''],
66
            ['1', '1', '1', ''],
67
            ['10', '4', '', ''],
68
            ['15', '2', '15', ''],
69
            ['18.8', '3', '18.8', ''],
70
            ['lkjhgdf', '4', 'lkjhgdf', ''],
71
        ], $format->unpack($format->pack($data). PHP_EOL . PHP_EOL));
72
    }
73
74
    /**
75
     * @throws MapperException
76
     * @requires extension yaml
77
     * @requires function yaml_parse
78
     * @requires function yaml_emit
79
     * -- markTestSkipped()
80
     */
81
    public function testYaml(): void
82
    {
83
        $data = $this->typesToProvider();
84
        $format = new Formats\Yaml();
85
        $this->assertEquals($data, $format->unpack($format->pack($data)));
86
        $this->expectException(MapperException::class);
87
        $format->unpack("\e\tasdfghjkl\t\e\r\n\r\nasdfgjkl");
88
    }
89
90
    /**
91
     * @throws MapperException
92
     * @requires function parse_ini_string
93
     * -- markTestSkipped()
94
     */
95
    public function testIni(): void
96
    {
97
        $data = $this->typesToProvider();
98
        $format = new Formats\Ini();
99
        $this->assertEquals($data, $format->unpack($format->pack($data)));
100
        $this->expectException(MapperException::class);
101
        $format->unpack("\e\tasdfghjkl\t\e\r\n\r\nasdfgjkl?{}|&~![()^");
102
    }
103
104
    /**
105
     * @throws MapperException
106
     */
107
    public function testCsv(): void
108
    {
109
        $data = $this->typesToProvider();
110
        $format = new Formats\Csv();
111
        $format->setDelimiters(PHP_EOL);
112
        $this->assertEquals([
113
            ['0', '1', '', ''],
114
            ['1', '1', '1', ''],
115
            ['10', '4', '', ''],
116
            ['15', '2', '15', ''],
117
            ['18.8', '3', '18.8', ''],
118
            ['lkjhgdf', '4', 'lkjhgdf', ''],
119
        ], $format->unpack($format->pack($data)));
120
    }
121
122
    public function typesToProvider(): array
123
    {
124
        return [
125
            'obj1' => ['a' => '0', 'b' => IEntryType::TYPE_BOOLEAN, 'c' => false],
126
            'obj2' => ['a' => '1', 'b' => IEntryType::TYPE_BOOLEAN, 'c' => true],
127
            'obj3' => ['a' => '10', 'b' => IEntryType::TYPE_STRING, 'c' => null],
128
            'obj4' => ['a' => '15', 'b' => IEntryType::TYPE_INTEGER, 'c' => 15],
129
            'obj5' => ['a' => '18.8', 'b' => IEntryType::TYPE_FLOAT, 'c' => 18.8],
130
            'obj6' => ['a' => 'lkjhgdf', 'b' => IEntryType::TYPE_STRING, 'c' => 'lkjhgdf'],
131
        ];
132
    }
133
}
134