|
|
@@ -1515,7 +1515,7 @@ class QueryTest extends TestCase {
|
|
|
->where('1 = 1');
|
|
|
|
|
|
$result = $query->sql();
|
|
|
- $this->assertRegExp('/^DELETE FROM [`"]?authors[`"]?/', $result);
|
|
|
+ $this->assertQuotedQuery('DELETE FROM [authors]', $result, true);
|
|
|
|
|
|
$result = $query->execute();
|
|
|
$this->assertInstanceOf('Cake\Database\StatementInterface', $result);
|
|
|
@@ -1534,7 +1534,7 @@ class QueryTest extends TestCase {
|
|
|
->where('1 = 1');
|
|
|
|
|
|
$result = $query->sql();
|
|
|
- $this->assertRegExp('/^DELETE FROM [`"]?authors[`"]? /', $result);
|
|
|
+ $this->assertQuotedQuery('DELETE FROM [authors]', $result, true);
|
|
|
|
|
|
$result = $query->execute();
|
|
|
$this->assertInstanceOf('Cake\Database\StatementInterface', $result);
|
|
|
@@ -1553,7 +1553,7 @@ class QueryTest extends TestCase {
|
|
|
->where('1 = 1');
|
|
|
$result = $query->sql();
|
|
|
|
|
|
- $this->assertRegExp('/^DELETE FROM [`"]?authors[`"]? /', $result);
|
|
|
+ $this->assertQuotedQuery('DELETE FROM [authors]', $result, true);
|
|
|
$this->assertContains(' WHERE 1 = 1', $result);
|
|
|
}
|
|
|
|
|
|
@@ -1568,7 +1568,7 @@ class QueryTest extends TestCase {
|
|
|
->set('name', 'mark')
|
|
|
->where(['id' => 1]);
|
|
|
$result = $query->sql();
|
|
|
- $this->assertRegExp('/^UPDATE ["`]?authors["`]? SET ["`]?name["`]? = :/', $result);
|
|
|
+ $this->assertQuotedQuery('UPDATE [authors] SET [name] = :', $result, true);
|
|
|
|
|
|
$result = $query->execute();
|
|
|
$this->assertCount(1, $result);
|
|
|
@@ -1587,13 +1587,13 @@ class QueryTest extends TestCase {
|
|
|
->where(['id' => 1]);
|
|
|
$result = $query->sql();
|
|
|
|
|
|
- $this->assertRegExp(
|
|
|
- '/UPDATE ["`]?articles["`]? SET ["`]?title["`]? = :c0 , ["`]?body["`]? = :c1/',
|
|
|
- $result
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'UPDATE [articles] SET [title] = :c0 , [body] = :c1',
|
|
|
+ $result,
|
|
|
+ true
|
|
|
);
|
|
|
|
|
|
- $this->assertRegExp('/ WHERE ["`]?id["`]? = :c2$/', $result);
|
|
|
-
|
|
|
+ $this->assertQuotedQuery(' WHERE [id] = :c2$', $result, true);
|
|
|
$result = $query->execute();
|
|
|
$this->assertCount(1, $result);
|
|
|
}
|
|
|
@@ -1613,11 +1613,12 @@ class QueryTest extends TestCase {
|
|
|
->where(['id' => 1]);
|
|
|
$result = $query->sql();
|
|
|
|
|
|
- $this->assertRegExp(
|
|
|
- '/UPDATE ["`]?articles["`]? SET ["`]?title["`]? = :[0-9a-z]+ , ["`]?body["`]? = :[0-9a-z]+/',
|
|
|
- $result
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'UPDATE [articles] SET [title] = :c0 , [body] = :c1',
|
|
|
+ $result,
|
|
|
+ true
|
|
|
);
|
|
|
- $this->assertRegExp('/WHERE ["`]?id["`]? = :/', $result);
|
|
|
+ $this->assertQuotedQuery('WHERE [id] = :', $result, true);
|
|
|
|
|
|
$result = $query->execute();
|
|
|
$this->assertCount(1, $result);
|
|
|
@@ -1639,9 +1640,10 @@ class QueryTest extends TestCase {
|
|
|
->where(['id' => 1]);
|
|
|
$result = $query->sql();
|
|
|
|
|
|
- $this->assertRegExp(
|
|
|
- '/UPDATE ["`]?articles["`]? SET title = author_id WHERE ["`]?id["`]? = :/',
|
|
|
- $result
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'UPDATE [articles] SET title = author_id WHERE [id] = :',
|
|
|
+ $result,
|
|
|
+ true
|
|
|
);
|
|
|
|
|
|
$result = $query->execute();
|
|
|
@@ -1676,10 +1678,11 @@ class QueryTest extends TestCase {
|
|
|
'body' => 'test insert'
|
|
|
]);
|
|
|
$result = $query->sql();
|
|
|
- $this->assertRegExp(
|
|
|
- '/INSERT INTO [`"]?articles[`"]? \([`"]?title[`"]?, [`"]?body[`"]?\) ' .
|
|
|
- 'VALUES \(\?, \?\)/',
|
|
|
- $result
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'INSERT INTO [articles] \([title], [body]\) ' .
|
|
|
+ 'VALUES \(\?, \?\)',
|
|
|
+ $result,
|
|
|
+ true
|
|
|
);
|
|
|
|
|
|
$result = $query->execute();
|
|
|
@@ -1710,10 +1713,11 @@ class QueryTest extends TestCase {
|
|
|
'title' => 'mark',
|
|
|
]);
|
|
|
$result = $query->sql();
|
|
|
- $this->assertRegExp(
|
|
|
- '/INSERT INTO [`"]?articles[`"]? \([`"]?title[`"]?, [`"]?body[`"]?\) ' .
|
|
|
- 'VALUES \(\?, \?\)/',
|
|
|
- $result
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'INSERT INTO [articles] \([title], [body]\) ' .
|
|
|
+ 'VALUES \(\?, \?\)',
|
|
|
+ $result,
|
|
|
+ true
|
|
|
);
|
|
|
|
|
|
$result = $query->execute();
|
|
|
@@ -1787,14 +1791,13 @@ class QueryTest extends TestCase {
|
|
|
->values($select);
|
|
|
|
|
|
$result = $query->sql();
|
|
|
- $this->assertRegExp(
|
|
|
- '/INSERT INTO [`"]?articles[`"]? \([`"]?title[`"]?, [`"]?body[`"]?, ' .
|
|
|
- '[`"]?author_id[`"]?\) SELECT/',
|
|
|
- $result
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'INSERT INTO [articles] \([title], [body], [author_id]\) SELECT',
|
|
|
+ $result,
|
|
|
+ true
|
|
|
);
|
|
|
- $this->assertRegExp(
|
|
|
- '/SELECT ["`]?name["`]?, \'some text\', 99 FROM ["`]?authors["`]?/',
|
|
|
- $result);
|
|
|
+ $this->assertQuotedQuery(
|
|
|
+ 'SELECT [name], \'some text\', 99 FROM [authors]', $result, true);
|
|
|
$result = $query->execute();
|
|
|
|
|
|
$this->assertCount(1, $result);
|
|
|
@@ -2035,27 +2038,27 @@ class QueryTest extends TestCase {
|
|
|
$this->connection->driver()->autoQuoting(true);
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select(['something'])->sql();
|
|
|
- $this->assertRegExp('/SELECT [`"]something[`"]$/', $sql);
|
|
|
+ $this->assertQuotedQuery('SELECT [something]$', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select(['foo' => 'something'])->sql();
|
|
|
- $this->assertRegExp('/SELECT [`"]something[`"] AS [`"]foo[`"]$/', $sql);
|
|
|
+ $this->assertQuotedQuery('SELECT [something] AS [foo]$', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select(['foo' => 1])->sql();
|
|
|
- $this->assertRegExp('/SELECT 1 AS [`"]foo[`"]$/', $sql);
|
|
|
+ $this->assertQuotedQuery('SELECT 1 AS [foo]$', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select(['foo' => '1 + 1'])->sql();
|
|
|
- $this->assertRegExp('/SELECT [`"]1 \+ 1[`"] AS [`"]foo[`"]$/', $sql);
|
|
|
+ $this->assertQuotedQuery('SELECT [1 \+ 1] AS [foo]$', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select(['foo' => $query->newExpr()->add('1 + 1')])->sql();
|
|
|
- $this->assertRegExp('/SELECT \(1 \+ 1\) AS [`"]foo[`"]$/', $sql);
|
|
|
+ $this->assertQuotedQuery('SELECT \(1 \+ 1\) AS [foo]$', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select(['foo' => new IdentifierExpression('bar')])->sql();
|
|
|
- $this->assertRegExp('/[`"]bar[`"]/', $sql);
|
|
|
+ $this->assertQuotedQuery('[bar]', $sql);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -2067,15 +2070,15 @@ class QueryTest extends TestCase {
|
|
|
$this->connection->driver()->autoQuoting(true);
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')->from(['something'])->sql();
|
|
|
- $this->assertRegExp('/FROM [`"]something[`"]/', $sql);
|
|
|
+ $this->assertQuotedQuery('FROM [something]', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')->from(['foo' => 'something'])->sql();
|
|
|
- $this->assertRegExp('/FROM [`"]something[`"] AS [`"]foo[`"]$/', $sql);
|
|
|
+ $this->assertQuotedQuery('FROM [something] AS [foo]$', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')->from(['foo' => $query->newExpr()->add('bar')])->sql();
|
|
|
- $this->assertRegExp('/FROM \(bar\) AS [`"]foo[`"]$/', $sql);
|
|
|
+ $this->assertQuotedQuery('FROM \(bar\) AS [foo]$', $sql);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -2087,7 +2090,7 @@ class QueryTest extends TestCase {
|
|
|
$this->connection->driver()->autoQuoting(true);
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')->distinct(['something'])->sql();
|
|
|
- $this->assertRegExp('/[`"]something[`"]/', $sql);
|
|
|
+ $this->assertQuotedQuery('[something]', $sql);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -2099,15 +2102,15 @@ class QueryTest extends TestCase {
|
|
|
$this->connection->driver()->autoQuoting(true);
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')->join(['something'])->sql();
|
|
|
- $this->assertRegExp('/JOIN [`"]something[`"]/', $sql);
|
|
|
+ $this->assertQuotedQuery('JOIN [something]', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')->join(['foo' => 'something'])->sql();
|
|
|
- $this->assertRegExp('/JOIN [`"]something[`"] [`"]foo[`"]/', $sql);
|
|
|
+ $this->assertQuotedQuery('JOIN [something] [foo]', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')->join(['foo' => $query->newExpr()->add('bar')])->sql();
|
|
|
- $this->assertRegExp('/JOIN \(bar\) [`"]foo[`"]/', $sql);
|
|
|
+ $this->assertQuotedQuery('JOIN \(bar\) [foo]', $sql);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -2119,15 +2122,15 @@ class QueryTest extends TestCase {
|
|
|
$this->connection->driver()->autoQuoting(true);
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')->group(['something'])->sql();
|
|
|
- $this->assertRegExp('/GROUP BY [`"]something[`"]/', $sql);
|
|
|
+ $this->assertQuotedQuery('GROUP BY [something]', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')->group([$query->newExpr()->add('bar')])->sql();
|
|
|
- $this->assertRegExp('/GROUP BY \(bar\)/', $sql);
|
|
|
+ $this->assertQuotedQuery('GROUP BY \(bar\)', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')->group([new IdentifierExpression('bar')])->sql();
|
|
|
- $this->assertRegExp('/GROUP BY \([`"]bar[`"]\)/', $sql);
|
|
|
+ $this->assertQuotedQuery('GROUP BY \([bar]\)', $sql);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -2141,7 +2144,7 @@ class QueryTest extends TestCase {
|
|
|
$sql = $query->select('*')
|
|
|
->where(['something' => 'value'])
|
|
|
->sql();
|
|
|
- $this->assertRegExp('/WHERE [`"]something[`"] = :c0/', $sql);
|
|
|
+ $this->assertQuotedQuery('WHERE [something] = :c0', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->select('*')
|
|
|
@@ -2150,8 +2153,8 @@ class QueryTest extends TestCase {
|
|
|
'OR' => ['foo' => 'bar', 'baz' => 'cake']
|
|
|
])
|
|
|
->sql();
|
|
|
- $this->assertRegExp('/[`"]something[`"] = :c0 AND/', $sql);
|
|
|
- $this->assertRegExp('/\([`"]foo[`"] = :c1 OR [`"]baz[`"] = :c2\)/', $sql);
|
|
|
+ $this->assertQuotedQuery('[something] = :c0 AND', $sql);
|
|
|
+ $this->assertQuotedQuery('\([foo] = :c1 OR [baz] = :c2\)', $sql);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -2165,13 +2168,13 @@ class QueryTest extends TestCase {
|
|
|
$sql = $query->insert('foo', ['bar', 'baz'])
|
|
|
->where(['something' => 'value'])
|
|
|
->sql();
|
|
|
- $this->assertRegExp('/INSERT INTO [`"]foo[`"] \([`"]bar[`"], [`"]baz[`"]\)/', $sql);
|
|
|
+ $this->assertQuotedQuery('INSERT INTO [foo] \([bar], [baz]\)', $sql);
|
|
|
|
|
|
$query = new Query($this->connection);
|
|
|
$sql = $query->insert('foo', [$query->newExpr()->add('bar')])
|
|
|
->where(['something' => 'value'])
|
|
|
->sql();
|
|
|
- $this->assertRegExp('/INSERT INTO [`"]foo[`"] \(\(bar\)\)/', $sql);
|
|
|
+ $this->assertQuotedQuery('INSERT INTO [foo] \(\(bar\)\)', $sql);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -2191,4 +2194,24 @@ class QueryTest extends TestCase {
|
|
|
$this->assertEquals($rows, $result->fetchAll('assoc'));
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * Assertion for comparing a regex pattern against a query having its indentifiers
|
|
|
+ * quoted. It accepts queries quoted with the characters `[` and `]`. If the third
|
|
|
+ * parameter is set to true, it will alter the pattern to both accept quoted and
|
|
|
+ * unquoted queries
|
|
|
+ *
|
|
|
+ * @param string $pattern
|
|
|
+ * @param string $query the result to compare against
|
|
|
+ * @param boolean $optional
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function assertQuotedQuery($pattern, $query, $optional = false) {
|
|
|
+ if ($optional) {
|
|
|
+ $optional = '?';
|
|
|
+ }
|
|
|
+ $pattern = str_replace('[', '[`"\[]' . $optional, $pattern);
|
|
|
+ $pattern = str_replace(']', '[`"\]]' . $optional, $pattern);
|
|
|
+ $this->assertRegExp('#' . $pattern . '#', $query);
|
|
|
+ }
|
|
|
+
|
|
|
}
|