MysqlTest.php 2.9 KB

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