ConnectionTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Test\TestCase\Database;
  18. use Cake\Core\Configure;
  19. use Cake\Database\Connection;
  20. use Cake\Database\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Tests Connection class
  24. */
  25. class ConnectionTest extends TestCase {
  26. public function setUp() {
  27. parent::setUp();
  28. $this->connection = ConnectionManager::getDataSource('test');
  29. }
  30. public function tearDown() {
  31. parent::tearDown();
  32. $this->connection->execute('DROP TABLE IF EXISTS things');
  33. unset($this->connection);
  34. }
  35. /**
  36. * Tests connecting to database
  37. *
  38. * @return void
  39. */
  40. public function testConnect() {
  41. $this->assertTrue($this->connection->connect());
  42. $this->assertTrue($this->connection->isConnected());
  43. }
  44. /**
  45. * Tests creating a connection using an invalid driver throws an exception
  46. *
  47. * @expectedException Cake\Database\Exception\MissingDriverException
  48. * @expectedExceptionMessage Database driver \Foo\InvalidDriver could not be found.
  49. * @return void
  50. */
  51. public function testMissingDriver() {
  52. $connection = new Connection(['datasource' => '\Foo\InvalidDriver']);
  53. }
  54. /**
  55. * Tests trying to use a disabled driver throws an exception
  56. *
  57. * @expectedException Cake\Database\Exception\MissingExtensionException
  58. * @expectedExceptionMessage Database driver DriverMock cannot be used due to a missing PHP extension or unmet dependency
  59. * @return void
  60. */
  61. public function testDisabledDriver() {
  62. $mock = $this->getMock('\Cake\Database\Connection\Driver', ['enabled'], [], 'DriverMock');
  63. $connection = new Connection(['datasource' => $mock]);
  64. }
  65. /**
  66. * Tests that connecting with invalid credentials or database name throws an exception
  67. *
  68. * @expectedException \Cake\Database\Exception\MissingConnectionException
  69. * @return void
  70. **/
  71. public function testWrongCredentials() {
  72. $config = ConnectionManager::config('test');
  73. $this->skipIf(isset($config['dsn']), 'Datasource has dsn, skipping.');
  74. $connection = new Connection(['database' => '_probably_not_there_'] + ConnectionManager::config('test'));
  75. $connection->connect();
  76. }
  77. /**
  78. * Tests disconnecting from database
  79. *
  80. * @return void
  81. **/
  82. public function testDisconnect() {
  83. $this->assertTrue($this->connection->connect());
  84. $this->assertTrue($this->connection->isConnected());
  85. $this->connection->disconnect();
  86. $this->assertFalse($this->connection->isConnected());
  87. }
  88. /**
  89. * Tests creation of prepared statements
  90. *
  91. * @return void
  92. **/
  93. public function testPrepare() {
  94. $sql = 'SELECT 1 + 1';
  95. $result = $this->connection->prepare($sql);
  96. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  97. $this->assertEquals($sql, $result->queryString);
  98. }
  99. /**
  100. * Tests executing a simple query using bound values
  101. *
  102. * @return void
  103. **/
  104. public function testExecuteWithArguments() {
  105. $sql = 'SELECT 1 + ?';
  106. $statement = $this->connection->execute($sql, [1], array('integer'));
  107. $this->assertCount(1, $statement);
  108. $result = $statement->fetch();
  109. $this->assertEquals([2], $result);
  110. $sql = 'SELECT 1 + ? + ? AS total';
  111. $statement = $this->connection->execute($sql, [2, 3], array('integer', 'integer'));
  112. $this->assertCount(1, $statement);
  113. $result = $statement->fetch('assoc');
  114. $this->assertEquals(['total' => 6], $result);
  115. $sql = 'SELECT 1 + :one + :two AS total';
  116. $statement = $this->connection->execute($sql, ['one' => 2, 'two' => 3], array('one' => 'integer', 'two' => 'integer'));
  117. $this->assertCount(1, $statement);
  118. $result = $statement->fetch('assoc');
  119. $this->assertEquals(['total' => 6], $result);
  120. }
  121. /**
  122. * Tests executing a query with params and associated types
  123. *
  124. * @return void
  125. **/
  126. public function testExecuteWithArgumentsAndTypes() {
  127. $sql = "SELECT ? = '2012-01-01'";
  128. $statement = $this->connection->execute($sql, [new \DateTime('2012-01-01')], ['date']);
  129. $result = $statement->fetch();
  130. $this->assertTrue((bool)$result[0]);
  131. $sql = "SELECT ? = '2012-01-01', ? = '2000-01-01 10:10:10', ? = 2";
  132. $params = [new \DateTime('2012-01-01 10:10:10'), '2000-01-01 10:10:10', 2.1];
  133. $statement = $this->connection->execute($sql, $params, ['date', 'string', 'integer']);
  134. $result = $statement->fetch();
  135. $this->assertEquals($result, array_filter($result));
  136. }
  137. /**
  138. * Tests that passing a unknown value to a query throws an exception
  139. *
  140. * @expectedException \InvalidArgumentException
  141. * @return void
  142. **/
  143. public function testExecuteWithMissingType() {
  144. $sql = 'SELECT ?';
  145. $statement = $this->connection->execute($sql, [new \DateTime('2012-01-01')], ['bar']);
  146. }
  147. /**
  148. * Tests executing a query with no params also works
  149. *
  150. * @return void
  151. **/
  152. public function testExecuteWithNoParams() {
  153. $sql = 'SELECT 1';
  154. $statement = $this->connection->execute($sql);
  155. $result = $statement->fetch();
  156. $this->assertCount(1, $result);
  157. $this->assertEquals([1], $result);
  158. }
  159. /**
  160. * Tests it is possible to insert data into a table using matching types by key name
  161. *
  162. * @return void
  163. **/
  164. public function testInsertWithMatchingTypes() {
  165. $table = 'CREATE TEMPORARY TABLE things(id int, title varchar(20), body varchar(50))';
  166. $this->connection->execute($table);
  167. $data = ['id' => '1', 'title' => 'a title', 'body' => 'a body'];
  168. $result = $this->connection->insert(
  169. 'things',
  170. $data,
  171. ['id' => 'integer', 'title' => 'string', 'body' => 'string']
  172. );
  173. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  174. $result = $this->connection->execute('SELECT * from things');
  175. $this->assertCount(1, $result);
  176. $row = $result->fetch('assoc');
  177. $this->assertEquals($data, $row);
  178. }
  179. /**
  180. * Tests it is possible to insert data into a table using matching types by array position
  181. *
  182. * @return void
  183. **/
  184. public function testInsertWithPositionalTypes() {
  185. $table = 'CREATE TEMPORARY TABLE things(id int, title varchar(20), body varchar(50))';
  186. $this->connection->execute($table);
  187. $data = ['id' => '1', 'title' => 'a title', 'body' => 'a body'];
  188. $result = $this->connection->insert(
  189. 'things',
  190. $data,
  191. ['integer', 'string', 'string']
  192. );
  193. $this->assertInstanceOf('Cake\Database\StatementInterface', $result);
  194. $result = $this->connection->execute('SELECT * from things');
  195. $this->assertCount(1, $result);
  196. $row = $result->fetch('assoc');
  197. $this->assertEquals($data, $row);
  198. }
  199. /**
  200. * Auxiliary function to insert a couple rows in a newly created table
  201. *
  202. * @return void
  203. **/
  204. protected function _insertTwoRecords() {
  205. $table = 'CREATE TEMPORARY TABLE things(id int, title varchar(20), body varchar(50))';
  206. $this->connection->execute($table);
  207. $data = ['id' => '1', 'title' => 'a title', 'body' => 'a body'];
  208. $result = $this->connection->insert(
  209. 'things',
  210. $data,
  211. ['id' => 'integer', 'title' => 'string', 'body' => 'string']
  212. );
  213. $result->bindValue(1, '2', 'integer');
  214. $result->bindValue(2, 'another title');
  215. $result->bindValue(3, 'another body');
  216. $result->execute();
  217. }
  218. /**
  219. * Tests an statement class can be reused for multiple executions
  220. *
  221. * @return void
  222. **/
  223. public function testStatementReusing() {
  224. $this->_insertTwoRecords();
  225. $total = $this->connection->execute('SELECT COUNT(*) AS total FROM things');
  226. $total = $total->fetch('assoc');
  227. $this->assertEquals(2, $total['total']);
  228. $result = $this->connection->execute('SELECT title, body FROM things');
  229. $row = $result->fetch('assoc');
  230. $this->assertEquals('a title', $row['title']);
  231. $this->assertEquals('a body', $row['body']);
  232. $row = $result->fetch('assoc');
  233. $this->assertEquals('another title', $row['title']);
  234. $this->assertEquals('another body', $row['body']);
  235. }
  236. /**
  237. * Tests rows can be updated without specifying any conditions nor types
  238. *
  239. * @return void
  240. **/
  241. public function testUpdateWithoutConditionsNorTypes() {
  242. $this->_insertTwoRecords();
  243. $title = 'changed the title!';
  244. $body = 'changed the body!';
  245. $this->connection->update('things', ['title' => $title, 'body' => $body]);
  246. $result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
  247. $this->assertCount(2, $result);
  248. }
  249. /**
  250. * Tests it is possible to use key => value conditions for update
  251. *
  252. * @return void
  253. **/
  254. public function testUpdateWithConditionsNoTypes() {
  255. $this->_insertTwoRecords();
  256. $title = 'changed the title!';
  257. $body = 'changed the body!';
  258. $this->connection->update('things', ['title' => $title, 'body' => $body], ['id' => 2]);
  259. $result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
  260. $this->assertCount(1, $result);
  261. }
  262. /**
  263. * Tests it is possible to use key => value and string conditions for update
  264. *
  265. * @return void
  266. **/
  267. public function testUpdateWithConditionsCombinedNoTypes() {
  268. $this->_insertTwoRecords();
  269. $title = 'changed the title!';
  270. $body = 'changed the body!';
  271. $this->connection->update('things', ['title' => $title, 'body' => $body], ['id' => 2, 'body is not null']);
  272. $result = $this->connection->execute('SELECT * FROM things WHERE title = ? AND body = ?', [$title, $body]);
  273. $this->assertCount(1, $result);
  274. }
  275. /**
  276. * Tests you can bind types to update values
  277. *
  278. * @return void
  279. **/
  280. public function testUpdateWithTypes() {
  281. $this->_insertTwoRecords();
  282. $title = 'changed the title!';
  283. $body = new \DateTime('2012-01-01');
  284. $values = compact('title', 'body');
  285. $this->connection->update('things', $values, [], ['body' => 'date']);
  286. $result = $this->connection->execute('SELECT * FROM things WHERE title = :title AND body = :body', $values, ['body' => 'date']);
  287. $this->assertCount(2, $result);
  288. $row = $result->fetch('assoc');
  289. $this->assertEquals('2012-01-01', $row['body']);
  290. $row = $result->fetch('assoc');
  291. $this->assertEquals('2012-01-01', $row['body']);
  292. }
  293. /**
  294. * Tests you can bind types to update values
  295. *
  296. * @return void
  297. **/
  298. public function testUpdateWithConditionsAndTypes() {
  299. $this->_insertTwoRecords();
  300. $title = 'changed the title!';
  301. $body = new \DateTime('2012-01-01');
  302. $values = compact('title', 'body');
  303. $this->connection->update('things', $values, ['id' => '1-string-parsed-as-int'], ['body' => 'date', 'id' => 'integer']);
  304. $result = $this->connection->execute('SELECT * FROM things WHERE title = :title AND body = :body', $values, ['body' => 'date']);
  305. $this->assertCount(1, $result);
  306. $row = $result->fetch('assoc');
  307. $this->assertEquals('2012-01-01', $row['body']);
  308. }
  309. /**
  310. * Tests delete from table with no conditions
  311. *
  312. * @return void
  313. **/
  314. public function testDeleteNoConditions() {
  315. $this->_insertTwoRecords();
  316. $this->connection->delete('things');
  317. $result = $this->connection->execute('SELECT * FROM things');
  318. $this->assertCount(0, $result);
  319. }
  320. /**
  321. * Tests delete from table with conditions
  322. * @return void
  323. **/
  324. public function testDeleteWithConditions() {
  325. $this->_insertTwoRecords();
  326. $this->connection->delete('things', ['id' => '1-rest-is-ommited'], ['id' => 'integer']);
  327. $result = $this->connection->execute('SELECT * FROM things');
  328. $this->assertCount(1, $result);
  329. $this->connection->delete('things', ['id' => '1-rest-is-ommited'], ['id' => 'integer']);
  330. $result = $this->connection->execute('SELECT * FROM things');
  331. $this->assertCount(1, $result);
  332. $this->connection->delete('things', ['id' => '2-rest-is-ommited'], ['id' => 'integer']);
  333. $result = $this->connection->execute('SELECT * FROM things');
  334. $this->assertCount(0, $result);
  335. }
  336. /**
  337. * Tests that it is possible to use simple database transactions
  338. *
  339. * @return void
  340. **/
  341. public function testSimpleTransactions() {
  342. $this->_insertTwoRecords();
  343. $this->connection->begin();
  344. $this->connection->delete('things', ['id' => 1]);
  345. $this->connection->rollback();
  346. $result = $this->connection->execute('SELECT * FROM things');
  347. $this->assertCount(2, $result);
  348. $this->connection->begin();
  349. $this->connection->delete('things', ['id' => 1]);
  350. $this->connection->commit();
  351. $result = $this->connection->execute('SELECT * FROM things');
  352. $this->assertCount(1, $result);
  353. }
  354. /**
  355. * Tests that it is possible to use virtualized nested transaction
  356. * with early rollback algorithm
  357. *
  358. * @return void
  359. **/
  360. public function testVirtualNestedTrasanction() {
  361. $this->_insertTwoRecords();
  362. //starting 3 virtual transaction
  363. $this->connection->begin();
  364. $this->connection->begin();
  365. $this->connection->begin();
  366. $this->connection->delete('things', ['id' => 1]);
  367. $result = $this->connection->execute('SELECT * FROM things');
  368. $this->assertCount(1, $result);
  369. $this->connection->commit();
  370. $this->connection->rollback();
  371. $result = $this->connection->execute('SELECT * FROM things');
  372. $this->assertCount(2, $result);
  373. }
  374. /**
  375. * Tests that it is possible to use virtualized nested transaction
  376. * with early rollback algorithm
  377. *
  378. * @return void
  379. **/
  380. public function testVirtualNestedTrasanction2() {
  381. $this->_insertTwoRecords();
  382. //starting 3 virtual transaction
  383. $this->connection->begin();
  384. $this->connection->begin();
  385. $this->connection->begin();
  386. $this->connection->delete('things', ['id' => 1]);
  387. $result = $this->connection->execute('SELECT * FROM things');
  388. $this->assertCount(1, $result);
  389. $this->connection->rollback();
  390. $result = $this->connection->execute('SELECT * FROM things');
  391. $this->assertCount(2, $result);
  392. }
  393. /**
  394. * Tests that it is possible to use virtualized nested transaction
  395. * with early rollback algorithm
  396. *
  397. * @return void
  398. **/
  399. public function testVirtualNestedTrasanction3() {
  400. $this->_insertTwoRecords();
  401. //starting 3 virtual transaction
  402. $this->connection->begin();
  403. $this->connection->begin();
  404. $this->connection->begin();
  405. $this->connection->delete('things', ['id' => 1]);
  406. $result = $this->connection->execute('SELECT * FROM things');
  407. $this->assertCount(1, $result);
  408. $this->connection->commit();
  409. $this->connection->commit();
  410. $this->connection->commit();
  411. $result = $this->connection->execute('SELECT * FROM things');
  412. $this->assertCount(1, $result);
  413. }
  414. /**
  415. * Tests that it is possible to real use nested transactions
  416. *
  417. * @return void
  418. **/
  419. public function testSavePoints() {
  420. $this->skipIf(!$this->connection->useSavePoints(true));
  421. $this->_insertTwoRecords();
  422. $this->connection->begin();
  423. $this->connection->delete('things', ['id' => 1]);
  424. $result = $this->connection->execute('SELECT * FROM things');
  425. $this->assertCount(1, $result);
  426. $this->connection->begin();
  427. $this->connection->delete('things', ['id' => 2]);
  428. $result = $this->connection->execute('SELECT * FROM things');
  429. $this->assertCount(0, $result);
  430. $this->connection->rollback();
  431. $result = $this->connection->execute('SELECT * FROM things');
  432. $this->assertCount(1, $result);
  433. $this->connection->rollback();
  434. $result = $this->connection->execute('SELECT * FROM things');
  435. $this->assertCount(2, $result);
  436. }
  437. /**
  438. * Tests that it is possible to real use nested transactions
  439. *
  440. * @return void
  441. **/
  442. public function testSavePoints2() {
  443. $this->skipIf(!$this->connection->useSavePoints(true));
  444. $this->_insertTwoRecords();
  445. $this->connection->begin();
  446. $this->connection->delete('things', ['id' => 1]);
  447. $result = $this->connection->execute('SELECT * FROM things');
  448. $this->assertCount(1, $result);
  449. $this->connection->begin();
  450. $this->connection->delete('things', ['id' => 2]);
  451. $result = $this->connection->execute('SELECT * FROM things');
  452. $this->assertCount(0, $result);
  453. $this->connection->rollback();
  454. $result = $this->connection->execute('SELECT * FROM things');
  455. $this->assertCount(1, $result);
  456. $this->connection->commit();
  457. $result = $this->connection->execute('SELECT * FROM things');
  458. $this->assertCount(1, $result);
  459. }
  460. /**
  461. * Tests connection can quote values to be safely used in query strings
  462. *
  463. * @return void
  464. **/
  465. public function testQuote() {
  466. $this->skipIf(!$this->connection->supportsQuoting());
  467. $expected = "'2012-01-01'";
  468. $result = $this->connection->quote(new \DateTime('2012-01-01'), 'date');
  469. $this->assertEquals($expected, $result);
  470. $expected = "'1'";
  471. $result = $this->connection->quote(1, 'string');
  472. $this->assertEquals($expected, $result);
  473. $expected = "'hello'";
  474. $result = $this->connection->quote('hello', 'string');
  475. $this->assertEquals($expected, $result);
  476. }
  477. /**
  478. * Tests identifier quoting
  479. *
  480. * @return void
  481. */
  482. public function testQuoteIdentifier() {
  483. $driver = $this->getMock('Cake\Database\Driver\Sqlite', ['enabled']);
  484. $driver->expects($this->once())
  485. ->method('enabled')
  486. ->will($this->returnValue(true));
  487. $connection = new Connection(['datasource' => $driver]);
  488. $result = $connection->quoteIdentifier('name');
  489. $expected = '"name"';
  490. $this->assertEquals($expected, $result);
  491. $result = $connection->quoteIdentifier('Model.*');
  492. $expected = '"Model".*';
  493. $this->assertEquals($expected, $result);
  494. $result = $connection->quoteIdentifier('MTD()');
  495. $expected = 'MTD()';
  496. $this->assertEquals($expected, $result);
  497. $result = $connection->quoteIdentifier('(sm)');
  498. $expected = '(sm)';
  499. $this->assertEquals($expected, $result);
  500. $result = $connection->quoteIdentifier('name AS x');
  501. $expected = '"name" AS "x"';
  502. $this->assertEquals($expected, $result);
  503. $result = $connection->quoteIdentifier('Model.name AS x');
  504. $expected = '"Model"."name" AS "x"';
  505. $this->assertEquals($expected, $result);
  506. $result = $connection->quoteIdentifier('Function(Something.foo)');
  507. $expected = 'Function("Something"."foo")';
  508. $this->assertEquals($expected, $result);
  509. $result = $connection->quoteIdentifier('Function(SubFunction(Something.foo))');
  510. $expected = 'Function(SubFunction("Something"."foo"))';
  511. $this->assertEquals($expected, $result);
  512. $result = $connection->quoteIdentifier('Function(Something.foo) AS x');
  513. $expected = 'Function("Something"."foo") AS "x"';
  514. $this->assertEquals($expected, $result);
  515. $result = $connection->quoteIdentifier('name-with-minus');
  516. $expected = '"name-with-minus"';
  517. $this->assertEquals($expected, $result);
  518. $result = $connection->quoteIdentifier('my-name');
  519. $expected = '"my-name"';
  520. $this->assertEquals($expected, $result);
  521. $result = $connection->quoteIdentifier('Foo-Model.*');
  522. $expected = '"Foo-Model".*';
  523. $this->assertEquals($expected, $result);
  524. $result = $connection->quoteIdentifier('Team.P%');
  525. $expected = '"Team"."P%"';
  526. $this->assertEquals($expected, $result);
  527. $result = $connection->quoteIdentifier('Team.G/G');
  528. $expected = '"Team"."G/G"';
  529. $result = $connection->quoteIdentifier('Model.name as y');
  530. $expected = '"Model"."name" AS "y"';
  531. $this->assertEquals($expected, $result);
  532. }
  533. /**
  534. * Tests default return vale for logger() function
  535. *
  536. * @return void
  537. */
  538. public function testLoggerDefault() {
  539. $logger = $this->connection->logger();
  540. $this->assertInstanceOf('\Cake\Database\Log\QueryLogger', $logger);
  541. $this->assertSame($logger, $this->connection->logger());
  542. }
  543. /**
  544. * Tests that a custom logger object can be set
  545. *
  546. * @return void
  547. */
  548. public function testSetLogger() {
  549. $logger = new \Cake\Database\Log\QueryLogger;
  550. $this->connection->logger($logger);
  551. $this->assertSame($logger, $this->connection->logger());
  552. }
  553. /**
  554. * Tests that statements are decorated with a logger when logQueries is set to true
  555. *
  556. * @return void
  557. */
  558. public function testLoggerDecorator() {
  559. $logger = new \Cake\Database\Log\QueryLogger;
  560. $this->connection->logQueries(true);
  561. $this->connection->logger($logger);
  562. $st = $this->connection->prepare('SELECT 1');
  563. $this->assertInstanceOf('\Cake\Database\Log\LoggingStatement', $st);
  564. $this->assertSame($logger, $st->logger());
  565. $this->connection->logQueries(false);
  566. $st = $this->connection->prepare('SELECT 1');
  567. $this->assertNotInstanceOf('\Cake\Database\Log\LoggingStatement', $st);
  568. }
  569. /**
  570. * Tests that log() function logs to the configured query logger
  571. *
  572. * @return void
  573. */
  574. public function testLogFunction() {
  575. $logger = $this->getMock('\Cake\Database\Log\QueryLogger');
  576. $this->connection->logger($logger);
  577. $logger->expects($this->once())->method('log')
  578. ->with($this->logicalAnd(
  579. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  580. $this->attributeEqualTo('query', 'SELECT 1')
  581. ));
  582. $this->connection->log('SELECT 1');
  583. }
  584. /**
  585. * Tests that begin and rollback are also logged
  586. *
  587. * @return void
  588. */
  589. public function testLogBeginRollbackTransaction() {
  590. $connection = $this->getMock(
  591. '\Cake\Database\Connection',
  592. ['connect'],
  593. [['log' => true]]
  594. );
  595. $driver = $this->getMock('Cake\Database\Driver');
  596. $driver->expects($this->once())
  597. ->method('enabled')
  598. ->will($this->returnValue(true));
  599. $connection->driver($driver);
  600. $logger = $this->getMock('\Cake\Database\Log\QueryLogger');
  601. $connection->logger($logger);
  602. $logger->expects($this->at(0))->method('log')
  603. ->with($this->logicalAnd(
  604. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  605. $this->attributeEqualTo('query', 'BEGIN')
  606. ));
  607. $logger->expects($this->at(1))->method('log')
  608. ->with($this->logicalAnd(
  609. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  610. $this->attributeEqualTo('query', 'ROLLBACK')
  611. ));
  612. $connection->begin();
  613. $connection->begin(); //This one will not be logged
  614. $connection->rollback();
  615. }
  616. /**
  617. * Tests that commits are logged
  618. *
  619. * @return void
  620. */
  621. public function testLogCommitTransaction() {
  622. $driver = $this->getMock('Cake\Database\Driver');
  623. $driver->expects($this->once())
  624. ->method('enabled')
  625. ->will($this->returnValue(true));
  626. $connection = $this->getMock(
  627. '\Cake\Database\Connection',
  628. ['connect'],
  629. [['datasource' => $driver]]
  630. );
  631. $logger = $this->getMock('\Cake\Database\Log\QueryLogger');
  632. $connection->logger($logger);
  633. $logger->expects($this->at(1))->method('log')
  634. ->with($this->logicalAnd(
  635. $this->isInstanceOf('\Cake\Database\Log\LoggedQuery'),
  636. $this->attributeEqualTo('query', 'COMMIT')
  637. ));
  638. $connection->logQueries(true);
  639. $connection->begin();
  640. $connection->commit();
  641. }
  642. }