rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / deferred / DeferredUpdatesTest.php
blob3b423563cace84fa3e5b8ab2ad13f5f190b700bb
1 <?php
3 class DeferredUpdatesTest extends MediaWikiTestCase {
5 /**
6 * @covers DeferredUpdates::getPendingUpdates
7 */
8 public function testGetPendingUpdates() {
9 # Prevent updates from running
10 $this->setMwGlobals( 'wgCommandLineMode', false );
12 $pre = DeferredUpdates::PRESEND;
13 $post = DeferredUpdates::POSTSEND;
14 $all = DeferredUpdates::ALL;
16 $update = $this->getMock( DeferrableUpdate::class );
17 $update->expects( $this->never() )
18 ->method( 'doUpdate' );
20 DeferredUpdates::addUpdate( $update, $pre );
21 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $pre ) );
22 $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $post ) );
23 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) );
24 $this->assertCount( 1, DeferredUpdates::getPendingUpdates() );
25 DeferredUpdates::clearPendingUpdates();
26 $this->assertCount( 0, DeferredUpdates::getPendingUpdates() );
28 DeferredUpdates::addUpdate( $update, $post );
29 $this->assertCount( 0, DeferredUpdates::getPendingUpdates( $pre ) );
30 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $post ) );
31 $this->assertCount( 1, DeferredUpdates::getPendingUpdates( $all ) );
32 $this->assertCount( 1, DeferredUpdates::getPendingUpdates() );
33 DeferredUpdates::clearPendingUpdates();
34 $this->assertCount( 0, DeferredUpdates::getPendingUpdates() );
37 public function testDoUpdatesWeb() {
38 $this->setMwGlobals( 'wgCommandLineMode', false );
40 $updates = [
41 '1' => "deferred update 1;\n",
42 '2' => "deferred update 2;\n",
43 '2-1' => "deferred update 1 within deferred update 2;\n",
44 '2-2' => "deferred update 2 within deferred update 2;\n",
45 '3' => "deferred update 3;\n",
46 '3-1' => "deferred update 1 within deferred update 3;\n",
47 '3-2' => "deferred update 2 within deferred update 3;\n",
48 '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
49 '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
51 DeferredUpdates::addCallableUpdate(
52 function () use ( $updates ) {
53 echo $updates['1'];
56 DeferredUpdates::addCallableUpdate(
57 function () use ( $updates ) {
58 echo $updates['2'];
59 DeferredUpdates::addCallableUpdate(
60 function () use ( $updates ) {
61 echo $updates['2-1'];
64 DeferredUpdates::addCallableUpdate(
65 function () use ( $updates ) {
66 echo $updates['2-2'];
71 DeferredUpdates::addCallableUpdate(
72 function () use ( $updates ) {
73 echo $updates['3'];
74 DeferredUpdates::addCallableUpdate(
75 function () use ( $updates ) {
76 echo $updates['3-1'];
77 DeferredUpdates::addCallableUpdate(
78 function () use ( $updates ) {
79 echo $updates['3-1-1'];
84 DeferredUpdates::addCallableUpdate(
85 function () use ( $updates ) {
86 echo $updates['3-2'];
87 DeferredUpdates::addCallableUpdate(
88 function () use ( $updates ) {
89 echo $updates['3-2-1'];
97 $this->assertEquals( 3, DeferredUpdates::pendingUpdatesCount() );
99 $this->expectOutputString( implode( '', $updates ) );
101 DeferredUpdates::doUpdates();
103 $x = null;
104 $y = null;
105 DeferredUpdates::addCallableUpdate(
106 function () use ( &$x ) {
107 $x = 'Sherity';
109 DeferredUpdates::PRESEND
111 DeferredUpdates::addCallableUpdate(
112 function () use ( &$y ) {
113 $y = 'Marychu';
115 DeferredUpdates::POSTSEND
118 $this->assertNull( $x, "Update not run yet" );
119 $this->assertNull( $y, "Update not run yet" );
121 DeferredUpdates::doUpdates( 'run', DeferredUpdates::PRESEND );
122 $this->assertEquals( "Sherity", $x, "PRESEND update ran" );
123 $this->assertNull( $y, "POSTSEND update not run yet" );
125 DeferredUpdates::doUpdates( 'run', DeferredUpdates::POSTSEND );
126 $this->assertEquals( "Marychu", $y, "POSTSEND update ran" );
129 public function testDoUpdatesCLI() {
130 $this->setMwGlobals( 'wgCommandLineMode', true );
131 $updates = [
132 '1' => "deferred update 1;\n",
133 '2' => "deferred update 2;\n",
134 '2-1' => "deferred update 1 within deferred update 2;\n",
135 '2-2' => "deferred update 2 within deferred update 2;\n",
136 '3' => "deferred update 3;\n",
137 '3-1' => "deferred update 1 within deferred update 3;\n",
138 '3-2' => "deferred update 2 within deferred update 3;\n",
139 '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
140 '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
143 wfGetLBFactory()->commitMasterChanges( __METHOD__ ); // clear anything
145 DeferredUpdates::addCallableUpdate(
146 function () use ( $updates ) {
147 echo $updates['1'];
150 DeferredUpdates::addCallableUpdate(
151 function () use ( $updates ) {
152 echo $updates['2'];
153 DeferredUpdates::addCallableUpdate(
154 function () use ( $updates ) {
155 echo $updates['2-1'];
158 DeferredUpdates::addCallableUpdate(
159 function () use ( $updates ) {
160 echo $updates['2-2'];
165 DeferredUpdates::addCallableUpdate(
166 function () use ( $updates ) {
167 echo $updates['3'];
168 DeferredUpdates::addCallableUpdate(
169 function () use ( $updates ) {
170 echo $updates['3-1'];
171 DeferredUpdates::addCallableUpdate(
172 function () use ( $updates ) {
173 echo $updates['3-1-1'];
178 DeferredUpdates::addCallableUpdate(
179 function () use ( $updates ) {
180 echo $updates['3-2'];
181 DeferredUpdates::addCallableUpdate(
182 function () use ( $updates ) {
183 echo $updates['3-2-1'];
191 $this->expectOutputString( implode( '', $updates ) );
193 DeferredUpdates::doUpdates();
196 public function testPresendAddOnPostsendRun() {
197 $this->setMwGlobals( 'wgCommandLineMode', true );
199 $x = false;
200 $y = false;
201 wfGetLBFactory()->commitMasterChanges( __METHOD__ ); // clear anything
203 DeferredUpdates::addCallableUpdate(
204 function () use ( &$x, &$y ) {
205 $x = true;
206 DeferredUpdates::addCallableUpdate(
207 function () use ( &$y ) {
208 $y = true;
210 DeferredUpdates::PRESEND
213 DeferredUpdates::POSTSEND
216 DeferredUpdates::doUpdates();
218 $this->assertTrue( $x, "Outer POSTSEND update ran" );
219 $this->assertTrue( $y, "Nested PRESEND update ran" );