BodytextParserTest.php 5.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 3/21/16
 * Time: 9:29 AM
 */

namespace qfq;
//use qfq;

require_once(__DIR__ . '/../../qfq/BodytextParser.php');
13
require_once(__DIR__ . '/../../qfq/exceptions/UserFormException.php');
14
15
16


class BodytextParserTest extends \PHPUnit_Framework_TestCase {
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    public function testProcessPlain() {
        $btp = new BodytextParser();

        // Simple row, nothing to remove
        $given = "10.sql = SELECT 'Hello World'";
        $expected = $given;
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // Several rows, remove all but one
        $given = "\n#some comments\n10.sql = SELECT 'Hello World'\n\n    \n   #more comment";
        $expected = "10.sql = SELECT 'Hello World'";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // Several rows, all to remove
        $given = "\n#some comments\n\n\n    \n   #more comment";
        $expected = "";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // Join a line
        $given = "\n10.sql = SELECT 'Hello World',\n'more content'\n      WHERE help=1";
        $expected = "10.sql = SELECT 'Hello World', 'more content' WHERE help=1";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // Join several lines, incl. form
        $given = "\n10.sql = SELECT 'Hello World',\n'more content'\n      WHERE help=1\n 20.head = <table>\n 30.sql     =   SELECT\n col1,\n col2, \n col3\n  # Query stops here\nform = Person\n";
        $expected = "10.sql = SELECT 'Hello World', 'more content' WHERE help=1\n20.head = <table>\n30.sql     =   SELECT col1, col2, col3\nform = Person";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // Nested expression: one
        $given = "10{\nsql = SELECT 'Hello World'}";
        $expected = "10.sql = SELECT 'Hello World'";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // Nested expression, one, added some white spaces
        $given = "\n\n10 { \n \n   sql    =    SELECT 'Hello World'     \n\n    }\n\n";
        $expected = "10.sql    =    SELECT 'Hello World'";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // Nested expression: multiple, simple
        $given = "10.sql = SELECT 'Hello World'\n20 {\nsql='Hello world2'\n}\n30 {\nsql='Hello world3'\n}\n";
        $expected = "10.sql = SELECT 'Hello World'\n20.sql='Hello world2'\n30.sql='Hello world3'";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // Nested expression: complex
70
        $given = "10.sql = SELECT 'Hello World'\n20 {\nsql='Hello world2'\n30 { \n sql=SELECT 'Hello World3'\n40 { \n sql = SELECT 'Hello World4'\n  }  \n  } \n  }  ";
71
72
73
74
75
        $expected = "10.sql = SELECT 'Hello World'\n20.sql='Hello world2'\n20.30.sql=SELECT 'Hello World3'\n20.30.40.sql = SELECT 'Hello World4'";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // form=...., {{ }}
76
        $given = "10.sql = SELECT 'Hello World'\nform = {{form:S}}\n20.sql = SELECT 'Hello World2'\n30 {\nsql=SELECT 'Hello World'\n}\n   form=Person\n";
77
78
79
80
81
82
83
84
85
86
        $expected = "10.sql = SELECT 'Hello World'\nform = {{form:S}}\n20.sql = SELECT 'Hello World2'\n30.sql=SELECT 'Hello World'\nform=Person";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // Nested: open bracket alone
        $given = "10.sql = SELECT 'Hello World'\n20\n{\nhead=test\n}\n30.sql = SELECT 'Hello World'\n";
        $expected = "10.sql = SELECT 'Hello World'\n20.head=test\n30.sql = SELECT 'Hello World'";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

87
88
89
90
91
92
93
94
95
        // Single open bracket inside a string.
        $given = "10.sql = SELECT 'Hello { World'";
        $expected = "10.sql = SELECT 'Hello { World'";
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

        // Complex test
        $given = "10.sql = SELECT '[\*]{7} [0-9]{5}<br>'\n20 {\n   10 {\n      5 {\n         sql = SELECT 'hello world<br>'\n      }\n   }\n}\n20.10.5.head = Terific\n20.sql = SELECT 20, '<br>'\n20.10.sql = SELECT  '20.10<br>'";
        $expected = "10.sql = SELECT '[\*]{7} [0-9]{5}<br>'\n20.10.5.sql = SELECT 'hello world<br>'\n20.10.5.head = Terific\n20.sql = SELECT 20, '<br>'\n20.10.sql = SELECT  '20.10<br>'";
96
97
98
99
100
101
        $result = $btp->process($given);
        $this->assertEquals($expected, $result);

    }

    /**
102
     * @expectedException \qfq\UserFormException
103
104
     *
     */
105
    public function testProcessExceptionClose() {
106
107
        $btp = new BodytextParser();

108
        // Nested: unclosed close bracket
109
110
111
112
        $btp->process("10.sql = SELECT 'Hello World'\n } \n30.sql = SELECT 'Hello World'\n");

    }

113
114
115
116
117
118
119
120
121
122
123
124
    /**
     * @expectedException \qfq\UserFormException
     *
     */
    public function testProcessExceptionOpen() {
        $btp = new BodytextParser();

        // Nested: unclosed open bracket
        $btp->process("10.sql = SELECT 'Hello World'\n20 { \n30.sql = SELECT 'Hello World'\n");

    }

125
}