rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / libs / MemoizedCallableTest.php
blobd99c58781cc1f41583488ad22347903f31ac0d69
1 <?php
2 /**
3 * A MemoizedCallable subclass that stores function return values
4 * in an instance property rather than APC or APCu.
5 */
6 class ArrayBackedMemoizedCallable extends MemoizedCallable {
7 private $cache = [];
9 protected function fetchResult( $key, &$success ) {
10 if ( array_key_exists( $key, $this->cache ) ) {
11 $success = true;
12 return $this->cache[$key];
14 $success = false;
15 return false;
18 protected function storeResult( $key, $result ) {
19 $this->cache[$key] = $result;
23 /**
24 * PHP Unit tests for MemoizedCallable class.
25 * @covers MemoizedCallable
27 class MemoizedCallableTest extends PHPUnit_Framework_TestCase {
29 /**
30 * The memoized callable should relate inputs to outputs in the same
31 * way as the original underlying callable.
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' ) );
40 $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
41 $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
44 /**
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/apcu_store
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' ) );
57 $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
59 // First invocation -- delegates to $observer->computeSomething()
60 $this->assertEquals( 'ok', $memoized->invoke() );
62 // Second invocation -- returns memoized result
63 $this->assertEquals( 'ok', $memoized->invoke() );
66 /**
67 * @covers MemoizedCallable::invoke
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' )
77 /**
78 * @covers MemoizedCallable::call
80 public function testShortcutMethod() {
81 $this->assertEquals(
82 'this is correct',
83 MemoizedCallable::call( 'sprintf', [ 'this is %s', 'correct' ] )
87 /**
88 * Outlier TTL values should be coerced to range 1 - 86400.
90 public function testTTLMaxMin() {
91 $memoized = new MemoizedCallable( 'abs', 100000 );
92 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
94 $memoized = new MemoizedCallable( 'abs', -10 );
95 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
98 /**
99 * Closure names should be distinct.
101 public function testMemoizedClosure() {
102 $a = new MemoizedCallable( function () {
103 return 'a';
104 } );
106 $b = new MemoizedCallable( function () {
107 return 'b';
108 } );
110 $this->assertEquals( $a->invokeArgs(), 'a' );
111 $this->assertEquals( $b->invokeArgs(), 'b' );
113 $this->assertNotEquals(
114 $this->readAttribute( $a, 'callableName' ),
115 $this->readAttribute( $b, 'callableName' )
118 $c = new ArrayBackedMemoizedCallable( function () {
119 return rand();
120 } );
121 $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
125 * @expectedExceptionMessage non-scalar argument
126 * @expectedException InvalidArgumentException
128 public function testNonScalarArguments() {
129 $memoized = new MemoizedCallable( 'gettype' );
130 $memoized->invoke( new stdClass() );
134 * @expectedExceptionMessage must be an instance of callable
135 * @expectedException InvalidArgumentException
137 public function testNotCallable() {
138 $memoized = new MemoizedCallable( 14 );