DriverTest.php 7.4 KB

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