DriverTest.php 8.1 KB

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