MysqlTest.php 5.2 KB

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