MediaWiki REL1_28
DeferredUpdatesTest.php
Go to the documentation of this file.
1<?php
2
4 public function testDoUpdatesWeb() {
5 $this->setMwGlobals( 'wgCommandLineMode', false );
6
7 $updates = [
8 '1' => "deferred update 1;\n",
9 '2' => "deferred update 2;\n",
10 '2-1' => "deferred update 1 within deferred update 2;\n",
11 '2-2' => "deferred update 2 within deferred update 2;\n",
12 '3' => "deferred update 3;\n",
13 '3-1' => "deferred update 1 within deferred update 3;\n",
14 '3-2' => "deferred update 2 within deferred update 3;\n",
15 '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
16 '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
17 ];
18 DeferredUpdates::addCallableUpdate(
19 function () use ( $updates ) {
20 echo $updates['1'];
21 }
22 );
23 DeferredUpdates::addCallableUpdate(
24 function () use ( $updates ) {
25 echo $updates['2'];
26 DeferredUpdates::addCallableUpdate(
27 function () use ( $updates ) {
28 echo $updates['2-1'];
29 }
30 );
31 DeferredUpdates::addCallableUpdate(
32 function () use ( $updates ) {
33 echo $updates['2-2'];
34 }
35 );
36 }
37 );
38 DeferredUpdates::addCallableUpdate(
39 function () use ( $updates ) {
40 echo $updates['3'];
41 DeferredUpdates::addCallableUpdate(
42 function () use ( $updates ) {
43 echo $updates['3-1'];
44 DeferredUpdates::addCallableUpdate(
45 function () use ( $updates ) {
46 echo $updates['3-1-1'];
47 }
48 );
49 }
50 );
51 DeferredUpdates::addCallableUpdate(
52 function () use ( $updates ) {
53 echo $updates['3-2'];
54 DeferredUpdates::addCallableUpdate(
55 function () use ( $updates ) {
56 echo $updates['3-2-1'];
57 }
58 );
59 }
60 );
61 }
62 );
63
64 $this->assertEquals( 3, DeferredUpdates::pendingUpdatesCount() );
65
66 $this->expectOutputString( implode( '', $updates ) );
67
68 DeferredUpdates::doUpdates();
69
70 $x = null;
71 $y = null;
72 DeferredUpdates::addCallableUpdate(
73 function () use ( &$x ) {
74 $x = 'Sherity';
75 },
76 DeferredUpdates::PRESEND
77 );
78 DeferredUpdates::addCallableUpdate(
79 function () use ( &$y ) {
80 $y = 'Marychu';
81 },
82 DeferredUpdates::POSTSEND
83 );
84
85 $this->assertNull( $x, "Update not run yet" );
86 $this->assertNull( $y, "Update not run yet" );
87
88 DeferredUpdates::doUpdates( 'run', DeferredUpdates::PRESEND );
89 $this->assertEquals( "Sherity", $x, "PRESEND update ran" );
90 $this->assertNull( $y, "POSTSEND update not run yet" );
91
92 DeferredUpdates::doUpdates( 'run', DeferredUpdates::POSTSEND );
93 $this->assertEquals( "Marychu", $y, "POSTSEND update ran" );
94 }
95
96 public function testDoUpdatesCLI() {
97 $this->setMwGlobals( 'wgCommandLineMode', true );
98 $updates = [
99 '1' => "deferred update 1;\n",
100 '2' => "deferred update 2;\n",
101 '2-1' => "deferred update 1 within deferred update 2;\n",
102 '2-2' => "deferred update 2 within deferred update 2;\n",
103 '3' => "deferred update 3;\n",
104 '3-1' => "deferred update 1 within deferred update 3;\n",
105 '3-2' => "deferred update 2 within deferred update 3;\n",
106 '3-1-1' => "deferred update 1 within deferred update 1 within deferred update 3;\n",
107 '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n",
108 ];
109
110 wfGetLBFactory()->commitMasterChanges( __METHOD__ ); // clear anything
111
112 DeferredUpdates::addCallableUpdate(
113 function () use ( $updates ) {
114 echo $updates['1'];
115 }
116 );
117 DeferredUpdates::addCallableUpdate(
118 function () use ( $updates ) {
119 echo $updates['2'];
120 DeferredUpdates::addCallableUpdate(
121 function () use ( $updates ) {
122 echo $updates['2-1'];
123 }
124 );
125 DeferredUpdates::addCallableUpdate(
126 function () use ( $updates ) {
127 echo $updates['2-2'];
128 }
129 );
130 }
131 );
132 DeferredUpdates::addCallableUpdate(
133 function () use ( $updates ) {
134 echo $updates['3'];
135 DeferredUpdates::addCallableUpdate(
136 function () use ( $updates ) {
137 echo $updates['3-1'];
138 DeferredUpdates::addCallableUpdate(
139 function () use ( $updates ) {
140 echo $updates['3-1-1'];
141 }
142 );
143 }
144 );
145 DeferredUpdates::addCallableUpdate(
146 function () use ( $updates ) {
147 echo $updates['3-2'];
148 DeferredUpdates::addCallableUpdate(
149 function () use ( $updates ) {
150 echo $updates['3-2-1'];
151 }
152 );
153 }
154 );
155 }
156 );
157
158 $this->expectOutputString( implode( '', $updates ) );
159
160 DeferredUpdates::doUpdates();
161 }
162}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfGetLBFactory()
Get the load balancer factory object.
setMwGlobals( $pairs, $value=null)
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37