MysqlTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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' => '+0:00',
  54. 'init' => ["SET time_zone = '+0:00'"],
  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. PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '+0:00'"
  62. ];
  63. $driver->expects($this->once())->method('_connect')
  64. ->with($expected);
  65. $driver->connect([]);
  66. }
  67. /**
  68. * Test connecting to Mysql with custom configuration
  69. *
  70. * @return void
  71. */
  72. public function testConnectionConfigCustom() {
  73. $config = [
  74. 'persistent' => false,
  75. 'host' => 'foo',
  76. 'database' => 'bar',
  77. 'login' => 'user',
  78. 'password' => 'pass',
  79. 'port' => 3440,
  80. 'flags' => [1 => true, 2 => false],
  81. 'encoding' => 'a-language',
  82. 'timezone' => 'Antartica',
  83. 'init' => ['Execute this', 'this too']
  84. ];
  85. $driver = $this->getMock(
  86. 'Cake\Database\Driver\Mysql',
  87. ['_connect'],
  88. [$config]
  89. );
  90. $expected = $config;
  91. $expected['dsn'] = 'mysql:host=foo;port=3440;dbname=bar;charset=a-language';
  92. $expected['init'][] = "SET time_zone = 'Antartica'";
  93. $expected['flags'] += [
  94. PDO::ATTR_PERSISTENT => false,
  95. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  96. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  97. PDO::MYSQL_ATTR_INIT_COMMAND => "Execute this;this too;SET time_zone = 'Antartica'"
  98. ];
  99. $driver->expects($this->once())->method('_connect')
  100. ->with($expected);
  101. $driver->connect();
  102. }
  103. }