Passed
Push — main ( 1152fc...a2dce0 )
by Siad
07:08
created

tests/Phing/Task/Optional/PDODelimitersTest.php (1 issue)

1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Test\Task\Optional;
22
23
use Phing\Io\File;
24
use Phing\Task\System\Pdo\PDOSQLExecTask;
25
use Phing\Test\Support\BuildFileTest;
26
27
/**
28
 * @author Alexey Borzov <[email protected]>
29
 */
30
class PDODelimitersTest extends BuildFileTest
31
{
32
    protected $queries = [];
33
34
    protected $mockTask;
35
36
    public function setUp(): void
37
    {
38
        $this->configureProject(PHING_TEST_BASE . '/etc/tasks/ext/pdo/empty.xml');
39
        $this->queries = [];
40
41
        $this->mockTask = $this->getMockBuilder(PDOSQLExecTask::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

41
        $this->mockTask = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(PDOSQLExecTask::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
42
            ->setMethods(['getConnection', 'execSQL'])
43
            ->getMock()
44
        ;
45
        $this->mockTask->setProject($this->project);
46
        // prevents calling beginTransaction() on obviously missing PDO instance
47
        $this->mockTask->setAutocommit(true);
48
        $this->mockTask->expects($this->atLeastOnce())->method('execSQL')
49
            ->will($this->returnCallback([$this, 'storeQuery']))
50
        ;
51
52
        $targets = $this->project->getTargets();
53
        $targets['test']->addTask($this->mockTask);
54
        $this->mockTask->setOwningTarget($targets['test']);
55
    }
56
57
    public function storeQuery($query)
58
    {
59
        $query = trim($query);
60
        if (strlen($query)) {
61
            $this->queries[] = str_replace(["\n\n", "\r"], ["\n", ''], $query);
62
        }
63
    }
64
65
    public function testDelimiterTypeNormal()
66
    {
67
        // for some reason default splitter mangles spaces on subsequent lines
68
        $expected = [
69
            <<<'SQL'
70
                insert into foo (bar, "strange;name""indeed") values ('bar''s value containing ;', 'a value for strange named column')
71
                SQL,
72
            <<<'SQL'
73
                delete
74
                 from
75
                 foo where bar = 'some value'
76
                SQL,
77
            <<<'SQL'
78
                update dump -- I should not be ignored
79
                 set message = 'I am a string with \\ backslash \' escapes and semicolons;'
80
                SQL,
81
            <<<'SQL'
82
                create procedure setfoo(newfoo int)
83
                 begin
84
                 set @foo = newfoo;
85
                 end
86
                SQL,
87
            <<<'SQL'
88
                insert into dump (message) values ('I am a statement not ending with a delimiter')
89
                SQL,
90
        ];
91
        // and insists on "\n" linebreaks
92
        foreach ($expected as &$query) {
93
            $query = str_replace(["\n\n", "\r"], ["\n", ''], $query);
94
        }
95
96
        $this->mockTask->setSrc(new File(PHING_TEST_BASE . '/etc/tasks/ext/pdo/delimiters-normal.sql'));
97
        $this->mockTask->setDelimiterType(PDOSQLExecTask::DELIM_NORMAL);
98
        $this->project->setProperty('bar.value', 'some value');
99
        $this->project->executeTarget('test');
100
101
        $this->assertEquals($expected, $this->queries);
102
    }
103
104
    public function testDelimiterTypeRow()
105
    {
106
        // for some reason default splitter mangles spaces on subsequent lines
107
        $expected = [
108
            <<<'SQL'
109
                insert into "duh" (foo) values ('duh')
110
                SQL,
111
            <<<'SQL'
112
                update "duh?" -- I should not be ignored
113
                 set foo = 'some value'
114
                SQL,
115
            <<<'SQL'
116
                insert into dump (message) values ('I am a statement not ending with a delimiter')
117
                SQL,
118
        ];
119
        // and insists on "\n" linebreaks
120
        foreach ($expected as &$query) {
121
            $query = str_replace(["\n\n", "\r"], ["\n", ''], $query);
122
        }
123
124
        $this->mockTask->setSrc(new File(PHING_TEST_BASE . '/etc/tasks/ext/pdo/delimiters-row.sql'));
125
        $this->mockTask->setDelimiterType(PDOSQLExecTask::DELIM_ROW);
126
        $this->mockTask->setDelimiter('duh');
127
        $this->project->setProperty('foo.value', 'some value');
128
        $this->project->executeTarget('test');
129
130
        $this->assertEquals($expected, $this->queries);
131
    }
132
133
    /**
134
     * Checks that PDOSQLExecTask properly understands PostgreSQL dialect
135
     * (especially "dollar quoting") when working with 'pgsql:' URLs.
136
     *
137
     * @see http://www.phing.info/trac/ticket/499
138
     * @see http://www.postgresql.org/docs/9.0/interactive/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING
139
     */
140
    public function testRequest499()
141
    {
142
        $expected = [
143
            <<<'SQL'
144
                select 1
145
                # 2
146
                SQL,
147
            <<<'SQL'
148
                select 'foo'
149
                // 'bar'
150
                SQL,
151
            <<<'SQL'
152
                insert into foo (bar, "strange;name""indeed") values ('bar''s value containing ;', 'a value for strange named column')
153
                SQL,
154
            <<<'SQL'
155
                create function foo(text)
156
                returns boolean as
157
                $function$
158
                BEGIN
159
                    RETURN ($1 ~ $q$[\t\r\n\v\\]$q$);
160
                END;
161
                $function$
162
                language plpgsql
163
                SQL,
164
            <<<'SQL'
165
                CREATE FUNCTION phingPDOtest() RETURNS "trigger"
166
                    AS $_X$
167
                if (1)
168
                {
169
                    # All is well - just continue
170
171
                    return;
172
                }
173
                else
174
                {
175
                    # Not good - this is probably a fatal error!
176
                    elog(ERROR,"True is not true");
177
178
                    return "SKIP";
179
                }
180
                $_X$
181
                    LANGUAGE plperl
182
                SQL,
183
            "insert into foo (bar) \nvalues ('some value')",
184
            <<<'SQL'
185
                insert into foo (bar) values ($$ a dollar-quoted string containing a few quotes ' ", a $placeholder$ and a semicolon;$$)
186
                SQL,
187
            <<<'SQL'
188
                create rule blah_insert
189
                as on insert to blah do instead (
190
                    insert into foo values (new.id, 'blah');
191
                    insert into bar values (new.id, 'blah-blah');
192
                )
193
                SQL,
194
            <<<'SQL'
195
                insert into dump (message) values ('I am a statement not ending with a delimiter')
196
                SQL,
197
        ];
198
        foreach ($expected as &$query) {
199
            $query = str_replace(["\n\n", "\r"], ["\n", ''], $query);
200
        }
201
202
        $this->mockTask->setSrc(new File(PHING_TEST_BASE . '/etc/tasks/ext/pdo/delimiters-pgsql.sql'));
203
        $this->mockTask->setUrl('pgsql:host=localhost;dbname=phing');
204
        $this->mockTask->setDelimiterType(PDOSQLExecTask::DELIM_NORMAL);
205
        $this->project->setProperty('bar.value', 'some value');
206
        $this->project->executeTarget('test');
207
208
        $this->assertEquals($expected, $this->queries);
209
    }
210
}
211