DriverTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.2.12
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Database;
  17. use Cake\Database\Driver;
  18. use Cake\Database\Driver\Mysql;
  19. use Cake\Database\Exception\MissingConnectionException;
  20. use Cake\Database\Query;
  21. use Cake\Database\QueryCompiler;
  22. use Cake\Database\Schema\TableSchema;
  23. use Cake\Database\ValueBinder;
  24. use Cake\TestSuite\TestCase;
  25. use PDO;
  26. /**
  27. * Tests Driver class
  28. */
  29. class DriverTest extends TestCase
  30. {
  31. /**
  32. * Setup.
  33. */
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. $this->driver = $this->getMockForAbstractClass(Driver::class);
  38. }
  39. /**
  40. * Test if building the object throws an exception if we're not passing
  41. * required config data.
  42. *
  43. * @return void
  44. */
  45. public function testConstructorException()
  46. {
  47. $arg = ['login' => 'Bear'];
  48. try {
  49. $this->getMockForAbstractClass(Driver::class, [$arg]);
  50. } catch (\Exception $e) {
  51. $this->assertStringContainsString(
  52. 'Please pass "username" instead of "login" for connecting to the database',
  53. $e->getMessage()
  54. );
  55. }
  56. }
  57. /**
  58. * Test the constructor.
  59. *
  60. * @return void
  61. */
  62. public function testConstructor()
  63. {
  64. $arg = ['quoteIdentifiers' => true];
  65. $driver = $this->getMockForAbstractClass(Driver::class, [$arg]);
  66. $this->assertTrue($driver->isAutoQuotingEnabled());
  67. $arg = ['username' => 'GummyBear'];
  68. $driver = $this->getMockForAbstractClass(Driver::class, [$arg]);
  69. $this->assertFalse($driver->isAutoQuotingEnabled());
  70. }
  71. /**
  72. * Test supportsSavePoints().
  73. *
  74. * @return void
  75. */
  76. public function testSupportsSavePoints()
  77. {
  78. $result = $this->driver->supportsSavePoints();
  79. $this->assertTrue($result);
  80. }
  81. /**
  82. * Test supportsQuoting().
  83. *
  84. * @return void
  85. */
  86. public function testSupportsQuoting()
  87. {
  88. $connection = $this->getMockBuilder(PDO::class)
  89. ->disableOriginalConstructor()
  90. ->setMethods(['getAttribute'])
  91. ->getMock();
  92. $connection
  93. ->expects($this->once())
  94. ->method('getAttribute')
  95. ->with(PDO::ATTR_DRIVER_NAME)
  96. ->willReturn('mysql');
  97. $this->driver->setConnection($connection);
  98. $result = $this->driver->supportsQuoting();
  99. $this->assertTrue($result);
  100. }
  101. /**
  102. * Test schemaValue().
  103. * Uses a provider for all the different values we can pass to the method.
  104. *
  105. * @dataProvider schemaValueProvider
  106. * @return void
  107. */
  108. public function testSchemaValue($input, $expected)
  109. {
  110. $result = $this->driver->schemaValue($input);
  111. $this->assertSame($expected, $result);
  112. }
  113. /**
  114. * Test schemaValue().
  115. * Asserting that quote() is being called because none of the conditions were met before.
  116. *
  117. * @return void
  118. */
  119. public function testSchemaValueConnectionQuoting()
  120. {
  121. $value = 'string';
  122. $connection = $this->getMockBuilder(PDO::class)
  123. ->disableOriginalConstructor()
  124. ->setMethods(['quote'])
  125. ->getMock();
  126. $connection
  127. ->expects($this->once())
  128. ->method('quote')
  129. ->with($value, PDO::PARAM_STR)
  130. ->will($this->returnValue('string'));
  131. $this->driver->setConnection($connection);
  132. $this->driver->schemaValue($value);
  133. }
  134. /**
  135. * Test lastInsertId().
  136. *
  137. * @return void
  138. */
  139. public function testLastInsertId()
  140. {
  141. $connection = $this->getMockBuilder(Mysql::class)
  142. ->disableOriginalConstructor()
  143. ->setMethods(['lastInsertId'])
  144. ->getMock();
  145. $connection
  146. ->expects($this->once())
  147. ->method('lastInsertId')
  148. ->willReturn('all-the-bears');
  149. $this->driver->setConnection($connection);
  150. $this->assertSame('all-the-bears', $this->driver->lastInsertId());
  151. }
  152. /**
  153. * Test isConnected().
  154. *
  155. * @return void
  156. */
  157. public function testIsConnected()
  158. {
  159. $this->assertFalse($this->driver->isConnected());
  160. $connection = $this->getMockBuilder(PDO::class)
  161. ->disableOriginalConstructor()
  162. ->setMethods(['query'])
  163. ->getMock();
  164. $connection
  165. ->expects($this->once())
  166. ->method('query')
  167. ->willReturn(true);
  168. $this->driver->setConnection($connection);
  169. $this->assertTrue($this->driver->isConnected());
  170. }
  171. /**
  172. * test autoQuoting().
  173. *
  174. * @return void
  175. */
  176. public function testAutoQuoting()
  177. {
  178. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  179. $this->assertSame($this->driver, $this->driver->enableAutoQuoting(true));
  180. $this->assertTrue($this->driver->isAutoQuotingEnabled());
  181. $this->driver->disableAutoQuoting();
  182. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  183. }
  184. /**
  185. * Test compileQuery().
  186. *
  187. * @return void
  188. */
  189. public function testCompileQuery()
  190. {
  191. $compiler = $this->getMockBuilder(QueryCompiler::class)
  192. ->setMethods(['compile'])
  193. ->getMock();
  194. $compiler
  195. ->expects($this->once())
  196. ->method('compile')
  197. ->willReturn('1');
  198. $driver = $this->getMockBuilder(Driver::class)
  199. ->setMethods(['newCompiler', 'queryTranslator'])
  200. ->getMockForAbstractClass();
  201. $driver
  202. ->expects($this->once())
  203. ->method('newCompiler')
  204. ->willReturn($compiler);
  205. $driver
  206. ->expects($this->once())
  207. ->method('queryTranslator')
  208. ->willReturn(function ($query) {
  209. return $query;
  210. });
  211. $query = $this->getMockBuilder(Query::class)
  212. ->disableOriginalConstructor()
  213. ->getMock();
  214. $query->method('type')->will($this->returnValue('select'));
  215. $result = $driver->compileQuery($query, new ValueBinder());
  216. $this->assertIsArray($result);
  217. $this->assertSame($query, $result[0]);
  218. $this->assertSame('1', $result[1]);
  219. }
  220. /**
  221. * Test newCompiler().
  222. *
  223. * @return void
  224. */
  225. public function testNewCompiler()
  226. {
  227. $this->assertInstanceOf(QueryCompiler::class, $this->driver->newCompiler());
  228. }
  229. /**
  230. * Test newTableSchema().
  231. *
  232. * @return void
  233. */
  234. public function testNewTableSchema()
  235. {
  236. $tableName = 'articles';
  237. $actual = $this->driver->newTableSchema($tableName);
  238. $this->assertInstanceOf(TableSchema::class, $actual);
  239. $this->assertEquals($tableName, $actual->name());
  240. }
  241. /**
  242. * Test __destruct().
  243. *
  244. * @return void
  245. */
  246. public function testDestructor()
  247. {
  248. $this->driver->setConnection(true);
  249. $this->driver->__destruct();
  250. $this->expectException(MissingConnectionException::class);
  251. $this->driver->getConnection();
  252. }
  253. /**
  254. * Data provider for testSchemaValue().
  255. *
  256. * @return array
  257. */
  258. public function schemaValueProvider()
  259. {
  260. return [
  261. [null, 'NULL'],
  262. [false, 'FALSE'],
  263. [true, 'TRUE'],
  264. [1, '1'],
  265. ['0', '0'],
  266. ['42', '42'],
  267. ];
  268. }
  269. }