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