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