MediaWiki REL1_30
MemoizedCallableTest.php
Go to the documentation of this file.
1<?php
7 private $cache = [];
8
9 protected function fetchResult( $key, &$success ) {
10 if ( array_key_exists( $key, $this->cache ) ) {
11 $success = true;
12 return $this->cache[$key];
13 }
14 $success = false;
15 return false;
16 }
17
18 protected function storeResult( $key, $result ) {
19 $this->cache[$key] = $result;
20 }
21}
22
27class MemoizedCallableTest extends PHPUnit_Framework_TestCase {
28
33 public function testReturnValuePassedThrough() {
34 $mock = $this->getMockBuilder( 'stdClass' )
35 ->setMethods( [ 'reverse' ] )->getMock();
36 $mock->expects( $this->any() )
37 ->method( 'reverse' )
38 ->will( $this->returnCallback( 'strrev' ) );
39
40 $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
41 $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
42 }
43
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' ) );
56
57 $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
58
59 // First invocation -- delegates to $observer->computeSomething()
60 $this->assertEquals( 'ok', $memoized->invoke() );
61
62 // Second invocation -- returns memoized result
63 $this->assertEquals( 'ok', $memoized->invoke() );
64 }
65
69 public function testInvokeVariadic() {
70 $memoized = new MemoizedCallable( 'sprintf' );
71 $this->assertEquals(
72 $memoized->invokeArgs( [ 'this is %s', 'correct' ] ),
73 $memoized->invoke( 'this is %s', 'correct' )
74 );
75 }
76
80 public function testShortcutMethod() {
81 $this->assertEquals(
82 'this is correct',
83 MemoizedCallable::call( 'sprintf', [ 'this is %s', 'correct' ] )
84 );
85 }
86
90 public function testTTLMaxMin() {
91 $memoized = new MemoizedCallable( 'abs', 100000 );
92 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
93
94 $memoized = new MemoizedCallable( 'abs', -10 );
95 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
96 }
97
101 public function testMemoizedClosure() {
102 $a = new MemoizedCallable( function () {
103 return 'a';
104 } );
105
106 $b = new MemoizedCallable( function () {
107 return 'b';
108 } );
109
110 $this->assertEquals( $a->invokeArgs(), 'a' );
111 $this->assertEquals( $b->invokeArgs(), 'b' );
112
113 $this->assertNotEquals(
114 $this->readAttribute( $a, 'callableName' ),
115 $this->readAttribute( $b, 'callableName' )
116 );
117
118 $c = new ArrayBackedMemoizedCallable( function () {
119 return rand();
120 } );
121 $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
122 }
123
128 public function testNonScalarArguments() {
129 $memoized = new MemoizedCallable( 'gettype' );
130 $memoized->invoke( new stdClass() );
131 }
132
137 public function testNotCallable() {
138 $memoized = new MemoizedCallable( 14 );
139 }
140}
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition COPYING.txt:326
A MemoizedCallable subclass that stores function return values in an instance property rather than AP...
fetchResult( $key, &$success)
Fetch the result of a previous invocation from APC or APCu.
storeResult( $key, $result)
Store the result of an invocation in APC or APCu.
PHP Unit tests for MemoizedCallable class.
testMemoizedClosure()
Closure names should be distinct.
testTTLMaxMin()
Outlier TTL values should be coerced to range 1 - 86400.
testShortcutMethod()
MemoizedCallable::call.
testInvokeVariadic()
MemoizedCallable::invoke.
testNonScalarArguments()
non-scalar argument InvalidArgumentException
testNotCallable()
must be an instance of callable InvalidArgumentException
testReturnValuePassedThrough()
The memoized callable should relate inputs to outputs in the same way as the original underlying call...
testCallableMemoized()
Consecutive calls to the memoized callable with the same arguments should result in just one invocati...
APC-backed and APCu-backed function memoization.
static call( $callable, array $args=[], $ttl=3600)
Shortcut method for creating a MemoizedCallable and invoking it with the specified arguments.
namespace being checked & $result
Definition hooks.txt:2293
you have access to all of the normal MediaWiki so you can get a DB use the cache