Merge "Switch the sidebar cache to using checkKeys"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / MemoizedCallableTest.php
index 6edb3d8..d64ba3d 100644 (file)
@@ -1,10 +1,10 @@
 <?php
 /**
  * A MemoizedCallable subclass that stores function return values
- * in an instance property rather than APC.
+ * in an instance property rather than APC or APCu.
  */
 class ArrayBackedMemoizedCallable extends MemoizedCallable {
-       public $cache = array();
+       private $cache = [];
 
        protected function fetchResult( $key, &$success ) {
                if ( array_key_exists( $key, $this->cache ) ) {
@@ -24,19 +24,22 @@ class ArrayBackedMemoizedCallable extends MemoizedCallable {
  * PHP Unit tests for MemoizedCallable class.
  * @covers MemoizedCallable
  */
-class MemoizedCallableTest extends PHPUnit_Framework_TestCase {
+class MemoizedCallableTest extends PHPUnit\Framework\TestCase {
+
+       use MediaWikiCoversValidator;
 
        /**
         * The memoized callable should relate inputs to outputs in the same
         * way as the original underlying callable.
         */
        public function testReturnValuePassedThrough() {
-               $mock = $this->getMock( 'stdClass', array( 'reverse' ) );
+               $mock = $this->getMockBuilder( stdClass::class )
+                       ->setMethods( [ 'reverse' ] )->getMock();
                $mock->expects( $this->any() )
                        ->method( 'reverse' )
                        ->will( $this->returnCallback( 'strrev' ) );
 
-               $memoized = new MemoizedCallable( array( $mock, 'reverse' ) );
+               $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
                $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
        }
 
@@ -44,15 +47,16 @@ class MemoizedCallableTest extends PHPUnit_Framework_TestCase {
         * Consecutive calls to the memoized callable with the same arguments
         * should result in just one invocation of the underlying callable.
         *
-        * @requires function apc_store
+        * @requires function apc_store/apcu_store
         */
        public function testCallableMemoized() {
-               $observer = $this->getMock( 'stdClass', array( 'computeSomething' ) );
+               $observer = $this->getMockBuilder( stdClass::class )
+                       ->setMethods( [ 'computeSomething' ] )->getMock();
                $observer->expects( $this->once() )
                        ->method( 'computeSomething' )
                        ->will( $this->returnValue( 'ok' ) );
 
-               $memoized = new ArrayBackedMemoizedCallable( array( $observer, 'computeSomething' ) );
+               $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
 
                // First invocation -- delegates to $observer->computeSomething()
                $this->assertEquals( 'ok', $memoized->invoke() );
@@ -67,7 +71,7 @@ class MemoizedCallableTest extends PHPUnit_Framework_TestCase {
        public function testInvokeVariadic() {
                $memoized = new MemoizedCallable( 'sprintf' );
                $this->assertEquals(
-                       $memoized->invokeArgs( array( 'this is %s', 'correct' ) ),
+                       $memoized->invokeArgs( [ 'this is %s', 'correct' ] ),
                        $memoized->invoke( 'this is %s', 'correct' )
                );
        }
@@ -78,7 +82,7 @@ class MemoizedCallableTest extends PHPUnit_Framework_TestCase {
        public function testShortcutMethod() {
                $this->assertEquals(
                        'this is correct',
-                       MemoizedCallable::call( 'sprintf', array( 'this is %s', 'correct' ) )
+                       MemoizedCallable::call( 'sprintf', [ 'this is %s', 'correct' ] )
                );
        }
 
@@ -112,6 +116,11 @@ class MemoizedCallableTest extends PHPUnit_Framework_TestCase {
                        $this->readAttribute( $a, 'callableName' ),
                        $this->readAttribute( $b, 'callableName' )
                );
+
+               $c = new ArrayBackedMemoizedCallable( function () {
+                       return rand();
+               } );
+               $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
        }
 
        /**