MysqlTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\Mysql;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\TestSuite\TestCase;
  21. use \PDO;
  22. /**
  23. * Tests Mysql driver
  24. *
  25. */
  26. class MysqlTest extends TestCase {
  27. /**
  28. * Helper method for skipping tests that need a real connection.
  29. *
  30. * @return void
  31. */
  32. protected function _needsConnection() {
  33. $config = Configure::read('Datasource.test');
  34. $this->skipIf(strpos($config['datasource'], 'Mysql') === false, 'Not using Mysql for test config');
  35. }
  36. /**
  37. * Test connecting to Mysql with default configuration
  38. *
  39. * @return void
  40. */
  41. public function testConnectionConfigDefault() {
  42. $driver = $this->getMock('Cake\Database\Driver\Mysql', ['_connect']);
  43. $expected = [
  44. 'persistent' => true,
  45. 'host' => 'localhost',
  46. 'login' => 'root',
  47. 'password' => '',
  48. 'database' => 'cake',
  49. 'port' => '3306',
  50. 'flags' => [],
  51. 'encoding' => 'utf8',
  52. 'timezone' => null,
  53. 'init' => [],
  54. 'dsn' => 'mysql:host=localhost;port=3306;dbname=cake;charset=utf8'
  55. ];
  56. $expected['flags'] += [
  57. PDO::ATTR_PERSISTENT => true,
  58. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  59. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  60. ];
  61. $driver->expects($this->once())->method('_connect')
  62. ->with($expected);
  63. $driver->connect([]);
  64. }
  65. /**
  66. * Test connecting to Mysql with custom configuration
  67. *
  68. * @return void
  69. */
  70. public function testConnectionConfigCustom() {
  71. $config = [
  72. 'persistent' => false,
  73. 'host' => 'foo',
  74. 'database' => 'bar',
  75. 'login' => 'user',
  76. 'password' => 'pass',
  77. 'port' => 3440,
  78. 'flags' => [1 => true, 2 => false],
  79. 'encoding' => 'a-language',
  80. 'timezone' => 'Antartica',
  81. 'init' => ['Execute this', 'this too']
  82. ];
  83. $driver = $this->getMock(
  84. 'Cake\Database\Driver\Mysql',
  85. ['_connect'],
  86. [$config]
  87. );
  88. $expected = $config;
  89. $expected['dsn'] = 'mysql:host=foo;port=3440;dbname=bar;charset=a-language';
  90. $expected['init'][] = "SET time_zone = 'Antartica'";
  91. $expected['flags'] += [
  92. PDO::ATTR_PERSISTENT => false,
  93. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  94. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  95. PDO::MYSQL_ATTR_INIT_COMMAND => "Execute this;this too;SET time_zone = 'Antartica'"
  96. ];
  97. $driver->expects($this->once())->method('_connect')
  98. ->with($expected);
  99. $driver->connect();
  100. }
  101. }