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->getMock( 'stdClass', [ 'reverse' ] );
35 $mock->expects( $this->any() )
37 ->will( $this->returnCallback( 'strrev' ) );
39 $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
40 $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
44 * Consecutive calls to the memoized callable with the same arguments
45 * should result in just one invocation of the underlying callable.
47 * @requires function apc_store/apcu_store
49 public function testCallableMemoized() {
50 $observer = $this->getMock( 'stdClass', [ 'computeSomething' ] );
51 $observer->expects( $this->once() )
52 ->method( 'computeSomething' )
53 ->will( $this->returnValue( 'ok' ) );
55 $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
57 // First invocation -- delegates to $observer->computeSomething()
58 $this->assertEquals( 'ok', $memoized->invoke() );
60 // Second invocation -- returns memoized result
61 $this->assertEquals( 'ok', $memoized->invoke() );
65 * @covers MemoizedCallable::invoke
67 public function testInvokeVariadic() {
68 $memoized = new MemoizedCallable( 'sprintf' );
70 $memoized->invokeArgs( [ 'this is %s', 'correct' ] ),
71 $memoized->invoke( 'this is %s', 'correct' )
76 * @covers MemoizedCallable::call
78 public function testShortcutMethod() {
81 MemoizedCallable
::call( 'sprintf', [ 'this is %s', 'correct' ] )
86 * Outlier TTL values should be coerced to range 1 - 86400.
88 public function testTTLMaxMin() {
89 $memoized = new MemoizedCallable( 'abs', 100000 );
90 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
92 $memoized = new MemoizedCallable( 'abs', -10 );
93 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
97 * Closure names should be distinct.
99 public function testMemoizedClosure() {
100 $a = new MemoizedCallable( function () {
104 $b = new MemoizedCallable( function () {
108 $this->assertEquals( $a->invokeArgs(), 'a' );
109 $this->assertEquals( $b->invokeArgs(), 'b' );
111 $this->assertNotEquals(
112 $this->readAttribute( $a, 'callableName' ),
113 $this->readAttribute( $b, 'callableName' )
116 $c = new ArrayBackedMemoizedCallable( function () {
119 $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
123 * @expectedExceptionMessage non-scalar argument
124 * @expectedException InvalidArgumentException
126 public function testNonScalarArguments() {
127 $memoized = new MemoizedCallable( 'gettype' );
128 $memoized->invoke( new stdClass() );
132 * @expectedExceptionMessage must be an instance of callable
133 * @expectedException InvalidArgumentException
135 public function testNotCallable() {
136 $memoized = new MemoizedCallable( 14 );