xref: /plugin/struct/_test/QueryBuilderSelectTest.php (revision 8fed17f342cc190557a6ce94d1787f9e2f63cb6c)
1*8fed17f3SAndreas Gohr<?php
2*8fed17f3SAndreas Gohr
3*8fed17f3SAndreas Gohrnamespace dokuwiki\plugin\struct\test;
4*8fed17f3SAndreas Gohr
5*8fed17f3SAndreas Gohruse dokuwiki\plugin\struct\meta\QueryBuilder;
6*8fed17f3SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
7*8fed17f3SAndreas Gohr
8*8fed17f3SAndreas Gohr/**
9*8fed17f3SAndreas Gohr * @group plugin_struct
10*8fed17f3SAndreas Gohr * @group plugins
11*8fed17f3SAndreas Gohr */
12*8fed17f3SAndreas Gohrclass QueryBuilderSelectTest extends StructTest
13*8fed17f3SAndreas Gohr{
14*8fed17f3SAndreas Gohr
15*8fed17f3SAndreas Gohr    /**
16*8fed17f3SAndreas Gohr     * @noinspection SqlDialectInspection
17*8fed17f3SAndreas Gohr     * @noinspection SqlNoDataSourceInspection
18*8fed17f3SAndreas Gohr     */
19*8fed17f3SAndreas Gohr    public function test_simple_select()
20*8fed17f3SAndreas Gohr    {
21*8fed17f3SAndreas Gohr        $qb = new QueryBuilder();
22*8fed17f3SAndreas Gohr
23*8fed17f3SAndreas Gohr        $qb->addTable('first', 'T1');
24*8fed17f3SAndreas Gohr        $qb->addSelectColumn('T1', 'colbar', 'asAlias');
25*8fed17f3SAndreas Gohr
26*8fed17f3SAndreas Gohr
27*8fed17f3SAndreas Gohr        $expectedSQL = '
28*8fed17f3SAndreas Gohr            SELECT T1.colbar AS asAlias FROM first AS T1 WHERE
29*8fed17f3SAndreas Gohr        ';
30*8fed17f3SAndreas Gohr
31*8fed17f3SAndreas Gohr        list($actual_sql, $actual_opts) = $qb->getSQL();
32*8fed17f3SAndreas Gohr        $this->assertEquals($this->cleanWS($expectedSQL), $this->cleanWS($actual_sql));
33*8fed17f3SAndreas Gohr        $this->assertEquals(array(), $actual_opts);
34*8fed17f3SAndreas Gohr    }
35*8fed17f3SAndreas Gohr
36*8fed17f3SAndreas Gohr    /**
37*8fed17f3SAndreas Gohr     * @noinspection SqlDialectInspection
38*8fed17f3SAndreas Gohr     * @noinspection SqlNoDataSourceInspection
39*8fed17f3SAndreas Gohr     */
40*8fed17f3SAndreas Gohr    public function test_overwrite_selects()
41*8fed17f3SAndreas Gohr    {
42*8fed17f3SAndreas Gohr        $qb = new QueryBuilder();
43*8fed17f3SAndreas Gohr
44*8fed17f3SAndreas Gohr        $qb->addTable('first', 'T1');
45*8fed17f3SAndreas Gohr        $qb->addSelectColumn('T1', 'colbar_original', 'colAlias');
46*8fed17f3SAndreas Gohr        $qb->addSelectColumn('T1', 'colfoo_overwritten', 'colAlias');
47*8fed17f3SAndreas Gohr
48*8fed17f3SAndreas Gohr        $expectedSQL = 'SELECT T1.colfoo_overwritten AS colAlias FROM first AS T1 WHERE';
49*8fed17f3SAndreas Gohr        list($actual_sql, $actual_opts) = $qb->getSQL();
50*8fed17f3SAndreas Gohr        $this->assertEquals($this->cleanWS($expectedSQL), $this->cleanWS($actual_sql));
51*8fed17f3SAndreas Gohr        $this->assertEquals(array(), $actual_opts);
52*8fed17f3SAndreas Gohr    }
53*8fed17f3SAndreas Gohr
54*8fed17f3SAndreas Gohr    /**
55*8fed17f3SAndreas Gohr     * @noinspection SqlDialectInspection
56*8fed17f3SAndreas Gohr     * @noinspection SqlNoDataSourceInspection
57*8fed17f3SAndreas Gohr     */
58*8fed17f3SAndreas Gohr    public function test_arbitrary_selects()
59*8fed17f3SAndreas Gohr    {
60*8fed17f3SAndreas Gohr        $qb = new QueryBuilder();
61*8fed17f3SAndreas Gohr
62*8fed17f3SAndreas Gohr        $qb->addSelectStatement('a.b', 'C');
63*8fed17f3SAndreas Gohr
64*8fed17f3SAndreas Gohr        $expectedSQL = 'SELECT a.b AS C FROM WHERE';
65*8fed17f3SAndreas Gohr        list($actual_sql, $actual_opts) = $qb->getSQL();
66*8fed17f3SAndreas Gohr        $this->assertEquals($this->cleanWS($expectedSQL), $this->cleanWS($actual_sql));
67*8fed17f3SAndreas Gohr        $this->assertEquals(array(), $actual_opts);
68*8fed17f3SAndreas Gohr    }
69*8fed17f3SAndreas Gohr
70*8fed17f3SAndreas Gohr    public function test_missing_alias()
71*8fed17f3SAndreas Gohr    {
72*8fed17f3SAndreas Gohr        $this->expectException(StructException::class);
73*8fed17f3SAndreas Gohr        $qb = new QueryBuilder();
74*8fed17f3SAndreas Gohr
75*8fed17f3SAndreas Gohr        $qb->addTable('first', 'T1');
76*8fed17f3SAndreas Gohr        $qb->addSelectColumn('WrongAlias', 'colbar', 'colAlias');
77*8fed17f3SAndreas Gohr    }
78*8fed17f3SAndreas Gohr
79*8fed17f3SAndreas Gohr}
80