1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\Value;
6use dokuwiki\plugin\struct\test\mock\Search;
7use dokuwiki\plugin\struct\types\Decimal;
8
9/**
10 * Testing the Decimal Type
11 *
12 * @group plugin_struct
13 * @group plugins
14 */
15class Type_Decimal_struct_test extends StructTest {
16
17    /**
18     * Provides failing validation data
19     *
20     * @return array
21     */
22    public function validateFailProvider() {
23        return array(
24            // same as integer:
25            array('foo', '', ''),
26            array('foo222', '', ''),
27            array('-5', '0', ''),
28            array('5', '', '0'),
29            array('500', '100', '200'),
30            array('50', '100', '200'),
31            // decimal specifics
32            array('5.5', '5.6', ''),
33            array('5,5', '5.6', ''),
34            array('-5.5', '-5.4', ''),
35            array('-5,5', '-5.4', ''),
36        );
37    }
38
39    /**
40     * Provides successful validation data
41     *
42     * @return array
43     */
44    public function validateSuccessProvider() {
45        return array(
46            // same as integer
47            array('0', '', ''),
48            array('-5', '', ''),
49            array('5', '', ''),
50            array('5', '0', ''),
51            array('-5', '', '0'),
52            array('150', '100', '200'),
53            // decimal specifics
54            array('5.5', '', ''),
55            array('5,5', '', ''),
56            array('-5.5', '', ''),
57            array('-5,5', '', ''),
58            array('5.5', '4.5', ''),
59            array('5,5', '4.5', ''),
60            array('-5.5', '', '4.5'),
61            array('-5,5', '', '4.5'),
62            array('5.5645000', '', ''),
63            // boundaries
64            array('0', '0', ''),
65            array('0', '', '0'),
66            array('5', '5', ''),
67            array('5', '', '5'),
68            array('0', '0.0', ''),
69            array('0', '', '0.0'),
70            array('5.0', '5.0', ''),
71            array('5.0', '', '5.0'),
72        );
73    }
74
75
76    /**
77     * @expectedException \dokuwiki\plugin\struct\meta\ValidationException
78     * @dataProvider validateFailProvider
79     */
80    public function test_validate_fail($value, $min, $max) {
81        $decimal = new Decimal(array('min' => $min, 'max' => $max));
82        $decimal->validate($value);
83    }
84
85    /**
86     * @dataProvider validateSuccessProvider
87     */
88    public function test_validate_success($value, $min, $max, $decpoint = '.') {
89        $decimal = new Decimal(array('min' => $min, 'max' => $max));
90        $decimal->validate($value);
91        $this->assertTrue(true); // we simply check that no exceptions are thrown
92    }
93
94
95    public function valueProvider() {
96        return array(
97            // $value, $expect, $roundto, $decpoint, $thousands, $trimzeros, $prefix='', $postfix=''
98            array('5000', '5 000,00', '2', ',', ' ', false),
99            array('5000', '5 000', '2', ',', ' ', true),
100            array('5000', '5 000', '0', ',', ' ', false),
101            array('5000', '5 000', '0', ',', ' ', true),
102            array('5000', '5 000', '-1', ',', ' ', false),
103            array('5000', '5 000', '-1', ',', ' ', true),
104
105            array('-0.55600', '-0,56', '2', ',', ' ', false),
106            array('-0.55600', '-0,55600', '-1', ',', ' ', false),
107            array('-0.55600', '-0,556', '-1', ',', ' ', true),
108            array('-0.55600', '-0,5560', '4', ',', ' ', false),
109            array('-0.55600', '-0,556', '4', ',', ' ', true),
110
111            array('-0.55600', '$ -0,556', '4', ',', ' ', true, '$ '),
112            array('-0.55600', '-0,556 EUR', '4', ',', ' ', true, '', ' EUR'),
113        );
114    }
115
116    /**
117     * @dataProvider valueProvider
118     */
119    public function test_renderValue($value, $expect, $roundto, $decpoint, $thousands, $trimzeros, $prefix='', $postfix='') {
120        $decimal = new Decimal(array(
121                                   'roundto' => $roundto,
122                                   'decpoint' => $decpoint,
123                                   'thousands' => $thousands,
124                                   'trimzeros' => $trimzeros,
125                                   'prefix' => $prefix,
126                                   'postfix' => $postfix
127                               ));
128        $R = new \Doku_Renderer_xhtml();
129        $R->doc = '';
130        $decimal->renderValue($value, $R, 'xhtml');
131        $this->assertEquals($expect, $R->doc);
132    }
133
134    public function test_sort() {
135        $this->loadSchemaJSON('decimal');
136        $this->waitForTick();
137        $this->saveData('page1', 'decimal', array('field' => '5000'));
138        $this->saveData('page2', 'decimal', array('field' => '5000.001'));
139        $this->saveData('page3', 'decimal', array('field' => '900.5'));
140        $this->saveData('page4', 'decimal', array('field' => '1.5'));
141
142        $search = new Search();
143        $search->addSchema('decimal');
144        $search->addColumn('%pageid%');
145        $search->addColumn('field');
146        $search->addSort('field', true);
147        /** @var Value[][] $result */
148        $result = $search->execute();
149
150        $this->assertEquals(4, count($result));
151        $this->assertEquals('page4', $result[0][0]->getValue());
152        $this->assertEquals('page3', $result[1][0]->getValue());
153        $this->assertEquals('page1', $result[2][0]->getValue());
154        $this->assertEquals('page2', $result[3][0]->getValue());
155    }
156
157    public function test_filter() {
158        $this->loadSchemaJSON('decimal');
159        $this->waitForTick();
160        $this->saveData('page1', 'decimal', array('field' => '5000'));
161        $this->saveData('page2', 'decimal', array('field' => '5000.001'));
162        $this->saveData('page3', 'decimal', array('field' => '900.5'));
163        $this->saveData('page4', 'decimal', array('field' => '1.5'));
164
165        $search = new Search();
166        $search->addSchema('decimal');
167        $search->addColumn('%pageid%');
168        $search->addColumn('field');
169        $search->addFilter('field', '800', '>', 'AND');
170        $search->addSort('field', true);
171        /** @var Value[][] $result */
172        $result = $search->execute();
173
174        $this->assertEquals(3, count($result));
175        $this->assertEquals('page3', $result[0][0]->getValue());
176        $this->assertEquals('page1', $result[1][0]->getValue());
177        $this->assertEquals('page2', $result[2][0]->getValue());
178    }
179
180}
181