xref: /plugin/struct/_test/QueryBuilderTest.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\StructException;
6*8fed17f3SAndreas Gohruse dokuwiki\plugin\struct\test\mock\QueryBuilder;
7*8fed17f3SAndreas Gohr
8*8fed17f3SAndreas Gohr/**
9*8fed17f3SAndreas Gohr * @group plugin_struct
10*8fed17f3SAndreas Gohr * @group plugins
11*8fed17f3SAndreas Gohr */
12*8fed17f3SAndreas Gohrclass QueryBuilderTest extends StructTest
13*8fed17f3SAndreas Gohr{
14*8fed17f3SAndreas Gohr
15*8fed17f3SAndreas Gohr    public function test_join()
16*8fed17f3SAndreas Gohr    {
17*8fed17f3SAndreas Gohr        $qb = new QueryBuilder();
18*8fed17f3SAndreas Gohr
19*8fed17f3SAndreas Gohr        $qb->addTable('first');
20*8fed17f3SAndreas Gohr        $qb->addTable('second');
21*8fed17f3SAndreas Gohr        $qb->addTable('third');
22*8fed17f3SAndreas Gohr
23*8fed17f3SAndreas Gohr        $qb->addLeftJoin('second', 'fourth', 'fourth', 'second.foo=fourth.foo');
24*8fed17f3SAndreas Gohr        $this->assertEquals(['first', 'second', 'fourth', 'third'], array_keys($qb->from));
25*8fed17f3SAndreas Gohr    }
26*8fed17f3SAndreas Gohr
27*8fed17f3SAndreas Gohr    public function test_placeholders()
28*8fed17f3SAndreas Gohr    {
29*8fed17f3SAndreas Gohr        $qb = new QueryBuilder();
30*8fed17f3SAndreas Gohr
31*8fed17f3SAndreas Gohr
32*8fed17f3SAndreas Gohr        $foo = $qb->addValue('foo');
33*8fed17f3SAndreas Gohr        $bar = $qb->addValue('bar');
34*8fed17f3SAndreas Gohr
35*8fed17f3SAndreas Gohr        $input = "this is $foo and $bar and $foo again";
36*8fed17f3SAndreas Gohr        $expect = "this is ? and ? and ? again";
37*8fed17f3SAndreas Gohr        $values = ['foo', 'bar', 'foo'];
38*8fed17f3SAndreas Gohr
39*8fed17f3SAndreas Gohr        $output = $qb->fixPlaceholders($input);
40*8fed17f3SAndreas Gohr
41*8fed17f3SAndreas Gohr        $this->assertEquals($expect, $output[0]);
42*8fed17f3SAndreas Gohr        $this->assertEquals($values, $output[1]);
43*8fed17f3SAndreas Gohr    }
44*8fed17f3SAndreas Gohr
45*8fed17f3SAndreas Gohr    public function test_placeholderfail()
46*8fed17f3SAndreas Gohr    {
47*8fed17f3SAndreas Gohr        $this->expectException(StructException::class);
48*8fed17f3SAndreas Gohr        $qb = new QueryBuilder();
49*8fed17f3SAndreas Gohr        $qb->fixPlaceholders('this has unknown placeholder :!!val7!!:');
50*8fed17f3SAndreas Gohr    }
51*8fed17f3SAndreas Gohr}
52