xref: /plugin/struct/_test/ConfigParserTest.php (revision b360e3b5f0dd65e1a784310a1ef841ecb24beeab)
1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta;
6
7/**
8 * Tests for parsing the aggregation config for the struct plugin
9 *
10 * @group plugin_struct
11 * @group plugins
12 *
13 */
14class ConfigParserTest extends StructTest
15{
16
17    public function test_simple()
18    {
19        $lines = [
20            "schema    : testtable, another, foo bar",
21            "cols      : %pageid%, count",
22            "sort      : ^count",
23            "sort      : %pageid%, ^bam",
24            "align     : r,l,center,foo"
25        ];
26
27        $configParser = new meta\ConfigParser($lines);
28        $actual_config = $configParser->getConfig();
29
30        $expected_config = [
31            'limit' => 0,
32            'dynfilters' => false,
33            'summarize' => false,
34            'rownumbers' => false,
35            'sepbyheaders' => false,
36            'headers' =>
37                [
38                    0 => NULL,
39                    1 => NULL,
40                ],
41            'widths' =>
42                [],
43            'filter' =>
44                [],
45            'schemas' =>
46                [
47                    0 =>
48                        [
49                            0 => 'testtable',
50                            1 => '',
51                        ],
52                    1 =>
53                        [
54                            0 => 'another',
55                            1 => '',
56                        ],
57                    2 =>
58                        [
59                            0 => 'foo',
60                            1 => 'bar',
61                        ],
62                ],
63            'cols' =>
64                [
65                    0 => '%pageid%',
66                    1 => 'count',
67                ],
68            'sort' =>
69                [
70                    [
71                        0 => 'count',
72                        1 => false,
73                    ],
74                    [
75                        0 => '%pageid%',
76                        1 => true,
77                    ],
78                    [
79                        0 => 'bam',
80                        1 => false,
81                    ]
82                ],
83            'csv' => true,
84            'target' => '',
85            'align' => ['right', 'left', 'center', null]
86        ];
87
88        $this->assertEquals($expected_config, $actual_config);
89    }
90
91    public function test_width()
92    {
93        $lines = ['width: 5, 15px, 23.4em, meh, 10em'];
94
95        $configParser = new meta\ConfigParser($lines);
96
97        $config = $configParser->getConfig();
98
99        $this->assertEquals(
100            ['5px', '15px', '23.4em', '', '10em'],
101            $config['widths']
102        );
103    }
104
105    /**
106     * @see test_splitLine
107     */
108    public function provide_splitLine()
109    {
110        return [
111            ['', ['', '']],
112            ['   ', ['', '']],
113            ['foo', ['foo', '']],
114            ['foo:bar', ['foo', 'bar']],
115            ['foo: bar', ['foo', 'bar']],
116            ['fOO: bar', ['foo', 'bar']],
117            ['  fOO: bar  ', ['foo', 'bar']],
118        ];
119    }
120
121    /**
122     * @dataProvider provide_splitLine
123     */
124    public function test_splitLine($line, $expected)
125    {
126        $configParser = new meta\ConfigParser(array());
127        $actual = $this->callInaccessibleMethod($configParser, 'splitLine', [$line]);
128        $this->assertEquals($expected, $actual);
129    }
130}
131