DriverTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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\DriverInterface;
  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 Exception;
  26. use PDO;
  27. use PDOStatement;
  28. /**
  29. * Tests Driver class
  30. */
  31. class DriverTest extends TestCase
  32. {
  33. /**
  34. * @var \Cake\Database\Driver|\PHPUnit\Framework\MockObject\MockObject
  35. */
  36. protected $driver;
  37. /**
  38. * Setup.
  39. */
  40. public function setUp(): void
  41. {
  42. parent::setUp();
  43. $this->driver = $this->getMockForAbstractClass(Driver::class);
  44. }
  45. /**
  46. * Test if building the object throws an exception if we're not passing
  47. * required config data.
  48. */
  49. public function testConstructorException(): void
  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. public function testConstructor(): void
  65. {
  66. $arg = ['quoteIdentifiers' => true];
  67. $driver = $this->getMockForAbstractClass(Driver::class, [$arg]);
  68. $this->assertTrue($driver->isAutoQuotingEnabled());
  69. $arg = ['username' => 'GummyBear'];
  70. $driver = $this->getMockForAbstractClass(Driver::class, [$arg]);
  71. $this->assertFalse($driver->isAutoQuotingEnabled());
  72. }
  73. /**
  74. * Tests default implementation of feature support check.
  75. */
  76. public function testSupports(): void
  77. {
  78. $this->assertTrue($this->driver->supports(DriverInterface::FEATURE_SAVEPOINT));
  79. $this->assertTrue($this->driver->supports(DriverInterface::FEATURE_QUOTE));
  80. $this->assertFalse($this->driver->supports(DriverInterface::FEATURE_CTE));
  81. $this->assertFalse($this->driver->supports(DriverInterface::FEATURE_JSON));
  82. $this->assertFalse($this->driver->supports(DriverInterface::FEATURE_WINDOW));
  83. $this->assertFalse($this->driver->supports('this-is-fake'));
  84. }
  85. /**
  86. * Test schemaValue().
  87. * Uses a provider for all the different values we can pass to the method.
  88. *
  89. * @dataProvider schemaValueProvider
  90. * @param mixed $input
  91. */
  92. public function testSchemaValue($input, string $expected): void
  93. {
  94. $result = $this->driver->schemaValue($input);
  95. $this->assertSame($expected, $result);
  96. }
  97. /**
  98. * Test schemaValue().
  99. * Asserting that quote() is being called because none of the conditions were met before.
  100. */
  101. public function testSchemaValueConnectionQuoting(): void
  102. {
  103. $value = 'string';
  104. $connection = $this->getMockBuilder(PDO::class)
  105. ->disableOriginalConstructor()
  106. ->onlyMethods(['quote'])
  107. ->getMock();
  108. $connection
  109. ->expects($this->once())
  110. ->method('quote')
  111. ->with($value, PDO::PARAM_STR)
  112. ->will($this->returnValue('string'));
  113. $this->driver->setConnection($connection);
  114. $this->driver->schemaValue($value);
  115. }
  116. /**
  117. * Test lastInsertId().
  118. */
  119. public function testLastInsertId(): void
  120. {
  121. $connection = $this->getMockBuilder(PDO::class)
  122. ->disableOriginalConstructor()
  123. ->onlyMethods(['lastInsertId'])
  124. ->getMock();
  125. $connection
  126. ->expects($this->once())
  127. ->method('lastInsertId')
  128. ->willReturn('all-the-bears');
  129. $this->driver->setConnection($connection);
  130. $this->assertSame('all-the-bears', $this->driver->lastInsertId());
  131. }
  132. /**
  133. * Test isConnected().
  134. */
  135. public function testIsConnected(): void
  136. {
  137. $this->assertFalse($this->driver->isConnected());
  138. $connection = $this->getMockBuilder(PDO::class)
  139. ->disableOriginalConstructor()
  140. ->onlyMethods(['query'])
  141. ->getMock();
  142. $connection
  143. ->expects($this->once())
  144. ->method('query')
  145. ->willReturn(new PDOStatement());
  146. $this->driver->setConnection($connection);
  147. $this->assertTrue($this->driver->isConnected());
  148. }
  149. /**
  150. * test autoQuoting().
  151. */
  152. public function testAutoQuoting(): void
  153. {
  154. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  155. $this->assertSame($this->driver, $this->driver->enableAutoQuoting(true));
  156. $this->assertTrue($this->driver->isAutoQuotingEnabled());
  157. $this->driver->disableAutoQuoting();
  158. $this->assertFalse($this->driver->isAutoQuotingEnabled());
  159. }
  160. /**
  161. * Test compileQuery().
  162. */
  163. public function testCompileQuery(): void
  164. {
  165. $compiler = $this->getMockBuilder(QueryCompiler::class)
  166. ->onlyMethods(['compile'])
  167. ->getMock();
  168. $compiler
  169. ->expects($this->once())
  170. ->method('compile')
  171. ->willReturn('1');
  172. $driver = $this->getMockBuilder(Driver::class)
  173. ->onlyMethods(['newCompiler', 'queryTranslator'])
  174. ->getMockForAbstractClass();
  175. $driver
  176. ->expects($this->once())
  177. ->method('newCompiler')
  178. ->willReturn($compiler);
  179. $driver
  180. ->expects($this->once())
  181. ->method('queryTranslator')
  182. ->willReturn(function ($query) {
  183. return $query;
  184. });
  185. $query = $this->getMockBuilder(Query::class)
  186. ->disableOriginalConstructor()
  187. ->getMock();
  188. $query->method('type')->will($this->returnValue('select'));
  189. $result = $driver->compileQuery($query, new ValueBinder());
  190. $this->assertIsArray($result);
  191. $this->assertSame($query, $result[0]);
  192. $this->assertSame('1', $result[1]);
  193. }
  194. /**
  195. * Test newCompiler().
  196. */
  197. public function testNewCompiler(): void
  198. {
  199. $this->assertInstanceOf(QueryCompiler::class, $this->driver->newCompiler());
  200. }
  201. /**
  202. * Test newTableSchema().
  203. */
  204. public function testNewTableSchema(): void
  205. {
  206. $tableName = 'articles';
  207. $actual = $this->driver->newTableSchema($tableName);
  208. $this->assertInstanceOf(TableSchema::class, $actual);
  209. $this->assertSame($tableName, $actual->name());
  210. }
  211. /**
  212. * Test __destruct().
  213. */
  214. public function testDestructor(): void
  215. {
  216. $this->driver->disconnect();
  217. $this->expectException(MissingConnectionException::class);
  218. $this->driver->getConnection();
  219. }
  220. /**
  221. * Data provider for testSchemaValue().
  222. *
  223. * @return array
  224. */
  225. public function schemaValueProvider(): array
  226. {
  227. return [
  228. [null, 'NULL'],
  229. [false, 'FALSE'],
  230. [true, 'TRUE'],
  231. [1, '1'],
  232. ['0', '0'],
  233. ['42', '42'],
  234. ];
  235. }
  236. }