MysqlTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\Datasource\ConnectionManager;
  18. use Cake\TestSuite\TestCase;
  19. use \PDO;
  20. /**
  21. * Tests Mysql driver
  22. *
  23. */
  24. class MysqlTest extends TestCase
  25. {
  26. /**
  27. * setup
  28. *
  29. * @return void
  30. */
  31. public function setup()
  32. {
  33. parent::setUp();
  34. $config = ConnectionManager::config('test');
  35. $this->skipIf(strpos($config['driver'], '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. {
  44. $driver = $this->getMock('Cake\Database\Driver\Mysql', ['_connect', 'connection']);
  45. $dsn = 'mysql:host=localhost;port=3306;dbname=cake;charset=utf8';
  46. $expected = [
  47. 'persistent' => true,
  48. 'host' => 'localhost',
  49. 'username' => 'root',
  50. 'password' => '',
  51. 'database' => 'cake',
  52. 'port' => '3306',
  53. 'flags' => [],
  54. 'encoding' => 'utf8',
  55. 'timezone' => null,
  56. 'init' => ['SET NAMES utf8'],
  57. ];
  58. $expected['flags'] += [
  59. PDO::ATTR_PERSISTENT => true,
  60. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  61. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  62. ];
  63. $connection = $this->getMock('StdClass', ['exec']);
  64. $driver->expects($this->once())->method('_connect')
  65. ->with($dsn, $expected);
  66. $driver->expects($this->any())
  67. ->method('connection')
  68. ->will($this->returnValue($connection));
  69. $driver->connect([]);
  70. }
  71. /**
  72. * Test connecting to Mysql with custom configuration
  73. *
  74. * @return void
  75. */
  76. public function testConnectionConfigCustom()
  77. {
  78. $config = [
  79. 'persistent' => false,
  80. 'host' => 'foo',
  81. 'database' => 'bar',
  82. 'username' => 'user',
  83. 'password' => 'pass',
  84. 'port' => 3440,
  85. 'flags' => [1 => true, 2 => false],
  86. 'encoding' => 'a-language',
  87. 'timezone' => 'Antartica',
  88. 'init' => [
  89. 'Execute this',
  90. 'this too',
  91. ]
  92. ];
  93. $driver = $this->getMock(
  94. 'Cake\Database\Driver\Mysql',
  95. ['_connect', 'connection'],
  96. [$config]
  97. );
  98. $dsn = 'mysql:host=foo;port=3440;dbname=bar;charset=a-language';
  99. $expected = $config;
  100. $expected['init'][] = "SET time_zone = 'Antartica'";
  101. $expected['init'][] = "SET NAMES a-language";
  102. $expected['flags'] += [
  103. PDO::ATTR_PERSISTENT => false,
  104. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  105. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  106. ];
  107. $connection = $this->getMock('StdClass', ['exec']);
  108. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  109. $connection->expects($this->at(1))->method('exec')->with('this too');
  110. $connection->expects($this->at(2))->method('exec')->with("SET time_zone = 'Antartica'");
  111. $connection->expects($this->at(3))->method('exec')->with("SET NAMES a-language");
  112. $connection->expects($this->exactly(4))->method('exec');
  113. $driver->expects($this->once())->method('_connect')
  114. ->with($dsn, $expected);
  115. $driver->expects($this->any())->method('connection')
  116. ->will($this->returnValue($connection));
  117. $driver->connect($config);
  118. }
  119. /**
  120. * Test isConnected
  121. *
  122. * @return void
  123. */
  124. public function testIsConnected()
  125. {
  126. $connection = ConnectionManager::get('test');
  127. $connection->disconnect();
  128. $this->assertFalse($connection->isConnected(), 'Not connected now.');
  129. $connection->connect();
  130. $this->assertTrue($connection->isConnected(), 'Should be connected.');
  131. }
  132. }