Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / libs / MemoizedCallableTest.php
blob881f5e11679392fe9a6de992c2695bfe0fa594bf
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->getMock( 'stdClass', [ 'reverse' ] );
35 $mock->expects( $this->any() )
36 ->method( 'reverse' )
37 ->will( $this->returnCallback( 'strrev' ) );
39 $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
40 $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
43 /**
44 * Consecutive calls to the memoized callable with the same arguments
45 * should result in just one invocation of the underlying callable.
47 * @requires function apc_store/apcu_store
49 public function testCallableMemoized() {
50 $observer = $this->getMock( 'stdClass', [ 'computeSomething' ] );
51 $observer->expects( $this->once() )
52 ->method( 'computeSomething' )
53 ->will( $this->returnValue( 'ok' ) );
55 $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
57 // First invocation -- delegates to $observer->computeSomething()
58 $this->assertEquals( 'ok', $memoized->invoke() );
60 // Second invocation -- returns memoized result
61 $this->assertEquals( 'ok', $memoized->invoke() );
64 /**
65 * @covers MemoizedCallable::invoke
67 public function testInvokeVariadic() {
68 $memoized = new MemoizedCallable( 'sprintf' );
69 $this->assertEquals(
70 $memoized->invokeArgs( [ 'this is %s', 'correct' ] ),
71 $memoized->invoke( 'this is %s', 'correct' )
75 /**
76 * @covers MemoizedCallable::call
78 public function testShortcutMethod() {
79 $this->assertEquals(
80 'this is correct',
81 MemoizedCallable::call( 'sprintf', [ 'this is %s', 'correct' ] )
85 /**
86 * Outlier TTL values should be coerced to range 1 - 86400.
88 public function testTTLMaxMin() {
89 $memoized = new MemoizedCallable( 'abs', 100000 );
90 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
92 $memoized = new MemoizedCallable( 'abs', -10 );
93 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
96 /**
97 * Closure names should be distinct.
99 public function testMemoizedClosure() {
100 $a = new MemoizedCallable( function () {
101 return 'a';
102 } );
104 $b = new MemoizedCallable( function () {
105 return 'b';
106 } );
108 $this->assertEquals( $a->invokeArgs(), 'a' );
109 $this->assertEquals( $b->invokeArgs(), 'b' );
111 $this->assertNotEquals(
112 $this->readAttribute( $a, 'callableName' ),
113 $this->readAttribute( $b, 'callableName' )
116 $c = new ArrayBackedMemoizedCallable( function () {
117 return rand();
118 } );
119 $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
123 * @expectedExceptionMessage non-scalar argument
124 * @expectedException InvalidArgumentException
126 public function testNonScalarArguments() {
127 $memoized = new MemoizedCallable( 'gettype' );
128 $memoized->invoke( new stdClass() );
132 * @expectedExceptionMessage must be an instance of callable
133 * @expectedException InvalidArgumentException
135 public function testNotCallable() {
136 $memoized = new MemoizedCallable( 14 );