3 * PHPUnit tests for MemoizedCallable class.
4 * @covers MemoizedCallable
6 class MemoizedCallableTest
extends PHPUnit\Framework\TestCase
{
8 use MediaWikiCoversValidator
;
11 * The memoized callable should relate inputs to outputs in the same
12 * way as the original underlying callable.
14 public function testReturnValuePassedThrough() {
15 $mock = $this->getMockBuilder( stdClass
::class )
16 ->setMethods( [ 'reverse' ] )->getMock();
17 $mock->expects( $this->any() )
19 ->will( $this->returnCallback( 'strrev' ) );
21 $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
22 $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
26 * Consecutive calls to the memoized callable with the same arguments
27 * should result in just one invocation of the underlying callable.
29 * @requires extension apcu
31 public function testCallableMemoized() {
32 $observer = $this->getMockBuilder( stdClass
::class )
33 ->setMethods( [ 'computeSomething' ] )->getMock();
34 $observer->expects( $this->once() )
35 ->method( 'computeSomething' )
36 ->will( $this->returnValue( 'ok' ) );
38 $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
40 // First invocation -- delegates to $observer->computeSomething()
41 $this->assertEquals( 'ok', $memoized->invoke() );
43 // Second invocation -- returns memoized result
44 $this->assertEquals( 'ok', $memoized->invoke() );
48 * @covers MemoizedCallable::invoke
50 public function testInvokeVariadic() {
51 $memoized = new MemoizedCallable( 'sprintf' );
53 $memoized->invokeArgs( [ 'this is %s', 'correct' ] ),
54 $memoized->invoke( 'this is %s', 'correct' )
59 * @covers MemoizedCallable::call
61 public function testShortcutMethod() {
64 MemoizedCallable
::call( 'sprintf', [ 'this is %s', 'correct' ] )
69 * Outlier TTL values should be coerced to range 1 - 86400.
71 public function testTTLMaxMin() {
72 $memoized = new MemoizedCallable( 'abs', 100000 );
73 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
75 $memoized = new MemoizedCallable( 'abs', -10 );
76 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
80 * Closure names should be distinct.
82 public function testMemoizedClosure() {
83 $a = new MemoizedCallable( function () {
87 $b = new MemoizedCallable( function () {
91 $this->assertEquals( $a->invokeArgs(), 'a' );
92 $this->assertEquals( $b->invokeArgs(), 'b' );
94 $this->assertNotEquals(
95 $this->readAttribute( $a, 'callableName' ),
96 $this->readAttribute( $b, 'callableName' )
99 $c = new ArrayBackedMemoizedCallable( function () {
102 $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
106 * @expectedExceptionMessage non-scalar argument
107 * @expectedException InvalidArgumentException
109 public function testNonScalarArguments() {
110 $memoized = new MemoizedCallable( 'gettype' );
111 $memoized->invoke( new stdClass() );
115 * @expectedExceptionMessage must be an instance of callable
116 * @expectedException InvalidArgumentException
118 public function testNotCallable() {
119 $memoized = new MemoizedCallable( 14 );
124 * A MemoizedCallable subclass that stores function return values
125 * in an instance property rather than APC or APCu.
127 class ArrayBackedMemoizedCallable
extends MemoizedCallable
{
130 protected function fetchResult( $key, &$success ) {
131 if ( array_key_exists( $key, $this->cache
) ) {
133 return $this->cache
[$key];
139 protected function storeResult( $key, $result ) {
140 $this->cache
[$key] = $result;