3 * A MemoizedCallable subclass that stores function return values
4 * in an instance property rather than APC or APCu.
6 class ArrayBackedMemoizedCallable
extends MemoizedCallable
{
9 protected function fetchResult( $key, &$success ) {
10 if ( array_key_exists( $key, $this->cache
) ) {
12 return $this->cache
[$key];
18 protected function storeResult( $key, $result ) {
19 $this->cache
[$key] = $result;
24 * PHP Unit tests for MemoizedCallable class.
25 * @covers MemoizedCallable
27 class MemoizedCallableTest
extends PHPUnit_Framework_TestCase
{
30 * The memoized callable should relate inputs to outputs in the same
31 * way as the original underlying callable.
33 public function testReturnValuePassedThrough() {
34 $mock = $this->getMockBuilder( 'stdClass' )
35 ->setMethods( [ 'reverse' ] )->getMock();
36 $mock->expects( $this->any() )
38 ->will( $this->returnCallback( 'strrev' ) );
40 $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
41 $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
45 * Consecutive calls to the memoized callable with the same arguments
46 * should result in just one invocation of the underlying callable.
48 * @requires function apc_store/apcu_store
50 public function testCallableMemoized() {
51 $observer = $this->getMockBuilder( 'stdClass' )
52 ->setMethods( [ 'computeSomething' ] )->getMock();
53 $observer->expects( $this->once() )
54 ->method( 'computeSomething' )
55 ->will( $this->returnValue( 'ok' ) );
57 $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
59 // First invocation -- delegates to $observer->computeSomething()
60 $this->assertEquals( 'ok', $memoized->invoke() );
62 // Second invocation -- returns memoized result
63 $this->assertEquals( 'ok', $memoized->invoke() );
67 * @covers MemoizedCallable::invoke
69 public function testInvokeVariadic() {
70 $memoized = new MemoizedCallable( 'sprintf' );
72 $memoized->invokeArgs( [ 'this is %s', 'correct' ] ),
73 $memoized->invoke( 'this is %s', 'correct' )
78 * @covers MemoizedCallable::call
80 public function testShortcutMethod() {
83 MemoizedCallable
::call( 'sprintf', [ 'this is %s', 'correct' ] )
88 * Outlier TTL values should be coerced to range 1 - 86400.
90 public function testTTLMaxMin() {
91 $memoized = new MemoizedCallable( 'abs', 100000 );
92 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
94 $memoized = new MemoizedCallable( 'abs', -10 );
95 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
99 * Closure names should be distinct.
101 public function testMemoizedClosure() {
102 $a = new MemoizedCallable( function () {
106 $b = new MemoizedCallable( function () {
110 $this->assertEquals( $a->invokeArgs(), 'a' );
111 $this->assertEquals( $b->invokeArgs(), 'b' );
113 $this->assertNotEquals(
114 $this->readAttribute( $a, 'callableName' ),
115 $this->readAttribute( $b, 'callableName' )
118 $c = new ArrayBackedMemoizedCallable( function () {
121 $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
125 * @expectedExceptionMessage non-scalar argument
126 * @expectedException InvalidArgumentException
128 public function testNonScalarArguments() {
129 $memoized = new MemoizedCallable( 'gettype' );
130 $memoized->invoke( new stdClass() );
134 * @expectedExceptionMessage must be an instance of callable
135 * @expectedException InvalidArgumentException
137 public function testNotCallable() {
138 $memoized = new MemoizedCallable( 14 );