MysqlTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /**
  29. * setup
  30. *
  31. * @return void
  32. */
  33. public function setup()
  34. {
  35. parent::setUp();
  36. $config = Configure::read('Datasource.test');
  37. $this->skipIf(strpos($config['datasource'], 'Mysql') === false, 'Not using Mysql for test config');
  38. }
  39. /**
  40. * Test connecting to Mysql with default configuration
  41. *
  42. * @return void
  43. */
  44. public function testConnectionConfigDefault()
  45. {
  46. $driver = $this->getMock('Cake\Database\Driver\Mysql', ['_connect']);
  47. $dsn = 'mysql:host=localhost;port=3306;dbname=cake;charset=utf8';
  48. $expected = [
  49. 'persistent' => true,
  50. 'host' => 'localhost',
  51. 'username' => 'root',
  52. 'password' => '',
  53. 'database' => 'cake',
  54. 'port' => '3306',
  55. 'flags' => [],
  56. 'encoding' => 'utf8',
  57. 'timezone' => null,
  58. 'init' => [],
  59. ];
  60. $expected['flags'] += [
  61. PDO::ATTR_PERSISTENT => true,
  62. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  63. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  64. ];
  65. $driver->expects($this->once())->method('_connect')
  66. ->with($dsn, $expected);
  67. $driver->connect([]);
  68. }
  69. /**
  70. * Test connecting to Mysql with custom configuration
  71. *
  72. * @return void
  73. */
  74. public function testConnectionConfigCustom()
  75. {
  76. $config = [
  77. 'persistent' => false,
  78. 'host' => 'foo',
  79. 'database' => 'bar',
  80. 'username' => 'user',
  81. 'password' => 'pass',
  82. 'port' => 3440,
  83. 'flags' => [1 => true, 2 => false],
  84. 'encoding' => 'a-language',
  85. 'timezone' => 'Antartica',
  86. 'init' => ['Execute this', 'this too']
  87. ];
  88. $driver = $this->getMock(
  89. 'Cake\Database\Driver\Mysql',
  90. ['_connect', 'connection'],
  91. [$config]
  92. );
  93. $dsn = 'mysql:host=foo;port=3440;dbname=bar;charset=a-language';
  94. $expected = $config;
  95. $expected['init'][] = "SET time_zone = 'Antartica'";
  96. $expected['flags'] += [
  97. PDO::ATTR_PERSISTENT => false,
  98. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  99. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  100. ];
  101. $connection = $this->getMock('StdClass', ['exec']);
  102. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  103. $connection->expects($this->at(1))->method('exec')->with('this too');
  104. $connection->expects($this->at(2))->method('exec')->with("SET time_zone = 'Antartica'");
  105. $connection->expects($this->exactly(3))->method('exec');
  106. $driver->expects($this->once())->method('_connect')
  107. ->with($dsn, $expected);
  108. $driver->expects($this->any())->method('connection')
  109. ->will($this->returnValue($connection));
  110. $driver->connect($config);
  111. }
  112. }