DriverTest.php 7.0 KB

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