MysqlTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * setup
  29. *
  30. * @return void
  31. */
  32. public function setup() {
  33. parent::setUp();
  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. $dsn = 'mysql:host=localhost;port=3306;dbname=cake;charset=utf8';
  45. $expected = [
  46. 'persistent' => true,
  47. 'host' => 'localhost',
  48. 'username' => 'root',
  49. 'password' => '',
  50. 'database' => 'cake',
  51. 'port' => '3306',
  52. 'flags' => [],
  53. 'encoding' => 'utf8',
  54. 'timezone' => null,
  55. 'init' => [],
  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($dsn, $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. 'username' => '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', 'connection'],
  87. [$config]
  88. );
  89. $dsn = 'mysql:host=foo;port=3440;dbname=bar;charset=a-language';
  90. $expected = $config;
  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. ];
  97. $connection = $this->getMock('StdClass', ['exec']);
  98. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  99. $connection->expects($this->at(1))->method('exec')->with('this too');
  100. $connection->expects($this->at(2))->method('exec')->with("SET time_zone = 'Antartica'");
  101. $connection->expects($this->exactly(3))->method('exec');
  102. $driver->expects($this->once())->method('_connect')
  103. ->with($dsn, $expected);
  104. $driver->expects($this->any())->method('connection')
  105. ->will($this->returnValue($connection));
  106. $driver->connect($config);
  107. }
  108. }