SqlserverTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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
  84. *
  85. * @return void
  86. */
  87. public function testSelectLimit() {
  88. $driver = $this->getMock(
  89. 'Cake\Database\Driver\Sqlserver',
  90. ['_connect', 'connection'],
  91. [['dsn' => 'foo']]
  92. );
  93. $connection = $this->getMock(
  94. '\Cake\Database\Connection',
  95. ['connect', 'driver'],
  96. [['log' => false]]
  97. );
  98. $connection
  99. ->expects($this->any())
  100. ->method('driver')
  101. ->will($this->returnValue($driver));
  102. $query = new \Cake\Database\Query($connection);
  103. $query->select(['id', 'title'])
  104. ->from('articles')
  105. ->order(['id'])
  106. ->offset(10);
  107. $this->assertEquals('SELECT id, title FROM articles ORDER BY id OFFSET 10 ROWS', $query->sql());
  108. $query = new \Cake\Database\Query($connection);
  109. $query->select(['id', 'title'])
  110. ->from('articles')
  111. ->order(['id'])
  112. ->limit(10)
  113. ->offset(50);
  114. $this->assertEquals('SELECT id, title FROM articles ORDER BY id OFFSET 50 ROWS FETCH FIRST 10 ROWS ONLY', $query->sql());
  115. $query = new \Cake\Database\Query($connection);
  116. $query->select(['id', 'title'])
  117. ->from('articles')
  118. ->offset(10);
  119. $this->assertEquals('SELECT id, title FROM articles ORDER BY (SELECT NULL) OFFSET 10 ROWS', $query->sql());
  120. $query = new \Cake\Database\Query($connection);
  121. $query->select(['id', 'title'])
  122. ->from('articles')
  123. ->limit(10);
  124. $this->assertEquals('SELECT TOP 10 id, title FROM articles', $query->sql());
  125. }
  126. }