Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / MemoizedCallableTest.php
1 <?php
2 /**
3 * PHPUnit tests for MemoizedCallable class.
4 * @covers MemoizedCallable
5 */
6 class MemoizedCallableTest extends PHPUnit\Framework\TestCase {
7
8 use MediaWikiCoversValidator;
9
10 /**
11 * The memoized callable should relate inputs to outputs in the same
12 * way as the original underlying callable.
13 */
14 public function testReturnValuePassedThrough() {
15 $mock = $this->getMockBuilder( stdClass::class )
16 ->setMethods( [ 'reverse' ] )->getMock();
17 $mock->expects( $this->any() )
18 ->method( 'reverse' )
19 ->will( $this->returnCallback( 'strrev' ) );
20
21 $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
22 $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
23 }
24
25 /**
26 * Consecutive calls to the memoized callable with the same arguments
27 * should result in just one invocation of the underlying callable.
28 *
29 * @requires extension apcu
30 */
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' ) );
37
38 $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
39
40 // First invocation -- delegates to $observer->computeSomething()
41 $this->assertEquals( 'ok', $memoized->invoke() );
42
43 // Second invocation -- returns memoized result
44 $this->assertEquals( 'ok', $memoized->invoke() );
45 }
46
47 /**
48 * @covers MemoizedCallable::invoke
49 */
50 public function testInvokeVariadic() {
51 $memoized = new MemoizedCallable( 'sprintf' );
52 $this->assertEquals(
53 $memoized->invokeArgs( [ 'this is %s', 'correct' ] ),
54 $memoized->invoke( 'this is %s', 'correct' )
55 );
56 }
57
58 /**
59 * @covers MemoizedCallable::call
60 */
61 public function testShortcutMethod() {
62 $this->assertEquals(
63 'this is correct',
64 MemoizedCallable::call( 'sprintf', [ 'this is %s', 'correct' ] )
65 );
66 }
67
68 /**
69 * Outlier TTL values should be coerced to range 1 - 86400.
70 */
71 public function testTTLMaxMin() {
72 $memoized = new MemoizedCallable( 'abs', 100000 );
73 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
74
75 $memoized = new MemoizedCallable( 'abs', -10 );
76 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
77 }
78
79 /**
80 * Closure names should be distinct.
81 */
82 public function testMemoizedClosure() {
83 $a = new MemoizedCallable( function () {
84 return 'a';
85 } );
86
87 $b = new MemoizedCallable( function () {
88 return 'b';
89 } );
90
91 $this->assertEquals( $a->invokeArgs(), 'a' );
92 $this->assertEquals( $b->invokeArgs(), 'b' );
93
94 $this->assertNotEquals(
95 $this->readAttribute( $a, 'callableName' ),
96 $this->readAttribute( $b, 'callableName' )
97 );
98
99 $c = new ArrayBackedMemoizedCallable( function () {
100 return rand();
101 } );
102 $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
103 }
104
105 /**
106 * @expectedExceptionMessage non-scalar argument
107 * @expectedException InvalidArgumentException
108 */
109 public function testNonScalarArguments() {
110 $memoized = new MemoizedCallable( 'gettype' );
111 $memoized->invoke( new stdClass() );
112 }
113
114 /**
115 * @expectedExceptionMessage must be an instance of callable
116 * @expectedException InvalidArgumentException
117 */
118 public function testNotCallable() {
119 $memoized = new MemoizedCallable( 14 );
120 }
121 }
122
123 /**
124 * A MemoizedCallable subclass that stores function return values
125 * in an instance property rather than APC or APCu.
126 */
127 class ArrayBackedMemoizedCallable extends MemoizedCallable {
128 private $cache = [];
129
130 protected function fetchResult( $key, &$success ) {
131 if ( array_key_exists( $key, $this->cache ) ) {
132 $success = true;
133 return $this->cache[$key];
134 }
135 $success = false;
136 return false;
137 }
138
139 protected function storeResult( $key, $result ) {
140 $this->cache[$key] = $result;
141 }
142 }