3 class DeferredUpdatesTest
extends MediaWikiTestCase
{
6 * @covers DeferredUpdates::addUpdate
7 * @covers DeferredUpdates::push
8 * @covers DeferredUpdates::doUpdates
9 * @covers DeferredUpdates::execute
10 * @covers DeferredUpdates::runUpdate
12 public function testAddAndRun() {
13 $update = $this->getMockBuilder( DeferrableUpdate
::class )
14 ->setMethods( [ 'doUpdate' ] )->getMock();
15 $update->expects( $this->once() )->method( 'doUpdate' );
17 DeferredUpdates
::addUpdate( $update );
18 DeferredUpdates
::doUpdates();
22 * @covers DeferredUpdates::addUpdate
23 * @covers DeferredUpdates::push
25 public function testAddMergeable() {
26 $this->setMwGlobals( 'wgCommandLineMode', false );
28 $update1 = $this->getMockBuilder( MergeableUpdate
::class )
29 ->setMethods( [ 'merge', 'doUpdate' ] )->getMock();
30 $update1->expects( $this->once() )->method( 'merge' );
31 $update1->expects( $this->never() )->method( 'doUpdate' );
33 $update2 = $this->getMockBuilder( MergeableUpdate
::class )
34 ->setMethods( [ 'merge', 'doUpdate' ] )->getMock();
35 $update2->expects( $this->never() )->method( 'merge' );
36 $update2->expects( $this->never() )->method( 'doUpdate' );
38 DeferredUpdates
::addUpdate( $update1 );
39 DeferredUpdates
::addUpdate( $update2 );
43 * @covers DeferredUpdates::addCallableUpdate
44 * @covers MWCallableUpdate::getOrigin
46 public function testAddCallableUpdate() {
47 $this->setMwGlobals( 'wgCommandLineMode', true );
50 DeferredUpdates
::addCallableUpdate( function () use ( &$ran ) {
53 DeferredUpdates
::doUpdates();
55 $this->assertSame( 1, $ran, 'Update ran' );
59 * @covers DeferredUpdates::getPendingUpdates
60 * @covers DeferredUpdates::clearPendingUpdates
62 public function testGetPendingUpdates() {
63 // Prevent updates from running
64 $this->setMwGlobals( 'wgCommandLineMode', false );
66 $pre = DeferredUpdates
::PRESEND
;
67 $post = DeferredUpdates
::POSTSEND
;
68 $all = DeferredUpdates
::ALL
;
70 $update = $this->getMock( DeferrableUpdate
::class );
71 $update->expects( $this->never() )
72 ->method( 'doUpdate' );
74 DeferredUpdates
::addUpdate( $update, $pre );
75 $this->assertCount( 1, DeferredUpdates
::getPendingUpdates( $pre ) );
76 $this->assertCount( 0, DeferredUpdates
::getPendingUpdates( $post ) );
77 $this->assertCount( 1, DeferredUpdates
::getPendingUpdates( $all ) );
78 $this->assertCount( 1, DeferredUpdates
::getPendingUpdates() );
79 DeferredUpdates
::clearPendingUpdates();
80 $this->assertCount( 0, DeferredUpdates
::getPendingUpdates() );
82 DeferredUpdates
::addUpdate( $update, $post );
83 $this->assertCount( 0, DeferredUpdates
::getPendingUpdates( $pre ) );
84 $this->assertCount( 1, DeferredUpdates
::getPendingUpdates( $post ) );
85 $this->assertCount( 1, DeferredUpdates
::getPendingUpdates( $all ) );
86 $this->assertCount( 1, DeferredUpdates
::getPendingUpdates() );
87 DeferredUpdates
::clearPendingUpdates();
88 $this->assertCount( 0, DeferredUpdates
::getPendingUpdates() );
92 * @covers DeferredUpdates::doUpdates
93 * @covers DeferredUpdates::execute
94 * @covers DeferredUpdates::addUpdate
96 public function testDoUpdatesWeb() {
97 $this->setMwGlobals( 'wgCommandLineMode', false );
100 '1' => "deferred update 1;\n",
101 '2' => "deferred update 2;\n",
102 '2-1' => "deferred update 1 within deferred update 2;\n",
103 '2-2' => "deferred update 2 within deferred update 2;\n",
104 '3' => "deferred update 3;\n",
105 '3-1' => "deferred update 1 within deferred update 3;\n",
106 '3-2' => "deferred update 2 within deferred update 3;\n",
107 '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
108 '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
110 DeferredUpdates
::addCallableUpdate(
111 function () use ( $updates ) {
115 DeferredUpdates
::addCallableUpdate(
116 function () use ( $updates ) {
118 DeferredUpdates
::addCallableUpdate(
119 function () use ( $updates ) {
120 echo $updates['2-1'];
123 DeferredUpdates
::addCallableUpdate(
124 function () use ( $updates ) {
125 echo $updates['2-2'];
130 DeferredUpdates
::addCallableUpdate(
131 function () use ( $updates ) {
133 DeferredUpdates
::addCallableUpdate(
134 function () use ( $updates ) {
135 echo $updates['3-1'];
136 DeferredUpdates
::addCallableUpdate(
137 function () use ( $updates ) {
138 echo $updates['3-1-1'];
143 DeferredUpdates
::addCallableUpdate(
144 function () use ( $updates ) {
145 echo $updates['3-2'];
146 DeferredUpdates
::addCallableUpdate(
147 function () use ( $updates ) {
148 echo $updates['3-2-1'];
156 $this->assertEquals( 3, DeferredUpdates
::pendingUpdatesCount() );
158 $this->expectOutputString( implode( '', $updates ) );
160 DeferredUpdates
::doUpdates();
164 DeferredUpdates
::addCallableUpdate(
165 function () use ( &$x ) {
168 DeferredUpdates
::PRESEND
170 DeferredUpdates
::addCallableUpdate(
171 function () use ( &$y ) {
174 DeferredUpdates
::POSTSEND
177 $this->assertNull( $x, "Update not run yet" );
178 $this->assertNull( $y, "Update not run yet" );
180 DeferredUpdates
::doUpdates( 'run', DeferredUpdates
::PRESEND
);
181 $this->assertEquals( "Sherity", $x, "PRESEND update ran" );
182 $this->assertNull( $y, "POSTSEND update not run yet" );
184 DeferredUpdates
::doUpdates( 'run', DeferredUpdates
::POSTSEND
);
185 $this->assertEquals( "Marychu", $y, "POSTSEND update ran" );
189 * @covers DeferredUpdates::doUpdates
190 * @covers DeferredUpdates::execute
191 * @covers DeferredUpdates::addUpdate
193 public function testDoUpdatesCLI() {
194 $this->setMwGlobals( 'wgCommandLineMode', true );
196 '1' => "deferred update 1;\n",
197 '2' => "deferred update 2;\n",
198 '2-1' => "deferred update 1 within deferred update 2;\n",
199 '2-2' => "deferred update 2 within deferred update 2;\n",
200 '3' => "deferred update 3;\n",
201 '3-1' => "deferred update 1 within deferred update 3;\n",
202 '3-2' => "deferred update 2 within deferred update 3;\n",
203 '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
204 '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
208 wfGetLBFactory()->commitMasterChanges( __METHOD__
);
210 DeferredUpdates
::addCallableUpdate(
211 function () use ( $updates ) {
215 DeferredUpdates
::addCallableUpdate(
216 function () use ( $updates ) {
218 DeferredUpdates
::addCallableUpdate(
219 function () use ( $updates ) {
220 echo $updates['2-1'];
223 DeferredUpdates
::addCallableUpdate(
224 function () use ( $updates ) {
225 echo $updates['2-2'];
230 DeferredUpdates
::addCallableUpdate(
231 function () use ( $updates ) {
233 DeferredUpdates
::addCallableUpdate(
234 function () use ( $updates ) {
235 echo $updates['3-1'];
236 DeferredUpdates
::addCallableUpdate(
237 function () use ( $updates ) {
238 echo $updates['3-1-1'];
243 DeferredUpdates
::addCallableUpdate(
244 function () use ( $updates ) {
245 echo $updates['3-2'];
246 DeferredUpdates
::addCallableUpdate(
247 function () use ( $updates ) {
248 echo $updates['3-2-1'];
256 $this->expectOutputString( implode( '', $updates ) );
258 DeferredUpdates
::doUpdates();
262 * @covers DeferredUpdates::doUpdates
263 * @covers DeferredUpdates::execute
264 * @covers DeferredUpdates::addUpdate
266 public function testPresendAddOnPostsendRun() {
267 $this->setMwGlobals( 'wgCommandLineMode', true );
272 wfGetLBFactory()->commitMasterChanges( __METHOD__
);
274 DeferredUpdates
::addCallableUpdate(
275 function () use ( &$x, &$y ) {
277 DeferredUpdates
::addCallableUpdate(
278 function () use ( &$y ) {
281 DeferredUpdates
::PRESEND
284 DeferredUpdates
::POSTSEND
287 DeferredUpdates
::doUpdates();
289 $this->assertTrue( $x, "Outer POSTSEND update ran" );
290 $this->assertTrue( $y, "Nested PRESEND update ran" );