MediaWiki
REL1_30
MemoizedCallableTest.php
Go to the documentation of this file.
1
<?php
6
class
ArrayBackedMemoizedCallable
extends
MemoizedCallable
{
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
27
class
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
}
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition
COPYING.txt:326
$success
$success
Definition
NoLocalSettings.php:44
ArrayBackedMemoizedCallable
A MemoizedCallable subclass that stores function return values in an instance property rather than AP...
Definition
MemoizedCallableTest.php:6
ArrayBackedMemoizedCallable\$cache
$cache
Definition
MemoizedCallableTest.php:7
ArrayBackedMemoizedCallable\fetchResult
fetchResult( $key, &$success)
Fetch the result of a previous invocation from APC or APCu.
Definition
MemoizedCallableTest.php:9
ArrayBackedMemoizedCallable\storeResult
storeResult( $key, $result)
Store the result of an invocation in APC or APCu.
Definition
MemoizedCallableTest.php:18
MemoizedCallableTest
PHP Unit tests for MemoizedCallable class.
Definition
MemoizedCallableTest.php:27
MemoizedCallableTest\testMemoizedClosure
testMemoizedClosure()
Closure names should be distinct.
Definition
MemoizedCallableTest.php:101
MemoizedCallableTest\testTTLMaxMin
testTTLMaxMin()
Outlier TTL values should be coerced to range 1 - 86400.
Definition
MemoizedCallableTest.php:90
MemoizedCallableTest\testShortcutMethod
testShortcutMethod()
MemoizedCallable::call.
Definition
MemoizedCallableTest.php:80
MemoizedCallableTest\testInvokeVariadic
testInvokeVariadic()
MemoizedCallable::invoke.
Definition
MemoizedCallableTest.php:69
MemoizedCallableTest\testNonScalarArguments
testNonScalarArguments()
non-scalar argument InvalidArgumentException
Definition
MemoizedCallableTest.php:128
MemoizedCallableTest\testNotCallable
testNotCallable()
must be an instance of callable InvalidArgumentException
Definition
MemoizedCallableTest.php:137
MemoizedCallableTest\testReturnValuePassedThrough
testReturnValuePassedThrough()
The memoized callable should relate inputs to outputs in the same way as the original underlying call...
Definition
MemoizedCallableTest.php:33
MemoizedCallableTest\testCallableMemoized
testCallableMemoized()
Consecutive calls to the memoized callable with the same arguments should result in just one invocati...
Definition
MemoizedCallableTest.php:50
MemoizedCallable
APC-backed and APCu-backed function memoization.
Definition
MemoizedCallable.php:43
MemoizedCallable\call
static call( $callable, array $args=[], $ttl=3600)
Shortcut method for creating a MemoizedCallable and invoking it with the specified arguments.
Definition
MemoizedCallable.php:154
$result
namespace being checked & $result
Definition
hooks.txt:2293
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition
maintenance.txt:55
tests
phpunit
includes
libs
MemoizedCallableTest.php
Generated on Mon Nov 25 2024 15:43:49 for MediaWiki by
1.10.0