3 * A MemoizedCallable subclass that stores function return values
4 * in an instance property rather than APC.
6 class ArrayBackedMemoizedCallable
extends MemoizedCallable
{
7 public $cache = array();
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', array( 'reverse' ) );
35 $mock->expects( $this->any() )
37 ->will( $this->returnCallback( 'strrev' ) );
39 $memoized = new MemoizedCallable( array( $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
49 public function testCallableMemoized() {
50 $observer = $this->getMock( 'stdClass', array( 'computeSomething' ) );
51 $observer->expects( $this->once() )
52 ->method( 'computeSomething' )
53 ->will( $this->returnValue( 'ok' ) );
55 $memoized = new ArrayBackedMemoizedCallable( array( $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( array( 'this is %s', 'correct' ) ),
71 $memoized->invoke( 'this is %s', 'correct' )
76 * @covers MemoizedCallable::call
78 public function testShortcutMethod() {
81 MemoizedCallable
::call( 'sprintf', array( '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' )
118 * @expectedExceptionMessage non-scalar argument
119 * @expectedException InvalidArgumentException
121 public function testNonScalarArguments() {
122 $memoized = new MemoizedCallable( 'gettype' );
123 $memoized->invoke( new stdClass() );
127 * @expectedExceptionMessage must be an instance of callable
128 * @expectedException InvalidArgumentException
130 public function testNotCallable() {
131 $memoized = new MemoizedCallable( 14 );