SqlserverTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Driver;
  16. use Cake\Core\Configure;
  17. use Cake\Database\Connection;
  18. use Cake\Database\Driver\Sqlserver;
  19. use Cake\Database\Query;
  20. use Cake\TestSuite\TestCase;
  21. use \PDO;
  22. /**
  23. * Tests Sqlserver driver
  24. */
  25. class SqlserverTest extends TestCase {
  26. /**
  27. * Set up
  28. *
  29. * @return void
  30. */
  31. public function setUp() {
  32. parent::setUp();
  33. $this->skipUnless(defined('PDO::SQLSRV_ENCODING_UTF8'), 'SQL Server extension not present');
  34. }
  35. /**
  36. * Test connecting to Sqlserver with custom configuration
  37. *
  38. * @return void
  39. */
  40. public function testConnectionConfigCustom() {
  41. $config = [
  42. 'persistent' => false,
  43. 'host' => 'foo',
  44. 'login' => 'Administrator',
  45. 'password' => 'blablabla',
  46. 'database' => 'bar',
  47. 'encoding' => 'a-language',
  48. 'flags' => [1 => true, 2 => false],
  49. 'init' => ['Execute this', 'this too'],
  50. 'settings' => ['config1' => 'value1', 'config2' => 'value2'],
  51. ];
  52. $driver = $this->getMock(
  53. 'Cake\Database\Driver\Sqlserver',
  54. ['_connect', 'connection'],
  55. [$config]
  56. );
  57. $expected = $config;
  58. $expected['dsn'] = 'sqlsrv:Server=foo;Database=bar;MultipleActiveResultSets=false';
  59. $expected['flags'] += [
  60. PDO::ATTR_PERSISTENT => false,
  61. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  62. PDO::SQLSRV_ATTR_ENCODING => 'a-language'
  63. ];
  64. $connection = $this->getMock('stdClass', ['exec', 'quote']);
  65. $connection->expects($this->any())
  66. ->method('quote')
  67. ->will($this->onConsecutiveCalls(
  68. $this->returnArgument(0),
  69. $this->returnArgument(0),
  70. $this->returnArgument(0)
  71. ));
  72. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  73. $connection->expects($this->at(1))->method('exec')->with('this too');
  74. $connection->expects($this->at(2))->method('exec')->with('SET config1 value1');
  75. $connection->expects($this->at(3))->method('exec')->with('SET config2 value2');
  76. $driver->connection($connection);
  77. $driver->expects($this->once())->method('_connect')
  78. ->with($expected);
  79. $driver->expects($this->any())->method('connection')
  80. ->will($this->returnValue($connection));
  81. $driver->connect();
  82. }
  83. /**
  84. * Test select with limit only and SQLServer2012+
  85. *
  86. * @return void
  87. */
  88. public function testSelectLimitVersion12() {
  89. $driver = $this->getMock(
  90. 'Cake\Database\Driver\Sqlserver',
  91. ['_connect', 'connection', '_version'],
  92. [['dsn' => 'foo']]
  93. );
  94. $driver
  95. ->expects($this->any())
  96. ->method('_version')
  97. ->will($this->returnValue(12));
  98. $connection = $this->getMock(
  99. '\Cake\Database\Connection',
  100. ['connect', 'driver'],
  101. [['log' => false]]
  102. );
  103. $connection
  104. ->expects($this->any())
  105. ->method('driver')
  106. ->will($this->returnValue($driver));
  107. $query = new \Cake\Database\Query($connection);
  108. $query->select(['id', 'title'])
  109. ->from('articles')
  110. ->order(['id'])
  111. ->offset(10);
  112. $this->assertEquals('SELECT id, title FROM articles ORDER BY id OFFSET 10 ROWS', $query->sql());
  113. $query = new \Cake\Database\Query($connection);
  114. $query->select(['id', 'title'])
  115. ->from('articles')
  116. ->order(['id'])
  117. ->limit(10)
  118. ->offset(50);
  119. $this->assertEquals('SELECT id, title FROM articles ORDER BY id OFFSET 50 ROWS FETCH FIRST 10 ROWS ONLY', $query->sql());
  120. $query = new \Cake\Database\Query($connection);
  121. $query->select(['id', 'title'])
  122. ->from('articles')
  123. ->offset(10);
  124. $this->assertEquals('SELECT id, title FROM articles ORDER BY (SELECT NULL) OFFSET 10 ROWS', $query->sql());
  125. $query = new \Cake\Database\Query($connection);
  126. $query->select(['id', 'title'])
  127. ->from('articles')
  128. ->limit(10);
  129. $this->assertEquals('SELECT TOP 10 id, title FROM articles', $query->sql());
  130. }
  131. /**
  132. * Test select with limit on lte SQLServer2008
  133. *
  134. * @return void
  135. */
  136. public function testSelectLimitOldServer() {
  137. $driver = $this->getMock(
  138. 'Cake\Database\Driver\Sqlserver',
  139. ['_connect', 'connection', '_version'],
  140. [['dsn' => 'foo']]
  141. );
  142. $driver
  143. ->expects($this->any())
  144. ->method('_version')
  145. ->will($this->returnValue(8));
  146. $connection = $this->getMock(
  147. '\Cake\Database\Connection',
  148. ['connect', 'driver'],
  149. [['log' => false]]
  150. );
  151. $connection
  152. ->expects($this->any())
  153. ->method('driver')
  154. ->will($this->returnValue($driver));
  155. $query = new \Cake\Database\Query($connection);
  156. $query->select(['id', 'title'])
  157. ->from('articles')
  158. ->limit(10);
  159. $expected = 'SELECT TOP 10 id, title FROM articles';
  160. $this->assertEquals($expected, $query->sql());
  161. $query = new \Cake\Database\Query($connection);
  162. $query->select(['id', 'title'])
  163. ->from('articles')
  164. ->offset(10);
  165. $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) AS [_cake_page_rownum_] ' .
  166. 'FROM articles) _cake_paging_ ' .
  167. 'WHERE _cake_paging_._cake_page_rownum_ > :c0';
  168. $this->assertEquals($expected, $query->sql());
  169. $query = new \Cake\Database\Query($connection);
  170. $query->select(['id', 'title'])
  171. ->from('articles')
  172. ->order(['id'])
  173. ->offset(10);
  174. $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY id)) AS [_cake_page_rownum_] ' .
  175. 'FROM articles) _cake_paging_ ' .
  176. 'WHERE _cake_paging_._cake_page_rownum_ > :c0';
  177. $this->assertEquals($expected, $query->sql());
  178. $query = new \Cake\Database\Query($connection);
  179. $query->select(['id', 'title'])
  180. ->from('articles')
  181. ->order(['id'])
  182. ->where(['title' => 'Something'])
  183. ->limit(10)
  184. ->offset(50);
  185. $expected = 'SELECT * FROM (SELECT id, title, (ROW_NUMBER() OVER (ORDER BY id)) AS [_cake_page_rownum_] ' .
  186. 'FROM articles WHERE title = :c0) _cake_paging_ ' .
  187. 'WHERE (_cake_paging_._cake_page_rownum_ > :c1 AND _cake_paging_._cake_page_rownum_ <= :c2)';
  188. $this->assertEquals($expected, $query->sql());
  189. }
  190. }