From 16d27be93010dada0c126e95a1c1d08e3a2694fa Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Fri, 8 Nov 2013 00:33:54 +0100 Subject: [PATCH] Using special assertion method to future proof the query tests --- Cake/Test/TestCase/Database/QueryTest.php | 127 +++++++++++++--------- 1 file changed, 75 insertions(+), 52 deletions(-) diff --git a/Cake/Test/TestCase/Database/QueryTest.php b/Cake/Test/TestCase/Database/QueryTest.php index bad0714e05e..48e4ddd654a 100644 --- a/Cake/Test/TestCase/Database/QueryTest.php +++ b/Cake/Test/TestCase/Database/QueryTest.php @@ -1515,7 +1515,7 @@ public function testDeleteWithFrom() { ->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 @@ public function testDeleteNoFrom() { ->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 @@ public function testSelectAndDeleteOnSameQuery() { ->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 @@ public function testUpdateSimple() { ->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 @@ public function testUpdateMultipleFields() { ->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 @@ public function testUpdateMultipleFieldsArray() { ->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 @@ public function testUpdateWithExpression() { ->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 @@ public function testInsertSimple() { '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 @@ public function testInsertSparseRow() { '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 @@ public function testInsertFromSelect() { ->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 @@ public function testQuotingSelectFieldsAndAlias() { $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 @@ public function testQuotingFromAndAlias() { $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 @@ public function testQuotingDistinctOn() { $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 @@ public function testQuotingJoinsAndAlias() { $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 @@ public function testQuotingGroupBy() { $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 @@ public function testQuotingExpressions() { $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 @@ public function testQuotingExpressions() { '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 @@ public function testQuotingInsert() { $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 @@ public function assertTable($table, $count, $rows, $conditions = []) { $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); + } + }