Merge "Chinese Conversion Table Update 2017-6"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / BagOStuffTest.php
index b9fd6ab..4320d80 100644 (file)
@@ -1,4 +1,7 @@
 <?php
+
+use Wikimedia\ScopedCallback;
+
 /**
  * @author Matthias Mullie <mmullie@wikimedia.org>
  * @group BagOStuff
@@ -52,8 +55,8 @@ class BagOStuffTest extends MediaWikiTestCase {
                );
 
                $this->assertNotEquals(
-                       $cache->makeKeyInternal( 'prefix', array( 'a', 'bc:', 'de' ) ),
-                       $cache->makeKeyInternal( 'prefix', array( 'a', 'bc', ':de' ) )
+                       $cache->makeKeyInternal( 'prefix', [ 'a', 'bc:', 'de' ] ),
+                       $cache->makeKeyInternal( 'prefix', [ 'a', 'bc', ':de' ] )
                );
        }
 
@@ -138,6 +141,20 @@ class BagOStuffTest extends MediaWikiTestCase {
                }
        }
 
+       /**
+        * @covers BagOStuff::changeTTL
+        */
+       public function testChangeTTL() {
+               $key = wfMemcKey( 'test' );
+               $value = 'meow';
+
+               $this->cache->add( $key, $value );
+               $this->assertTrue( $this->cache->changeTTL( $key, 5 ) );
+               $this->assertEquals( $this->cache->get( $key ), $value );
+               $this->cache->delete( $key );
+               $this->assertFalse( $this->cache->changeTTL( $key, 5 ) );
+       }
+
        /**
         * @covers BagOStuff::add
         */
@@ -146,8 +163,11 @@ class BagOStuffTest extends MediaWikiTestCase {
                $this->assertTrue( $this->cache->add( $key, 'test' ) );
        }
 
+       /**
+        * @covers BagOStuff::get
+        */
        public function testGet() {
-               $value = array( 'this' => 'is', 'a' => 'test' );
+               $value = [ 'this' => 'is', 'a' => 'test' ];
 
                $key = wfMemcKey( 'test' );
                $this->cache->add( $key, $value );
@@ -199,10 +219,10 @@ class BagOStuffTest extends MediaWikiTestCase {
         * @covers BagOStuff::getMulti
         */
        public function testGetMulti() {
-               $value1 = array( 'this' => 'is', 'a' => 'test' );
-               $value2 = array( 'this' => 'is', 'another' => 'test' );
-               $value3 = array( 'testing a key that may be encoded when sent to cache backend' );
-               $value4 = array( 'another test where chars in key will be encoded' );
+               $value1 = [ 'this' => 'is', 'a' => 'test' ];
+               $value2 = [ 'this' => 'is', 'another' => 'test' ];
+               $value3 = [ 'testing a key that may be encoded when sent to cache backend' ];
+               $value4 = [ 'another test where chars in key will be encoded' ];
 
                $key1 = wfMemcKey( 'test1' );
                $key2 = wfMemcKey( 'test2' );
@@ -218,8 +238,8 @@ class BagOStuffTest extends MediaWikiTestCase {
                $this->cache->add( $key4, $value4 );
 
                $this->assertEquals(
-                       array( $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ),
-                       $this->cache->getMulti( array( $key1, $key2, $key3, $key4 ) )
+                       [ $key1 => $value1, $key2 => $value2, $key3 => $value3, $key4 => $value4 ],
+                       $this->cache->getMulti( [ $key1, $key2, $key3, $key4 ] )
                );
 
                // cleanup
@@ -237,19 +257,44 @@ class BagOStuffTest extends MediaWikiTestCase {
                $value1 = $this->cache->getScopedLock( $key, 0 );
                $value2 = $this->cache->getScopedLock( $key, 0 );
 
-               $this->assertType( 'ScopedCallback', $value1, 'First call returned lock' );
+               $this->assertType( ScopedCallback::class, $value1, 'First call returned lock' );
                $this->assertNull( $value2, 'Duplicate call returned no lock' );
 
                unset( $value1 );
 
                $value3 = $this->cache->getScopedLock( $key, 0 );
-               $this->assertType( 'ScopedCallback', $value3, 'Lock returned callback after release' );
+               $this->assertType( ScopedCallback::class, $value3, 'Lock returned callback after release' );
                unset( $value3 );
 
                $value1 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
                $value2 = $this->cache->getScopedLock( $key, 0, 5, 'reentry' );
 
-               $this->assertType( 'ScopedCallback', $value1, 'First reentrant call returned lock' );
-               $this->assertType( 'ScopedCallback', $value1, 'Second reentrant call returned lock' );
+               $this->assertType( ScopedCallback::class, $value1, 'First reentrant call returned lock' );
+               $this->assertType( ScopedCallback::class, $value1, 'Second reentrant call returned lock' );
+       }
+
+       /**
+        * @covers BagOStuff::__construct
+        * @covers BagOStuff::trackDuplicateKeys
+        */
+       public function testReportDupes() {
+               $logger = $this->createMock( Psr\Log\NullLogger::class );
+               $logger->expects( $this->once() )
+                       ->method( 'warning' )
+                       ->with( 'Duplicate get(): "{key}" fetched {count} times', [
+                               'key' => 'foo',
+                               'count' => 2,
+                       ] );
+
+               $cache = new HashBagOStuff( [
+                       'reportDupes' => true,
+                       'asyncHandler' => 'DeferredUpdates::addCallableUpdate',
+                       'logger' => $logger,
+               ] );
+               $cache->get( 'foo' );
+               $cache->get( 'bar' );
+               $cache->get( 'foo' );
+
+               DeferredUpdates::doUpdates();
        }
 }