Debian Codegraph @

tests/phpunit/unit/includes/deferred/MWCallableUpdateTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Deferred\MWCallableUpdate;
4
5 /**
6 * @covers \MediaWiki\Deferred\MWCallableUpdate
7 */
8 class MWCallableUpdateTest extends MediaWikiUnitTestCase {
9 /** @var int */
10 private $callbackMethodRan = 0;
11
12 public function testDoUpdate() {
13 $ran = 0;
14 $ranBy = '';
15 $caller = 'caller_name';
16 $update = new MWCallableUpdate(
17 static function ( $fname ) use ( &$ran, &$ranBy ) {
18 $ran++;
19 $ranBy = $fname;
20 },
21 $caller
22 );
23 $this->assertSame( 0, $ran );
24 $this->assertSame( '', $ranBy );
25 $update->doUpdate();
26 $this->assertSame( 1, $ran );
27 $this->assertSame( $caller, $ranBy );
28
29 $this->callbackMethodRan = 0;
30 $update = new MWCallableUpdate(
31 [ $this, 'callbackMethodWithNoArgs' ],
32 $caller
33 );
34 $this->assertSame( 0, $this->callbackMethodRan );
35 $update->doUpdate();
36 $this->assertSame( 1, $this->callbackMethodRan );
37 }
38
39 public function testCancel() {
40 // Prepare update and DB
41 $db = new DatabaseTestHelper( __METHOD__ );
42 $db->begin( __METHOD__ );
43 $ran = 0;
44 $update = new MWCallableUpdate( static function () use ( &$ran ) {
45 $ran++;
46 }, __METHOD__, $db );
47
48 // Emulate rollback
49 $db->rollback( __METHOD__ );
50
51 $update->doUpdate();
52
53 // Ensure it was cancelled
54 $this->assertSame( 0, $ran );
55 }
56
57 public function testCancelSome() {
58 // Prepare update and DB
59 $db1 = new DatabaseTestHelper( __METHOD__ );
60 $db1->begin( __METHOD__ );
61 $db2 = new DatabaseTestHelper( __METHOD__ );
62 $db2->begin( __METHOD__ );
63 $ran = 0;
64 $update = new MWCallableUpdate( static function () use ( &$ran ) {
65 $ran++;
66 }, __METHOD__, [ $db1, $db2 ] );
67
68 // Emulate rollback
69 $db1->rollback( __METHOD__ );
70
71 $update->doUpdate();
72
73 // Prevents: "Notice: DB transaction writes or callbacks still pending"
74 $db2->rollback( __METHOD__ );
75
76 // Ensure it was cancelled
77 $this->assertSame( 0, $ran );
78 }
79
80 public function testCancelAll() {
81 // Prepare update and DB
82 $db1 = new DatabaseTestHelper( __METHOD__ );
83 $db1->begin( __METHOD__ );
84 $db2 = new DatabaseTestHelper( __METHOD__ );
85 $db2->begin( __METHOD__ );
86 $ran = 0;
87 $update = new MWCallableUpdate( static function () use ( &$ran ) {
88 $ran++;
89 }, __METHOD__, [ $db1, $db2 ] );
90
91 // Emulate rollbacks
92 $db1->rollback( __METHOD__ );
93 $db2->rollback( __METHOD__ );
94
95 $update->doUpdate();
96
97 // Ensure it was cancelled
98 $this->assertSame( 0, $ran );
99 }
100
101 public function callbackMethodWithNoArgs() {
102 $this->callbackMethodRan++;
103 $this->assertSame( 0, func_num_args() );
104 }
105 }