<?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 ) ) {
}
}
-
/**
* PHP Unit tests for MemoizedCallable class.
* @covers MemoizedCallable
* way as the original underlying callable.
*/
public function testReturnValuePassedThrough() {
- $mock = $this->getMock( 'stdClass', array( 'reverse' ) );
+ $mock = $this->getMock( 'stdClass', [ 'reverse' ] );
$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' ) );
}
* 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->getMock( 'stdClass', [ 'computeSomething' ] );
$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() );
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' )
);
}
public function testShortcutMethod() {
$this->assertEquals(
'this is correct',
- MemoizedCallable::call( 'sprintf', array( 'this is %s', 'correct' ) )
+ MemoizedCallable::call( 'sprintf', [ 'this is %s', 'correct' ] )
);
}
$this->readAttribute( $a, 'callableName' ),
$this->readAttribute( $b, 'callableName' )
);
+
+ $c = new ArrayBackedMemoizedCallable( function () {
+ return rand();
+ } );
+ $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
}
/**