DriverTest.php 8.0 KB

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