DriverTest.php 7.5 KB

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