SqlserverTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Test\TestCase\Database\Driver;
  18. use Cake\Core\Configure;
  19. use Cake\Database\Connection;
  20. use Cake\Database\Driver\Sqlserver;
  21. use Cake\Database\Query;
  22. use \PDO;
  23. /**
  24. * Tests Sqlserver driver
  25. */
  26. class SqlserverTest extends \Cake\TestSuite\TestCase {
  27. /**
  28. * Test connecting to Sqlserver with custom configuration
  29. *
  30. * @return void
  31. */
  32. public function testConnectionConfigCustom() {
  33. $config = [
  34. 'persistent' => false,
  35. 'host' => 'foo',
  36. 'login' => 'Administrator',
  37. 'password' => 'blablabla',
  38. 'database' => 'bar',
  39. 'encoding' => 'a-language',
  40. 'flags' => [1 => true, 2 => false],
  41. 'init' => ['Execute this', 'this too'],
  42. 'settings' => ['config1' => 'value1', 'config2' => 'value2'],
  43. ];
  44. $driver = $this->getMock(
  45. 'Cake\Database\Driver\Sqlserver',
  46. ['_connect', 'connection'],
  47. [$config]
  48. );
  49. $expected = $config;
  50. $expected['dsn'] = 'sqlsrv:Server=foo;Database=bar;MultipleActiveResultSets=false';
  51. $expected['flags'] += [
  52. PDO::ATTR_PERSISTENT => false,
  53. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  54. PDO::SQLSRV_ATTR_ENCODING => 'a-language'
  55. ];
  56. $connection = $this->getMock('stdClass', ['exec', 'quote']);
  57. $connection->expects($this->any())
  58. ->method('quote')
  59. ->will($this->onConsecutiveCalls(
  60. $this->returnArgument(0),
  61. $this->returnArgument(0),
  62. $this->returnArgument(0)
  63. ));
  64. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  65. $connection->expects($this->at(1))->method('exec')->with('this too');
  66. $connection->expects($this->at(2))->method('exec')->with('SET config1 value1');
  67. $connection->expects($this->at(3))->method('exec')->with('SET config2 value2');
  68. $driver->connection($connection);
  69. $driver->expects($this->once())->method('_connect')
  70. ->with($expected);
  71. $driver->expects($this->any())->method('connection')
  72. ->will($this->returnValue($connection));
  73. $driver->connect();
  74. }
  75. /**
  76. * Test select with limit only
  77. *
  78. * @return void
  79. */
  80. public function testSelectLimit() {
  81. $driver = $this->getMock(
  82. 'Cake\Database\Driver\Sqlserver',
  83. ['_connect', 'connection'],
  84. [['dsn' => 'foo']]
  85. );
  86. $connection = $this->getMock(
  87. '\Cake\Database\Connection',
  88. ['connect', 'driver'],
  89. [['log' => false]]
  90. );
  91. $connection
  92. ->expects($this->any())
  93. ->method('driver')
  94. ->will($this->returnValue($driver));
  95. $query = new \Cake\Database\Query($connection);
  96. $query->select(['id', 'title'])
  97. ->from('articles')
  98. ->order(['id'])
  99. ->offset(10);
  100. $this->assertEquals('SELECT id, title FROM articles ORDER BY id OFFSET 10 ROWS', $query->sql());
  101. $query = new \Cake\Database\Query($connection);
  102. $query->select(['id', 'title'])
  103. ->from('articles')
  104. ->order(['id'])
  105. ->limit(10)
  106. ->offset(50);
  107. $this->assertEquals('SELECT id, title FROM articles ORDER BY id OFFSET 50 ROWS FETCH FIRST 10 ROWS ONLY', $query->sql());
  108. $query = new \Cake\Database\Query($connection);
  109. $query->select(['id', 'title'])
  110. ->from('articles')
  111. ->offset(10);
  112. $this->assertEquals('SELECT id, title FROM articles ORDER BY (SELECT NULL) OFFSET 10 ROWS', $query->sql());
  113. $query = new \Cake\Database\Query($connection);
  114. $query->select(['id', 'title'])
  115. ->from('articles')
  116. ->limit(10);
  117. $this->assertEquals('SELECT TOP 10 id, title FROM articles', $query->sql());
  118. }
  119. }