1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\QueryBuilder;
6
7/**
8 * @group plugin_struct
9 * @group plugins
10 */
11class QueryBuilderSelect_struct_test extends StructTest
12{
13
14    public function test_simple_select()
15    {
16        $qb = new QueryBuilder();
17
18        $qb->addTable('first', 'T1');
19        $qb->addSelectColumn('T1', 'colbar', 'asAlias');
20
21
22        $expectedSQL = '
23            SELECT T1.colbar AS asAlias FROM first AS T1 WHERE
24';
25
26        list($actual_sql, $actual_opts) = $qb->getSQL();
27        $this->assertEquals($this->cleanWS($expectedSQL), $this->cleanWS($actual_sql));
28        $this->assertEquals(array(), $actual_opts);
29    }
30
31    public function test_overwrite_selects()
32    {
33        $qb = new QueryBuilder();
34
35        $qb->addTable('first', 'T1');
36        $qb->addSelectColumn('T1', 'colbar_original', 'colAlias');
37        $qb->addSelectColumn('T1', 'colfoo_overwritten', 'colAlias');
38
39        $expectedSQL = 'SELECT T1.colfoo_overwritten AS colAlias FROM first AS T1 WHERE';
40        list($actual_sql, $actual_opts) = $qb->getSQL();
41        $this->assertEquals($this->cleanWS($expectedSQL), $this->cleanWS($actual_sql));
42        $this->assertEquals(array(), $actual_opts);
43    }
44
45    public function test_arbitrary_selects()
46    {
47        $qb = new QueryBuilder();
48
49        $qb->addSelectStatement('a.b', 'C');
50
51        $expectedSQL = 'SELECT a.b AS C FROM WHERE';
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    /**
58     * @expectedException \dokuwiki\plugin\struct\meta\StructException
59     */
60    public function test_missing_alias()
61    {
62        $qb = new QueryBuilder();
63
64        $qb->addTable('first', 'T1');
65        $qb->addSelectColumn('WrongAlias', 'colbar', 'colAlias');
66    }
67
68}
69