DriverTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. $connection = $this->getMockBuilder(PDO::class)
  81. ->disableOriginalConstructor()
  82. ->setMethods(['getAttribute'])
  83. ->getMock();
  84. $connection
  85. ->expects($this->once())
  86. ->method('getAttribute')
  87. ->with(PDO::ATTR_DRIVER_NAME)
  88. ->willReturn('mysql');
  89. $this->driver->setConnection($connection);
  90. $result = $this->driver->supportsQuoting();
  91. $this->assertTrue($result);
  92. }
  93. /**
  94. * Test schemaValue().
  95. * Uses a provider for all the different values we can pass to the method.
  96. *
  97. * @dataProvider schemaValueProvider
  98. * @return void
  99. */
  100. public function testSchemaValue($input, $expected)
  101. {
  102. $result = $this->driver->schemaValue($input);
  103. $this->assertSame($expected, $result);
  104. }
  105. /**
  106. * Test schemaValue().
  107. * Asserting that quote() is being called because none of the conditions were met before.
  108. *
  109. * @return void
  110. */
  111. public function testSchemaValueConnectionQuoting()
  112. {
  113. $value = 'string';
  114. $connection = $this->getMockBuilder(PDO::class)
  115. ->disableOriginalConstructor()
  116. ->setMethods(['quote'])
  117. ->getMock();
  118. $connection
  119. ->expects($this->once())
  120. ->method('quote')
  121. ->with($value, PDO::PARAM_STR);
  122. $this->driver->setConnection($connection);
  123. $this->driver->schemaValue($value);
  124. }
  125. /**
  126. * Test lastInsertId().
  127. *
  128. * @return void
  129. */
  130. public function testLastInsertId()
  131. {
  132. $connection = $this->getMockBuilder(Mysql::class)
  133. ->disableOriginalConstructor()
  134. ->setMethods(['lastInsertId'])
  135. ->getMock();
  136. $connection
  137. ->expects($this->once())
  138. ->method('lastInsertId')
  139. ->willReturn('all-the-bears');
  140. $this->driver->setConnection($connection);
  141. $this->assertSame('all-the-bears', $this->driver->lastInsertId());
  142. }
  143. /**
  144. * Test isConnected().
  145. *
  146. * @return void
  147. */
  148. public function testIsConnected()
  149. {
  150. $this->assertFalse($this->driver->isConnected());
  151. $connection = $this->getMockBuilder(PDO::class)
  152. ->disableOriginalConstructor()
  153. ->setMethods(['query'])
  154. ->getMock();
  155. $connection
  156. ->expects($this->once())
  157. ->method('query')
  158. ->willReturn(true);
  159. $this->driver->setConnection($connection);
  160. $this->assertTrue($this->driver->isConnected());
  161. }
  162. /**
  163. * test autoQuoting().
  164. *
  165. * @return void
  166. */
  167. public function testAutoQuoting()
  168. {
  169. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  170. $this->assertSame($this->driver, $this->driver->enableAutoQuoting(true));
  171. $this->assertTrue($this->driver->isAutoQuotingEnabled());
  172. $this->driver->enableAutoQuoting(false);
  173. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  174. $this->driver->enableAutoQuoting('string');
  175. $this->assertTrue($this->driver->isAutoQuotingEnabled());
  176. $this->driver->enableAutoQuoting('0');
  177. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  178. $this->driver->enableAutoQuoting(1);
  179. $this->assertTrue($this->driver->isAutoQuotingEnabled());
  180. $this->driver->enableAutoQuoting(0);
  181. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  182. }
  183. /**
  184. * Test compileQuery().
  185. *
  186. * @return void
  187. */
  188. public function testCompileQuery()
  189. {
  190. $compiler = $this->getMockBuilder(QueryCompiler::class)
  191. ->setMethods(['compile'])
  192. ->getMock();
  193. $compiler
  194. ->expects($this->once())
  195. ->method('compile')
  196. ->willReturn(true);
  197. $driver = $this->getMockBuilder(Driver::class)
  198. ->setMethods(['newCompiler', 'queryTranslator'])
  199. ->getMockForAbstractClass();
  200. $driver
  201. ->expects($this->once())
  202. ->method('newCompiler')
  203. ->willReturn($compiler);
  204. $driver
  205. ->expects($this->once())
  206. ->method('queryTranslator')
  207. ->willReturn(function ($query) {
  208. return $query;
  209. });
  210. $query = $this->getMockBuilder(Query::class)
  211. ->disableOriginalConstructor()
  212. ->getMock();
  213. $result = $driver->compileQuery($query, new ValueBinder);
  214. $this->assertInternalType('array', $result);
  215. $this->assertSame($query, $result[0]);
  216. $this->assertTrue($result[1]);
  217. }
  218. /**
  219. * Test newCompiler().
  220. *
  221. * @return void
  222. */
  223. public function testNewCompiler()
  224. {
  225. $this->assertInstanceOf(QueryCompiler::class, $this->driver->newCompiler());
  226. }
  227. /**
  228. * Test __destruct().
  229. *
  230. * @return void
  231. */
  232. public function testDestructor()
  233. {
  234. $this->driver->setConnection(true);
  235. $this->driver->__destruct();
  236. $this->assertNull($this->driver->getConnection());
  237. }
  238. /**
  239. * Data provider for testSchemaValue().
  240. *
  241. * @return array
  242. */
  243. public function schemaValueProvider()
  244. {
  245. return [
  246. [null, 'NULL'],
  247. [false, 'FALSE'],
  248. [true, 'TRUE'],
  249. [1, '1'],
  250. ['0', '0'],
  251. ['42', '42']
  252. ];
  253. }
  254. }