|
|
@@ -387,4 +387,78 @@ class SqlserverTest extends TestCase
|
|
|
$expected = 'INSERT INTO articles (title) OUTPUT INSERTED.* VALUES (:c0)';
|
|
|
$this->assertEquals($expected, $query->sql());
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that having queries replace the aggregated alias field.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testHavingReplacesAlias()
|
|
|
+ {
|
|
|
+ $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlserver')
|
|
|
+ ->setMethods(['connect', 'getConnection', 'version'])
|
|
|
+ ->setConstructorArgs([[]])
|
|
|
+ ->getMock();
|
|
|
+ $driver->expects($this->any())
|
|
|
+ ->method('version')
|
|
|
+ ->will($this->returnValue('8'));
|
|
|
+
|
|
|
+ $connection = $this->getMockBuilder('\Cake\Database\Connection')
|
|
|
+ ->setMethods(['connect', 'getDriver', 'setDriver'])
|
|
|
+ ->setConstructorArgs([['log' => false]])
|
|
|
+ ->getMock();
|
|
|
+ $connection->expects($this->any())
|
|
|
+ ->method('getDriver')
|
|
|
+ ->will($this->returnValue($driver));
|
|
|
+
|
|
|
+ $query = new Query($connection);
|
|
|
+ $query
|
|
|
+ ->select([
|
|
|
+ 'posts.author_id',
|
|
|
+ 'post_count' => $query->func()->count('posts.id'),
|
|
|
+ ])
|
|
|
+ ->group(['posts.author_id'])
|
|
|
+ ->having([$query->newExpr()->gte('post_count', 2, 'integer')]);
|
|
|
+
|
|
|
+ $expected = 'SELECT posts.author_id, (COUNT(posts.id)) AS post_count ' .
|
|
|
+ 'GROUP BY posts.author_id HAVING COUNT(posts.id) >= :c0';
|
|
|
+ $this->assertEquals($expected, $query->sql());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that having queries replaces nothing is no alias is used.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testHavingWhenNoAliasIsUsed()
|
|
|
+ {
|
|
|
+ $driver = $this->getMockBuilder('Cake\Database\Driver\Sqlserver')
|
|
|
+ ->setMethods(['connect', 'getConnection', 'version'])
|
|
|
+ ->setConstructorArgs([[]])
|
|
|
+ ->getMock();
|
|
|
+ $driver->expects($this->any())
|
|
|
+ ->method('version')
|
|
|
+ ->will($this->returnValue('8'));
|
|
|
+
|
|
|
+ $connection = $this->getMockBuilder('\Cake\Database\Connection')
|
|
|
+ ->setMethods(['connect', 'getDriver', 'setDriver'])
|
|
|
+ ->setConstructorArgs([['log' => false]])
|
|
|
+ ->getMock();
|
|
|
+ $connection->expects($this->any())
|
|
|
+ ->method('getDriver')
|
|
|
+ ->will($this->returnValue($driver));
|
|
|
+
|
|
|
+ $query = new Query($connection);
|
|
|
+ $query
|
|
|
+ ->select([
|
|
|
+ 'posts.author_id',
|
|
|
+ 'post_count' => $query->func()->count('posts.id'),
|
|
|
+ ])
|
|
|
+ ->group(['posts.author_id'])
|
|
|
+ ->having([$query->newExpr()->gte('posts.author_id', 2, 'integer')]);
|
|
|
+
|
|
|
+ $expected = 'SELECT posts.author_id, (COUNT(posts.id)) AS post_count ' .
|
|
|
+ 'GROUP BY posts.author_id HAVING posts.author_id >= :c0';
|
|
|
+ $this->assertEquals($expected, $query->sql());
|
|
|
+ }
|
|
|
}
|