| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWikiCoversValidator; |
| 7 |
use MemoizedCallable; |
| 8 |
use PHPUnit\Framework\TestCase; |
| 9 |
use stdClass; |
| 10 |
use Wikimedia\TestingAccessWrapper; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class MemoizedCallableTest extends TestCase { |
| 16 |
|
| 17 |
use MediaWikiCoversValidator; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public function testReturnValuePassedThrough() { |
| 24 |
$mock = $this->getMockBuilder( stdClass::class ) |
| 25 |
->addMethods( [ 'reverse' ] )->getMock(); |
| 26 |
$mock->method( 'reverse' ) |
| 27 |
->willReturnCallback( 'strrev' ); |
| 28 |
|
| 29 |
$memoized = new MemoizedCallable( [ $mock, 'reverse' ] ); |
| 30 |
$this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function testCallableMemoized() { |
| 40 |
$observer = $this->getMockBuilder( stdClass::class ) |
| 41 |
->addMethods( [ 'computeSomething' ] )->getMock(); |
| 42 |
$observer->expects( $this->once() ) |
| 43 |
->method( 'computeSomething' ) |
| 44 |
->willReturn( 'ok' ); |
| 45 |
|
| 46 |
$memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] ); |
| 47 |
|
| 48 |
|
| 49 |
$this->assertEquals( 'ok', $memoized->invoke() ); |
| 50 |
|
| 51 |
|
| 52 |
$this->assertEquals( 'ok', $memoized->invoke() ); |
| 53 |
} |
| 54 |
|
| 55 |
public function testInvokeVariadic() { |
| 56 |
$memoized = new MemoizedCallable( 'sprintf' ); |
| 57 |
$this->assertEquals( |
| 58 |
$memoized->invokeArgs( [ 'this is %s', 'correct' ] ), |
| 59 |
$memoized->invoke( 'this is %s', 'correct' ) |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
public function testShortcutMethod() { |
| 64 |
$this->assertEquals( |
| 65 |
'this is correct', |
| 66 |
MemoizedCallable::call( 'sprintf', [ 'this is %s', 'correct' ] ) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function testTTLMaxMin() { |
| 74 |
$memoized = TestingAccessWrapper::newFromObject( new MemoizedCallable( 'abs', 100000 ) ); |
| 75 |
$this->assertEquals( 86400, $memoized->ttl ); |
| 76 |
|
| 77 |
$memoized = TestingAccessWrapper::newFromObject( new MemoizedCallable( 'abs', -10 ) ); |
| 78 |
$this->assertSame( 1, $memoized->ttl ); |
| 79 |
} |
| 80 |
|
| 81 |
public static function makeA() { |
| 82 |
return 'a'; |
| 83 |
} |
| 84 |
|
| 85 |
public static function makeB() { |
| 86 |
return 'b'; |
| 87 |
} |
| 88 |
|
| 89 |
public static function makeRand() { |
| 90 |
return rand(); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function testMemoizedClosure() { |
| 97 |
$a = new MemoizedCallable( [ self::class, 'makeA' ] ); |
| 98 |
$b = new MemoizedCallable( [ self::class, 'makeB' ] ); |
| 99 |
|
| 100 |
$this->assertEquals( 'a', $a->invokeArgs() ); |
| 101 |
$this->assertEquals( 'b', $b->invokeArgs() ); |
| 102 |
|
| 103 |
$a = TestingAccessWrapper::newFromObject( $a ); |
| 104 |
$b = TestingAccessWrapper::newFromObject( $b ); |
| 105 |
|
| 106 |
$this->assertNotEquals( |
| 107 |
$a->callableName, |
| 108 |
$b->callableName |
| 109 |
); |
| 110 |
|
| 111 |
$c = new ArrayBackedMemoizedCallable( [ self::class, 'makeRand' ] ); |
| 112 |
$this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' ); |
| 113 |
} |
| 114 |
|
| 115 |
public function testNonScalarArguments() { |
| 116 |
$memoized = new MemoizedCallable( 'gettype' ); |
| 117 |
$this->expectExceptionMessage( "non-scalar argument" ); |
| 118 |
$this->expectException( InvalidArgumentException::class ); |
| 119 |
$memoized->invoke( (object)[] ); |
| 120 |
} |
| 121 |
|
| 122 |
public function testUnnamedCallable() { |
| 123 |
$this->expectExceptionMessage( 'Cannot memoize unnamed closure' ); |
| 124 |
$this->expectException( InvalidArgumentException::class ); |
| 125 |
$memoized = new MemoizedCallable( static function () { |
| 126 |
return 'a'; |
| 127 |
} ); |
| 128 |
} |
| 129 |
|
| 130 |
public function testNotCallable() { |
| 131 |
$this->expectExceptionMessage( "must be an instance of callable" ); |
| 132 |
$this->expectException( InvalidArgumentException::class ); |
| 133 |
$memoized = new MemoizedCallable( 14 ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
class ArrayBackedMemoizedCallable extends MemoizedCallable { |
| 142 |
|
| 143 |
private $cache = []; |
| 144 |
|
| 145 |
protected function fetchResult( $key, &$success ) { |
| 146 |
if ( array_key_exists( $key, $this->cache ) ) { |
| 147 |
$success = true; |
| 148 |
return $this->cache[$key]; |
| 149 |
} |
| 150 |
$success = false; |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
protected function storeResult( $key, $result ) { |
| 155 |
$this->cache[$key] = $result; |
| 156 |
} |
| 157 |
} |