Merge "rdbms: avoid LoadBalancer::getConnection waste when using $groups"
[lhc/web/wiklou.git] / tests / phpunit / includes / deferred / MWCallableUpdateTest.php
1 <?php
2
3 /**
4 * @covers MWCallableUpdate
5 */
6 class MWCallableUpdateTest extends PHPUnit\Framework\TestCase {
7
8 use MediaWikiCoversValidator;
9
10 public function testDoUpdate() {
11 $ran = 0;
12 $update = new MWCallableUpdate( function () use ( &$ran ) {
13 $ran++;
14 } );
15 $this->assertSame( 0, $ran );
16 $update->doUpdate();
17 $this->assertSame( 1, $ran );
18 }
19
20 public function testCancel() {
21 // Prepare update and DB
22 $db = new DatabaseTestHelper( __METHOD__ );
23 $db->begin( __METHOD__ );
24 $ran = 0;
25 $update = new MWCallableUpdate( function () use ( &$ran ) {
26 $ran++;
27 }, __METHOD__, $db );
28
29 // Emulate rollback
30 $db->rollback( __METHOD__ );
31
32 $update->doUpdate();
33
34 // Ensure it was cancelled
35 $this->assertSame( 0, $ran );
36 }
37
38 public function testCancelSome() {
39 // Prepare update and DB
40 $db1 = new DatabaseTestHelper( __METHOD__ );
41 $db1->begin( __METHOD__ );
42 $db2 = new DatabaseTestHelper( __METHOD__ );
43 $db2->begin( __METHOD__ );
44 $ran = 0;
45 $update = new MWCallableUpdate( function () use ( &$ran ) {
46 $ran++;
47 }, __METHOD__, [ $db1, $db2 ] );
48
49 // Emulate rollback
50 $db1->rollback( __METHOD__ );
51
52 $update->doUpdate();
53
54 // Prevents: "Notice: DB transaction writes or callbacks still pending"
55 $db2->rollback( __METHOD__ );
56
57 // Ensure it was cancelled
58 $this->assertSame( 0, $ran );
59 }
60
61 public function testCancelAll() {
62 // Prepare update and DB
63 $db1 = new DatabaseTestHelper( __METHOD__ );
64 $db1->begin( __METHOD__ );
65 $db2 = new DatabaseTestHelper( __METHOD__ );
66 $db2->begin( __METHOD__ );
67 $ran = 0;
68 $update = new MWCallableUpdate( function () use ( &$ran ) {
69 $ran++;
70 }, __METHOD__, [ $db1, $db2 ] );
71
72 // Emulate rollbacks
73 $db1->rollback( __METHOD__ );
74 $db2->rollback( __METHOD__ );
75
76 $update->doUpdate();
77
78 // Ensure it was cancelled
79 $this->assertSame( 0, $ran );
80 }
81
82 }