DriverTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.2.12
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database;
  16. use Cake\Database\Driver;
  17. use Cake\Database\Query;
  18. use Cake\Database\QueryCompiler;
  19. use Cake\Database\ValueBinder;
  20. use Cake\TestSuite\TestCase;
  21. use PDO;
  22. /**
  23. * Tests Driver class
  24. */
  25. class DriverTest extends TestCase
  26. {
  27. /**
  28. * Setup.
  29. */
  30. public function setUp()
  31. {
  32. $this->driver = $this->getMockForAbstractClass(Driver::class);
  33. }
  34. /**
  35. * Test if building the object throws an exception if we're not passing
  36. * required config data.
  37. *
  38. * @expectedException \InvalidArgumentException
  39. * @expectedExceptionMessage Please pass "username" instead of "login" for connecting to the database
  40. * @return void
  41. */
  42. public function testConstructorException()
  43. {
  44. $arg = ['login' => 'Bear'];
  45. $this->getMockForAbstractClass(Driver::class, [$arg]);
  46. }
  47. /**
  48. * Test the constructor.
  49. *
  50. * @return void
  51. */
  52. public function testConstructor()
  53. {
  54. $arg = ['quoteIdentifiers' => true];
  55. $driver = $this->getMockForAbstractClass(Driver::class, [$arg]);
  56. $this->assertTrue($driver->autoQuoting());
  57. $arg = ['username' => 'GummyBear'];
  58. $driver = $this->getMockForAbstractClass(Driver::class, [$arg]);
  59. $this->assertFalse($driver->autoQuoting());
  60. }
  61. /**
  62. * Test supportsSavePoints().
  63. *
  64. * @return void
  65. */
  66. public function testSupportsSavePoints()
  67. {
  68. $result = $this->driver->supportsSavePoints();
  69. $this->assertTrue($result);
  70. }
  71. /**
  72. * Test supportsQuoting().
  73. *
  74. * @return void
  75. */
  76. public function testSupportsQuoting()
  77. {
  78. $result = $this->driver->supportsQuoting();
  79. $this->assertTrue($result);
  80. }
  81. /**
  82. * Test schemaValue().
  83. * Uses a provider for all the different values we can pass to the method.
  84. *
  85. * @dataProvider schemaValueProvider
  86. * @return void
  87. */
  88. public function testSchemaValue($input, $expected)
  89. {
  90. $this->driver->_connection = $this->getMockBuilder(PDO::class)
  91. ->disableOriginalConstructor()
  92. ->setMethods(['quote'])
  93. ->getMock();
  94. $this->driver->_connection
  95. ->expects($this->any())
  96. ->method('quote')
  97. ->willReturn($expected);
  98. $result = $this->driver->schemaValue($input);
  99. $this->assertSame($expected, $result);
  100. }
  101. /**
  102. * Test lastInsertId().
  103. *
  104. * @return void
  105. */
  106. public function testLastInsertId()
  107. {
  108. $connection = $this->getMockBuilder(PDO::class)
  109. ->disableOriginalConstructor()
  110. ->setMethods(['lastInsertId'])
  111. ->getMock();
  112. $connection
  113. ->expects($this->once())
  114. ->method('lastInsertId')
  115. ->willReturn('all-the-bears');
  116. $this->driver->_connection = $connection;
  117. $this->assertSame('all-the-bears', $this->driver->lastInsertId());
  118. }
  119. /**
  120. * Test isConnected().
  121. *
  122. * @return void
  123. */
  124. public function testIsConnected()
  125. {
  126. $this->driver->_connection = 'connection';
  127. $this->assertTrue($this->driver->isConnected());
  128. $this->driver->_connection = null;
  129. $this->assertFalse($this->driver->isConnected());
  130. }
  131. /**
  132. * test autoQuoting().
  133. *
  134. * @return void
  135. */
  136. public function testAutoQuoting()
  137. {
  138. $this->assertFalse($this->driver->autoQuoting());
  139. $this->driver->autoQuoting(true);
  140. $this->assertTrue($this->driver->autoQuoting());
  141. $this->assertTrue($this->driver->autoQuoting(true));
  142. $this->assertFalse($this->driver->autoQuoting(false));
  143. $this->assertTrue($this->driver->autoQuoting('string'));
  144. $this->assertFalse($this->driver->autoQuoting('0'));
  145. $this->assertTrue($this->driver->autoQuoting(1));
  146. $this->assertFalse($this->driver->autoQuoting(0));
  147. }
  148. /**
  149. * Test compileQuery().
  150. *
  151. * @return void
  152. * @group new
  153. */
  154. public function testCompileQuery()
  155. {
  156. $compiler = $this->getMockBuilder(QueryCompiler::class)
  157. ->setMethods(['compile'])
  158. ->getMock();
  159. $compiler
  160. ->expects($this->once())
  161. ->method('compile')
  162. ->willReturn(true);
  163. $driver = $this->getMockBuilder(Driver::class)
  164. ->setMethods(['newCompiler', 'queryTranslator'])
  165. ->getMockForAbstractClass();
  166. $driver
  167. ->expects($this->once())
  168. ->method('newCompiler')
  169. ->willReturn($compiler);
  170. $driver
  171. ->expects($this->once())
  172. ->method('queryTranslator')
  173. ->willReturn(function ($query) {
  174. return $query;
  175. });
  176. $query = $this->getMockBuilder(Query::class)
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. $result = $driver->compileQuery($query, new ValueBinder);
  180. $this->assertInternalType('array', $result);
  181. $this->assertSame($query, $result[0]);
  182. $this->assertTrue($result[1]);
  183. }
  184. /**
  185. * Test newCompiler().
  186. *
  187. * @return void
  188. */
  189. public function testNewCompiler()
  190. {
  191. $this->assertInstanceOf(QueryCompiler::class, $this->driver->newCompiler());
  192. }
  193. /**
  194. * Test __destruct().
  195. *
  196. * @return void
  197. */
  198. public function testDestructor()
  199. {
  200. $this->driver->_connection = true;
  201. $this->driver->__destruct();
  202. $this->assertNull($this->driver->_connection);
  203. }
  204. /**
  205. * Data provider for testSchemaValue().
  206. *
  207. * @return array
  208. */
  209. public function schemaValueProvider()
  210. {
  211. return [
  212. [null, 'NULL'],
  213. [false, 'FALSE'],
  214. [true, 'TRUE'],
  215. [1, 1],
  216. ['0', '0'],
  217. ['42', '42'],
  218. ['string', true]
  219. ];
  220. }
  221. }