MediaWiki REL1_31
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
29 use MediaWikiCoversValidator;
30
35 public function testReturnValuePassedThrough() {
36 $mock = $this->getMockBuilder( stdClass::class )
37 ->setMethods( [ 'reverse' ] )->getMock();
38 $mock->expects( $this->any() )
39 ->method( 'reverse' )
40 ->will( $this->returnCallback( 'strrev' ) );
41
42 $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
43 $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
44 }
45
52 public function testCallableMemoized() {
53 $observer = $this->getMockBuilder( stdClass::class )
54 ->setMethods( [ 'computeSomething' ] )->getMock();
55 $observer->expects( $this->once() )
56 ->method( 'computeSomething' )
57 ->will( $this->returnValue( 'ok' ) );
58
59 $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
60
61 // First invocation -- delegates to $observer->computeSomething()
62 $this->assertEquals( 'ok', $memoized->invoke() );
63
64 // Second invocation -- returns memoized result
65 $this->assertEquals( 'ok', $memoized->invoke() );
66 }
67
71 public function testInvokeVariadic() {
72 $memoized = new MemoizedCallable( 'sprintf' );
73 $this->assertEquals(
74 $memoized->invokeArgs( [ 'this is %s', 'correct' ] ),
75 $memoized->invoke( 'this is %s', 'correct' )
76 );
77 }
78
82 public function testShortcutMethod() {
83 $this->assertEquals(
84 'this is correct',
85 MemoizedCallable::call( 'sprintf', [ 'this is %s', 'correct' ] )
86 );
87 }
88
92 public function testTTLMaxMin() {
93 $memoized = new MemoizedCallable( 'abs', 100000 );
94 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
95
96 $memoized = new MemoizedCallable( 'abs', -10 );
97 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
98 }
99
103 public function testMemoizedClosure() {
104 $a = new MemoizedCallable( function () {
105 return 'a';
106 } );
107
108 $b = new MemoizedCallable( function () {
109 return 'b';
110 } );
111
112 $this->assertEquals( $a->invokeArgs(), 'a' );
113 $this->assertEquals( $b->invokeArgs(), 'b' );
114
115 $this->assertNotEquals(
116 $this->readAttribute( $a, 'callableName' ),
117 $this->readAttribute( $b, 'callableName' )
118 );
119
120 $c = new ArrayBackedMemoizedCallable( function () {
121 return rand();
122 } );
123 $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
124 }
125
130 public function testNonScalarArguments() {
131 $memoized = new MemoizedCallable( 'gettype' );
132 $memoized->invoke( new stdClass() );
133 }
134
139 public function testNotCallable() {
140 $memoized = new MemoizedCallable( 14 );
141 }
142}
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:2323
you have access to all of the normal MediaWiki so you can get a DB use the cache