MysqlTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Driver;
  16. use Cake\Datasource\ConnectionManager;
  17. use Cake\TestSuite\TestCase;
  18. use PDO;
  19. /**
  20. * Tests Mysql driver
  21. */
  22. class MysqlTest extends TestCase
  23. {
  24. /**
  25. * setup
  26. *
  27. * @return void
  28. */
  29. public function setup()
  30. {
  31. parent::setUp();
  32. $config = ConnectionManager::getConfig('test');
  33. $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Not using Mysql for test config');
  34. }
  35. /**
  36. * Test connecting to Mysql with default configuration
  37. *
  38. * @return void
  39. */
  40. public function testConnectionConfigDefault()
  41. {
  42. $driver = $this->getMockBuilder('Cake\Database\Driver\Mysql')
  43. ->setMethods(['_connect', 'connection'])
  44. ->getMock();
  45. $dsn = 'mysql:host=localhost;port=3306;dbname=cake;charset=utf8mb4';
  46. $expected = [
  47. 'persistent' => true,
  48. 'host' => 'localhost',
  49. 'username' => 'root',
  50. 'password' => '',
  51. 'database' => 'cake',
  52. 'port' => '3306',
  53. 'flags' => [],
  54. 'encoding' => 'utf8mb4',
  55. 'timezone' => null,
  56. 'init' => ['SET NAMES utf8mb4'],
  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->getMockBuilder('StdClass')
  64. ->setMethods(['exec'])
  65. ->getMock();
  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' => 'some-encoding',
  89. 'timezone' => 'Antarctica',
  90. 'init' => [
  91. 'Execute this',
  92. 'this too',
  93. ]
  94. ];
  95. $driver = $this->getMockBuilder('Cake\Database\Driver\Mysql')
  96. ->setMethods(['_connect', 'connection'])
  97. ->setConstructorArgs([$config])
  98. ->getMock();
  99. $dsn = 'mysql:host=foo;port=3440;dbname=bar;charset=some-encoding';
  100. $expected = $config;
  101. $expected['init'][] = "SET time_zone = 'Antarctica'";
  102. $expected['init'][] = 'SET NAMES some-encoding';
  103. $expected['flags'] += [
  104. PDO::ATTR_PERSISTENT => false,
  105. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  106. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  107. ];
  108. $connection = $this->getMockBuilder('StdClass')
  109. ->setMethods(['exec'])
  110. ->getMock();
  111. $connection->expects($this->at(0))->method('exec')->with('Execute this');
  112. $connection->expects($this->at(1))->method('exec')->with('this too');
  113. $connection->expects($this->at(2))->method('exec')->with("SET time_zone = 'Antarctica'");
  114. $connection->expects($this->at(3))->method('exec')->with('SET NAMES some-encoding');
  115. $connection->expects($this->exactly(4))->method('exec');
  116. $driver->expects($this->once())->method('_connect')
  117. ->with($dsn, $expected);
  118. $driver->expects($this->any())->method('connection')
  119. ->will($this->returnValue($connection));
  120. $driver->connect($config);
  121. }
  122. /**
  123. * Test isConnected
  124. *
  125. * @return void
  126. */
  127. public function testIsConnected()
  128. {
  129. $connection = ConnectionManager::get('test');
  130. $connection->disconnect();
  131. $this->assertFalse($connection->isConnected(), 'Not connected now.');
  132. $connection->connect();
  133. $this->assertTrue($connection->isConnected(), 'Should be connected.');
  134. }
  135. public function testRollbackTransactionAutoConnect()
  136. {
  137. $connection = ConnectionManager::get('test');
  138. $connection->disconnect();
  139. $driver = $connection->getDriver();
  140. $this->assertFalse($driver->rollbackTransaction());
  141. $this->assertTrue($driver->isConnected());
  142. }
  143. public function testCommitTransactionAutoConnect()
  144. {
  145. $connection = ConnectionManager::get('test');
  146. $driver = $connection->getDriver();
  147. $this->assertFalse($driver->commitTransaction());
  148. $this->assertTrue($driver->isConnected());
  149. }
  150. }