MysqlTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 = ConnectionManager::config('test');
  37. $this->skipIf(strpos($config['driver'], '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', 'connection']);
  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' => ['SET NAMES utf8'],
  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. $connection = $this->getMock('StdClass', ['exec']);
  66. $driver->expects($this->once())->method('_connect')
  67. ->with($dsn, $expected);
  68. $driver->expects($this->any())
  69. ->method('connection')
  70. ->will($this->returnValue($connection));
  71. $driver->connect([]);
  72. }
  73. /**
  74. * Test connecting to Mysql with custom configuration
  75. *
  76. * @return void
  77. */
  78. public function testConnectionConfigCustom()
  79. {
  80. $config = [
  81. 'persistent' => false,
  82. 'host' => 'foo',
  83. 'database' => 'bar',
  84. 'username' => 'user',
  85. 'password' => 'pass',
  86. 'port' => 3440,
  87. 'flags' => [1 => true, 2 => false],
  88. 'encoding' => 'a-language',
  89. 'timezone' => 'Antartica',
  90. 'init' => [
  91. 'Execute this',
  92. 'this too',
  93. ]
  94. ];
  95. $driver = $this->getMock(
  96. 'Cake\Database\Driver\Mysql',
  97. ['_connect', 'connection'],
  98. [$config]
  99. );
  100. $dsn = 'mysql:host=foo;port=3440;dbname=bar;charset=a-language';
  101. $expected = $config;
  102. $expected['init'][] = "SET time_zone = 'Antartica'";
  103. $expected['init'][] = "SET NAMES a-language";
  104. $expected['flags'] += [
  105. PDO::ATTR_PERSISTENT => false,
  106. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  107. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  108. ];
  109. $connection = $this->getMock('StdClass', ['exec']);
  110. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  111. $connection->expects($this->at(1))->method('exec')->with('this too');
  112. $connection->expects($this->at(2))->method('exec')->with("SET time_zone = 'Antartica'");
  113. $connection->expects($this->at(3))->method('exec')->with("SET NAMES a-language");
  114. $connection->expects($this->exactly(4))->method('exec');
  115. $driver->expects($this->once())->method('_connect')
  116. ->with($dsn, $expected);
  117. $driver->expects($this->any())->method('connection')
  118. ->will($this->returnValue($connection));
  119. $driver->connect($config);
  120. }
  121. /**
  122. * Test isConnected
  123. *
  124. * @return void
  125. */
  126. public function testIsConnected()
  127. {
  128. $connection = ConnectionManager::get('test');
  129. $connection->disconnect();
  130. $this->assertFalse($connection->isConnected(), 'Not connected now.');
  131. $connection->connect();
  132. $this->assertTrue($connection->isConnected(), 'Should be connected.');
  133. }
  134. }