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;
25 * PHP Unit tests for MemoizedCallable class.
26 * @covers MemoizedCallable
28 class MemoizedCallableTest
extends PHPUnit_Framework_TestCase
{
31 * The memoized callable should relate inputs to outputs in the same
32 * way as the original underlying callable.
34 public function testReturnValuePassedThrough() {
35 $mock = $this->getMock( 'stdClass', array( 'reverse' ) );
36 $mock->expects( $this->any() )
38 ->will( $this->returnCallback( 'strrev' ) );
40 $memoized = new MemoizedCallable( array( $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
50 public function testCallableMemoized() {
51 $observer = $this->getMock( 'stdClass', array( 'computeSomething' ) );
52 $observer->expects( $this->once() )
53 ->method( 'computeSomething' )
54 ->will( $this->returnValue( 'ok' ) );
56 $memoized = new ArrayBackedMemoizedCallable( array( $observer, 'computeSomething' ) );
58 // First invocation -- delegates to $observer->computeSomething()
59 $this->assertEquals( 'ok', $memoized->invoke() );
61 // Second invocation -- returns memoized result
62 $this->assertEquals( 'ok', $memoized->invoke() );
66 * @covers MemoizedCallable::invoke
68 public function testInvokeVariadic() {
69 $memoized = new MemoizedCallable( 'sprintf' );
71 $memoized->invokeArgs( array( 'this is %s', 'correct' ) ),
72 $memoized->invoke( 'this is %s', 'correct' )
77 * @covers MemoizedCallable::call
79 public function testShortcutMethod() {
82 MemoizedCallable
::call( 'sprintf', array( 'this is %s', 'correct' ) )
87 * Outlier TTL values should be coerced to range 1 - 86400.
89 public function testTTLMaxMin() {
90 $memoized = new MemoizedCallable( 'abs', 100000 );
91 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
93 $memoized = new MemoizedCallable( 'abs', -10 );
94 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
98 * Closure names should be distinct.
100 public function testMemoizedClosure() {
101 $a = new MemoizedCallable( function () {
105 $b = new MemoizedCallable( function () {
109 $this->assertEquals( $a->invokeArgs(), 'a' );
110 $this->assertEquals( $b->invokeArgs(), 'b' );
112 $this->assertNotEquals(
113 $this->readAttribute( $a, 'callableName' ),
114 $this->readAttribute( $b, 'callableName' )
119 * @expectedExceptionMessage non-scalar argument
120 * @expectedException InvalidArgumentException
122 public function testNonScalarArguments() {
123 $memoized = new MemoizedCallable( 'gettype' );
124 $memoized->invoke( new stdClass() );
128 * @expectedExceptionMessage must be an instance of callable
129 * @expectedException InvalidArgumentException
131 public function testNotCallable() {
132 $memoized = new MemoizedCallable( 14 );