DriverTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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\Schema\TableSchema;
  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. $this->driver->setConnection($connection);
  124. $this->driver->schemaValue($value);
  125. }
  126. /**
  127. * Test lastInsertId().
  128. *
  129. * @return void
  130. */
  131. public function testLastInsertId()
  132. {
  133. $connection = $this->getMockBuilder(Mysql::class)
  134. ->disableOriginalConstructor()
  135. ->setMethods(['lastInsertId'])
  136. ->getMock();
  137. $connection
  138. ->expects($this->once())
  139. ->method('lastInsertId')
  140. ->willReturn('all-the-bears');
  141. $this->driver->setConnection($connection);
  142. $this->assertSame('all-the-bears', $this->driver->lastInsertId());
  143. }
  144. /**
  145. * Test isConnected().
  146. *
  147. * @return void
  148. */
  149. public function testIsConnected()
  150. {
  151. $this->assertFalse($this->driver->isConnected());
  152. $connection = $this->getMockBuilder(PDO::class)
  153. ->disableOriginalConstructor()
  154. ->setMethods(['query'])
  155. ->getMock();
  156. $connection
  157. ->expects($this->once())
  158. ->method('query')
  159. ->willReturn(true);
  160. $this->driver->setConnection($connection);
  161. $this->assertTrue($this->driver->isConnected());
  162. }
  163. /**
  164. * test autoQuoting().
  165. *
  166. * @return void
  167. */
  168. public function testAutoQuoting()
  169. {
  170. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  171. $this->assertSame($this->driver, $this->driver->enableAutoQuoting(true));
  172. $this->assertTrue($this->driver->isAutoQuotingEnabled());
  173. $this->driver->disableAutoQuoting();
  174. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  175. $this->driver->enableAutoQuoting('string');
  176. $this->assertTrue($this->driver->isAutoQuotingEnabled());
  177. $this->driver->enableAutoQuoting('0');
  178. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  179. $this->driver->enableAutoQuoting(1);
  180. $this->assertTrue($this->driver->isAutoQuotingEnabled());
  181. $this->driver->enableAutoQuoting(0);
  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(true);
  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. $result = $driver->compileQuery($query, new ValueBinder());
  215. $this->assertInternalType('array', $result);
  216. $this->assertSame($query, $result[0]);
  217. $this->assertTrue($result[1]);
  218. }
  219. /**
  220. * Test newCompiler().
  221. *
  222. * @return void
  223. */
  224. public function testNewCompiler()
  225. {
  226. $this->assertInstanceOf(QueryCompiler::class, $this->driver->newCompiler());
  227. }
  228. /**
  229. * Test newTableSchema().
  230. *
  231. * @return void
  232. */
  233. public function testNewTableSchema()
  234. {
  235. $actual = $this->driver->newTableSchema('articles');
  236. $this->assertInstanceOf(TableSchema::class, $actual);
  237. }
  238. /**
  239. * Test __destruct().
  240. *
  241. * @return void
  242. */
  243. public function testDestructor()
  244. {
  245. $this->driver->setConnection(true);
  246. $this->driver->__destruct();
  247. $this->assertNull($this->driver->getConnection());
  248. }
  249. /**
  250. * Data provider for testSchemaValue().
  251. *
  252. * @return array
  253. */
  254. public function schemaValueProvider()
  255. {
  256. return [
  257. [null, 'NULL'],
  258. [false, 'FALSE'],
  259. [true, 'TRUE'],
  260. [1, '1'],
  261. ['0', '0'],
  262. ['42', '42']
  263. ];
  264. }
  265. }