X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fdeferred%2FDeferredUpdatesTest.php;h=6b4170733378a4b6cf134b00245cea55ee712256;hb=7189837b07f23ed5c4e5c081e5e6f6671e9171f2;hp=3b423563cace84fa3e5b8ab2ad13f5f190b700bb;hpb=fb44a171b20b1ca832b93c8d788070fc6b13d932;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/deferred/DeferredUpdatesTest.php b/tests/phpunit/includes/deferred/DeferredUpdatesTest.php index 3b423563ca..6b41707333 100644 --- a/tests/phpunit/includes/deferred/DeferredUpdatesTest.php +++ b/tests/phpunit/includes/deferred/DeferredUpdatesTest.php @@ -1,12 +1,68 @@ getMockBuilder( DeferrableUpdate::class ) + ->setMethods( [ 'doUpdate' ] )->getMock(); + $update->expects( $this->once() )->method( 'doUpdate' ); + + DeferredUpdates::addUpdate( $update ); + DeferredUpdates::doUpdates(); + } + + /** + * @covers DeferredUpdates::addUpdate + * @covers DeferredUpdates::push + */ + public function testAddMergeable() { + $this->setMwGlobals( 'wgCommandLineMode', false ); + + $update1 = $this->getMockBuilder( MergeableUpdate::class ) + ->setMethods( [ 'merge', 'doUpdate' ] )->getMock(); + $update1->expects( $this->once() )->method( 'merge' ); + $update1->expects( $this->never() )->method( 'doUpdate' ); + + $update2 = $this->getMockBuilder( MergeableUpdate::class ) + ->setMethods( [ 'merge', 'doUpdate' ] )->getMock(); + $update2->expects( $this->never() )->method( 'merge' ); + $update2->expects( $this->never() )->method( 'doUpdate' ); + + DeferredUpdates::addUpdate( $update1 ); + DeferredUpdates::addUpdate( $update2 ); + } + + /** + * @covers DeferredUpdates::addCallableUpdate + * @covers MWCallableUpdate::getOrigin + */ + public function testAddCallableUpdate() { + $this->setMwGlobals( 'wgCommandLineMode', true ); + + $ran = 0; + DeferredUpdates::addCallableUpdate( function () use ( &$ran ) { + $ran++; + } ); + DeferredUpdates::doUpdates(); + + $this->assertSame( 1, $ran, 'Update ran' ); + } + /** * @covers DeferredUpdates::getPendingUpdates + * @covers DeferredUpdates::clearPendingUpdates */ public function testGetPendingUpdates() { - # Prevent updates from running + // Prevent updates from running $this->setMwGlobals( 'wgCommandLineMode', false ); $pre = DeferredUpdates::PRESEND; @@ -34,6 +90,11 @@ class DeferredUpdatesTest extends MediaWikiTestCase { $this->assertCount( 0, DeferredUpdates::getPendingUpdates() ); } + /** + * @covers DeferredUpdates::doUpdates + * @covers DeferredUpdates::execute + * @covers DeferredUpdates::addUpdate + */ public function testDoUpdatesWeb() { $this->setMwGlobals( 'wgCommandLineMode', false ); @@ -126,6 +187,11 @@ class DeferredUpdatesTest extends MediaWikiTestCase { $this->assertEquals( "Marychu", $y, "POSTSEND update ran" ); } + /** + * @covers DeferredUpdates::doUpdates + * @covers DeferredUpdates::execute + * @covers DeferredUpdates::addUpdate + */ public function testDoUpdatesCLI() { $this->setMwGlobals( 'wgCommandLineMode', true ); $updates = [ @@ -140,7 +206,9 @@ class DeferredUpdatesTest extends MediaWikiTestCase { '3-2-1' => "deferred update 1 within deferred update 2 with deferred update 3;\n", ]; - wfGetLBFactory()->commitMasterChanges( __METHOD__ ); // clear anything + // clear anything + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $lbFactory->commitMasterChanges( __METHOD__ ); DeferredUpdates::addCallableUpdate( function () use ( $updates ) { @@ -193,12 +261,19 @@ class DeferredUpdatesTest extends MediaWikiTestCase { DeferredUpdates::doUpdates(); } + /** + * @covers DeferredUpdates::doUpdates + * @covers DeferredUpdates::execute + * @covers DeferredUpdates::addUpdate + */ public function testPresendAddOnPostsendRun() { $this->setMwGlobals( 'wgCommandLineMode', true ); $x = false; $y = false; - wfGetLBFactory()->commitMasterChanges( __METHOD__ ); // clear anything + // clear anything + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $lbFactory->commitMasterChanges( __METHOD__ ); DeferredUpdates::addCallableUpdate( function () use ( &$x, &$y ) { @@ -218,4 +293,46 @@ class DeferredUpdatesTest extends MediaWikiTestCase { $this->assertTrue( $x, "Outer POSTSEND update ran" ); $this->assertTrue( $y, "Nested PRESEND update ran" ); } + + /** + * @covers DeferredUpdates::runUpdate + */ + public function testRunUpdateTransactionScope() { + $this->setMwGlobals( 'wgCommandLineMode', false ); + + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $this->assertFalse( $lbFactory->hasTransactionRound(), 'Initial state' ); + + $ran = 0; + DeferredUpdates::addCallableUpdate( function () use ( &$ran, $lbFactory ) { + $ran++; + $this->assertTrue( $lbFactory->hasTransactionRound(), 'Has transaction' ); + } ); + DeferredUpdates::doUpdates(); + + $this->assertSame( 1, $ran, 'Update ran' ); + $this->assertFalse( $lbFactory->hasTransactionRound(), 'Final state' ); + } + + /** + * @covers DeferredUpdates::runUpdate + * @covers TransactionRoundDefiningUpdate::getOrigin + */ + public function testRunOuterScopeUpdate() { + $this->setMwGlobals( 'wgCommandLineMode', false ); + + $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); + $this->assertFalse( $lbFactory->hasTransactionRound(), 'Initial state' ); + + $ran = 0; + DeferredUpdates::addUpdate( new TransactionRoundDefiningUpdate( + function () use ( &$ran, $lbFactory ) { + $ran++; + $this->assertFalse( $lbFactory->hasTransactionRound(), 'No transaction' ); + } ) + ); + DeferredUpdates::doUpdates(); + + $this->assertSame( 1, $ran, 'Update ran' ); + } }