xref: /plugin/struct/_test/QueryBuilderOtherTest.php (revision 7234bfb14e712ff548d9266ef32fdcc8eaf2d04e)
1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\StructException;
6use dokuwiki\plugin\struct\test\mock\QueryBuilder;
7
8/**
9 * @group plugin_struct
10 * @group plugins
11 */
12class QueryBuilderOtherTest extends StructTest
13{
14
15    /**
16     * @noinspection SqlDialectInspection
17     * @noinspection SqlNoDataSourceInspection
18     */
19    public function test_order_by()
20    {
21        $qb = new QueryBuilder();
22
23        $qb->addTable('first', 'T1');
24        $qb->addOrderBy('Q.foo');
25
26        $expectedSQL = '
27            SELECT FROM first AS T1 WHERE ORDER BY Q.foo
28        ';
29
30        list($actual_sql, $actual_opts) = $qb->getSQL();
31        $this->assertEquals($this->cleanWS($expectedSQL), $this->cleanWS($actual_sql));
32        $this->assertEquals(array(), $actual_opts);
33
34    }
35
36    /**
37     * @noinspection SqlDialectInspection
38     * @noinspection SqlNoDataSourceInspection
39     */
40    public function test_group_by()
41    {
42        $qb = new QueryBuilder();
43
44        $qb->addTable('first', 'T1');
45        $qb->addGroupByColumn('T1', 'foo');
46        $qb->addGroupByStatement('T2.bar');
47
48        $expectedSQL = '
49            SELECT FROM first AS T1 WHERE GROUP BY T1.foo, T2.bar
50        ';
51
52        list($actual_sql, $actual_opts) = $qb->getSQL();
53        $this->assertEquals($this->cleanWS($expectedSQL), $this->cleanWS($actual_sql));
54        $this->assertEquals(array(), $actual_opts);
55    }
56
57    public function test_groupby_missing_alias()
58    {
59        $this->expectException(StructException::class);
60        $qb = new QueryBuilder();
61
62        $qb->addTable('first', 'T1');
63        $qb->addGroupByColumn('T2', 'foo');
64    }
65}
66