MysqlTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database\Driver;
  17. use Cake\Database\Driver\Mysql;
  18. use Cake\Database\DriverFeatureEnum;
  19. use Cake\Datasource\ConnectionManager;
  20. use Cake\TestSuite\TestCase;
  21. use PDO;
  22. use PHPUnit\Framework\Attributes\DataProvider;
  23. /**
  24. * Tests MySQL driver
  25. */
  26. class MysqlTest extends TestCase
  27. {
  28. /**
  29. * setup
  30. */
  31. public function setup(): void
  32. {
  33. parent::setUp();
  34. $config = ConnectionManager::getConfig('test');
  35. $this->skipIf(!str_contains($config['driver'], 'Mysql'), 'Not using Mysql for test config');
  36. }
  37. /**
  38. * Test connecting to MySQL with default configuration
  39. */
  40. public function testConnectionConfigDefault(): void
  41. {
  42. $driver = $this->getMockBuilder(Mysql::class)
  43. ->onlyMethods(['createPdo'])
  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' => [],
  57. 'log' => false,
  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. $driver->expects($this->once())->method('createPdo')
  65. ->with($dsn, $expected);
  66. $driver->connect();
  67. }
  68. /**
  69. * Test connecting to MySQL with custom configuration
  70. */
  71. public function testConnectionConfigCustom(): void
  72. {
  73. $config = [
  74. 'persistent' => false,
  75. 'host' => 'foo',
  76. 'database' => 'bar',
  77. 'username' => 'user',
  78. 'password' => 'pass',
  79. 'port' => 3440,
  80. 'flags' => [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'],
  81. 'encoding' => null,
  82. 'timezone' => 'Antarctica',
  83. 'init' => [
  84. 'Execute this',
  85. 'this too',
  86. ],
  87. 'log' => false,
  88. ];
  89. $driver = $this->getMockBuilder(Mysql::class)
  90. ->onlyMethods(['createPdo'])
  91. ->setConstructorArgs([$config])
  92. ->getMock();
  93. $dsn = 'mysql:host=foo;port=3440;dbname=bar';
  94. $expected = $config;
  95. $expected['init'][] = "SET time_zone = 'Antarctica'";
  96. $expected['flags'] += [
  97. PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  98. PDO::ATTR_PERSISTENT => false,
  99. PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
  100. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  101. ];
  102. $connection = $this->getMockBuilder('PDO')
  103. ->disableOriginalConstructor()
  104. ->onlyMethods(['exec'])
  105. ->getMock();
  106. $connection->expects($this->exactly(3))
  107. ->method('exec')
  108. ->with(
  109. ...self::withConsecutive(['Execute this'], ['this too'], ["SET time_zone = 'Antarctica'"])
  110. );
  111. $driver->expects($this->once())->method('createPdo')
  112. ->with($dsn, $expected)
  113. ->willReturn($connection);
  114. $driver->connect();
  115. }
  116. /**
  117. * Test schema
  118. */
  119. public function testSchema(): void
  120. {
  121. $connection = ConnectionManager::get('test');
  122. $config = ConnectionManager::getConfig('test');
  123. $this->assertEquals($config['database'], $connection->getDriver()->schema());
  124. }
  125. /**
  126. * Test isConnected
  127. */
  128. public function testIsConnected(): void
  129. {
  130. $connection = ConnectionManager::get('test');
  131. $connection->getDriver()->disconnect();
  132. $this->assertFalse($connection->getDriver()->isConnected(), 'Not connected now.');
  133. $connection->getDriver()->connect();
  134. $this->assertTrue($connection->getDriver()->isConnected(), 'Should be connected.');
  135. }
  136. public function testRollbackTransactionAutoConnect(): void
  137. {
  138. $connection = ConnectionManager::get('test');
  139. $connection->getDriver()->disconnect();
  140. $driver = $connection->getDriver();
  141. $this->assertFalse($driver->rollbackTransaction());
  142. $this->assertTrue($driver->isConnected());
  143. }
  144. public function testCommitTransactionAutoConnect(): void
  145. {
  146. $connection = ConnectionManager::get('test');
  147. $driver = $connection->getDriver();
  148. $this->assertFalse($driver->commitTransaction());
  149. $this->assertTrue($driver->isConnected());
  150. }
  151. /**
  152. * @param string $dbVersion
  153. * @param string $expectedVersion
  154. */
  155. #[DataProvider('versionStringProvider')]
  156. public function testVersion($dbVersion, $expectedVersion): void
  157. {
  158. /** @var \PHPUnit\Framework\MockObject\MockObject&\PDO $connection */
  159. $connection = $this->getMockBuilder(PDO::class)
  160. ->disableOriginalConstructor()
  161. ->onlyMethods(['getAttribute'])
  162. ->getMock();
  163. $connection->expects($this->once())
  164. ->method('getAttribute')
  165. ->with(PDO::ATTR_SERVER_VERSION)
  166. ->willReturn($dbVersion);
  167. /** @var \PHPUnit\Framework\MockObject\MockObject&\Cake\Database\Driver\Mysql $driver */
  168. $driver = $this->getMockBuilder(Mysql::class)
  169. ->onlyMethods(['createPdo'])
  170. ->getMock();
  171. $driver->expects($this->once())
  172. ->method('createPdo')
  173. ->willReturn($connection);
  174. $result = $driver->version();
  175. $this->assertSame($expectedVersion, $result);
  176. }
  177. public static function versionStringProvider(): array
  178. {
  179. return [
  180. ['10.2.23-MariaDB', '10.2.23-MariaDB'],
  181. ['5.5.5-10.2.23-MariaDB', '10.2.23-MariaDB'],
  182. ['5.5.5-10.4.13-MariaDB-1:10.4.13+maria~focal', '10.4.13-MariaDB-1'],
  183. ['8.0.0', '8.0.0'],
  184. ];
  185. }
  186. /**
  187. * Tests driver-specific feature support check.
  188. */
  189. public function testSupports(): void
  190. {
  191. $driver = ConnectionManager::get('test')->getDriver();
  192. $this->skipIf(!$driver instanceof Mysql);
  193. $serverType = $driver->isMariadb() ? 'mariadb' : 'mysql';
  194. $featureVersions = [
  195. 'mysql' => [
  196. 'json' => '5.7.0',
  197. 'cte' => '8.0.0',
  198. 'window' => '8.0.0',
  199. 'intersect' => '8.0.31',
  200. 'intersect-all' => '8.0.31',
  201. ],
  202. 'mariadb' => [
  203. 'json' => '10.2.7',
  204. 'cte' => '10.2.1',
  205. 'window' => '10.2.0',
  206. 'intersect' => '10.3.0',
  207. 'intersect-all' => '10.5.0',
  208. ],
  209. ];
  210. foreach ($featureVersions[$serverType] as $feature => $version) {
  211. $this->assertSame(
  212. version_compare($driver->version(), $version, '>='),
  213. $driver->supports(DriverFeatureEnum::from($feature))
  214. );
  215. }
  216. $this->assertTrue($driver->supports(DriverFeatureEnum::DISABLE_CONSTRAINT_WITHOUT_TRANSACTION));
  217. $this->assertTrue($driver->supports(DriverFeatureEnum::SAVEPOINT));
  218. $this->assertFalse($driver->supports(DriverFeatureEnum::TRUNCATE_WITH_CONSTRAINTS));
  219. }
  220. /**
  221. * Tests identifier quoting
  222. */
  223. public function testQuoteIdentifier(): void
  224. {
  225. $driver = new Mysql();
  226. $result = $driver->quoteIdentifier('name');
  227. $expected = '`name`';
  228. $this->assertEquals($expected, $result);
  229. $result = $driver->quoteIdentifier('Model.*');
  230. $expected = '`Model`.*';
  231. $this->assertEquals($expected, $result);
  232. $result = $driver->quoteIdentifier('Items.No_ 2');
  233. $expected = '`Items`.`No_ 2`';
  234. $this->assertEquals($expected, $result);
  235. $result = $driver->quoteIdentifier('Items.No_ 2 thing');
  236. $expected = '`Items`.`No_ 2 thing`';
  237. $this->assertEquals($expected, $result);
  238. $result = $driver->quoteIdentifier('Items.No_ 2 thing AS thing');
  239. $expected = '`Items`.`No_ 2 thing` AS `thing`';
  240. $this->assertEquals($expected, $result);
  241. $result = $driver->quoteIdentifier('Items.Item Category Code = :c1');
  242. $expected = '`Items`.`Item Category Code` = :c1';
  243. $this->assertEquals($expected, $result);
  244. $result = $driver->quoteIdentifier('MTD()');
  245. $expected = 'MTD()';
  246. $this->assertEquals($expected, $result);
  247. $result = $driver->quoteIdentifier('(sm)');
  248. $expected = '(sm)';
  249. $this->assertEquals($expected, $result);
  250. $result = $driver->quoteIdentifier('name AS x');
  251. $expected = '`name` AS `x`';
  252. $this->assertEquals($expected, $result);
  253. $result = $driver->quoteIdentifier('Model.name AS x');
  254. $expected = '`Model`.`name` AS `x`';
  255. $this->assertEquals($expected, $result);
  256. $result = $driver->quoteIdentifier('Function(Something.foo)');
  257. $expected = 'Function(`Something`.`foo`)';
  258. $this->assertEquals($expected, $result);
  259. $result = $driver->quoteIdentifier('Function(SubFunction(Something.foo))');
  260. $expected = 'Function(SubFunction(`Something`.`foo`))';
  261. $this->assertEquals($expected, $result);
  262. $result = $driver->quoteIdentifier('Function(Something.foo) AS x');
  263. $expected = 'Function(`Something`.`foo`) AS `x`';
  264. $this->assertEquals($expected, $result);
  265. $result = $driver->quoteIdentifier('name-with-minus');
  266. $expected = '`name-with-minus`';
  267. $this->assertEquals($expected, $result);
  268. $result = $driver->quoteIdentifier('my-name');
  269. $expected = '`my-name`';
  270. $this->assertEquals($expected, $result);
  271. $result = $driver->quoteIdentifier('Foo-Model.*');
  272. $expected = '`Foo-Model`.*';
  273. $this->assertEquals($expected, $result);
  274. $result = $driver->quoteIdentifier('Team.P%');
  275. $expected = '`Team`.`P%`';
  276. $this->assertEquals($expected, $result);
  277. $result = $driver->quoteIdentifier('Team.G/G');
  278. $expected = '`Team`.`G/G`';
  279. $this->assertEquals($expected, $result);
  280. $result = $driver->quoteIdentifier('Model.name as y');
  281. $expected = '`Model`.`name` AS `y`';
  282. $this->assertEquals($expected, $result);
  283. $result = $driver->quoteIdentifier('nämé');
  284. $expected = '`nämé`';
  285. $this->assertEquals($expected, $result);
  286. $result = $driver->quoteIdentifier('aßa.nämé');
  287. $expected = '`aßa`.`nämé`';
  288. $this->assertEquals($expected, $result);
  289. $result = $driver->quoteIdentifier('aßa.*');
  290. $expected = '`aßa`.*';
  291. $this->assertEquals($expected, $result);
  292. $result = $driver->quoteIdentifier('Modeß.nämé as y');
  293. $expected = '`Modeß`.`nämé` AS `y`';
  294. $this->assertEquals($expected, $result);
  295. $result = $driver->quoteIdentifier('Model.näme Datum as y');
  296. $expected = '`Model`.`näme Datum` AS `y`';
  297. $this->assertEquals($expected, $result);
  298. }
  299. }