SqlserverTest.php 8.2 KB

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