Skip to content

Commit

Permalink
Using special assertion method to future proof the query tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 7, 2013
1 parent ad52d6e commit 16d27be
Showing 1 changed file with 75 additions and 52 deletions.
127 changes: 75 additions & 52 deletions Cake/Test/TestCase/Database/QueryTest.php
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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('*')
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

}

0 comments on commit 16d27be

Please sign in to comment.