PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/cakephp/cakephp/tests/TestCase/Database/Driver/SqlserverTest.php

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