SqlserverTest.php 6.2 KB

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