MediaWiki
REL1_31
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
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
}
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:42
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:103
MemoizedCallableTest\testTTLMaxMin
testTTLMaxMin()
Outlier TTL values should be coerced to range 1 - 86400.
Definition
MemoizedCallableTest.php:92
MemoizedCallableTest\testShortcutMethod
testShortcutMethod()
MemoizedCallable::call.
Definition
MemoizedCallableTest.php:82
MemoizedCallableTest\testInvokeVariadic
testInvokeVariadic()
MemoizedCallable::invoke.
Definition
MemoizedCallableTest.php:71
MemoizedCallableTest\testNonScalarArguments
testNonScalarArguments()
non-scalar argument InvalidArgumentException
Definition
MemoizedCallableTest.php:130
MemoizedCallableTest\testNotCallable
testNotCallable()
must be an instance of callable InvalidArgumentException
Definition
MemoizedCallableTest.php:139
MemoizedCallableTest\testReturnValuePassedThrough
testReturnValuePassedThrough()
The memoized callable should relate inputs to outputs in the same way as the original underlying call...
Definition
MemoizedCallableTest.php:35
MemoizedCallableTest\testCallableMemoized
testCallableMemoized()
Consecutive calls to the memoized callable with the same arguments should result in just one invocati...
Definition
MemoizedCallableTest.php:52
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:2323
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:36:25 for MediaWiki by
1.10.0