Merge "SECURITY: Do not allow users to undelete a page they can't edit or create"
[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 /**
30 * The memoized callable should relate inputs to outputs in the same
31 * way as the original underlying callable.
32 */
33 public function testReturnValuePassedThrough() {
34 $mock = $this->getMock( 'stdClass', [ 'reverse' ] );
35 $mock->expects( $this->any() )
36 ->method( 'reverse' )
37 ->will( $this->returnCallback( 'strrev' ) );
38
39 $memoized = new MemoizedCallable( [ $mock, 'reverse' ] );
40 $this->assertEquals( 'flow', $memoized->invoke( 'wolf' ) );
41 }
42
43 /**
44 * Consecutive calls to the memoized callable with the same arguments
45 * should result in just one invocation of the underlying callable.
46 *
47 * @requires function apc_store/apcu_store
48 */
49 public function testCallableMemoized() {
50 $observer = $this->getMock( 'stdClass', [ 'computeSomething' ] );
51 $observer->expects( $this->once() )
52 ->method( 'computeSomething' )
53 ->will( $this->returnValue( 'ok' ) );
54
55 $memoized = new ArrayBackedMemoizedCallable( [ $observer, 'computeSomething' ] );
56
57 // First invocation -- delegates to $observer->computeSomething()
58 $this->assertEquals( 'ok', $memoized->invoke() );
59
60 // Second invocation -- returns memoized result
61 $this->assertEquals( 'ok', $memoized->invoke() );
62 }
63
64 /**
65 * @covers MemoizedCallable::invoke
66 */
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' )
72 );
73 }
74
75 /**
76 * @covers MemoizedCallable::call
77 */
78 public function testShortcutMethod() {
79 $this->assertEquals(
80 'this is correct',
81 MemoizedCallable::call( 'sprintf', [ 'this is %s', 'correct' ] )
82 );
83 }
84
85 /**
86 * Outlier TTL values should be coerced to range 1 - 86400.
87 */
88 public function testTTLMaxMin() {
89 $memoized = new MemoizedCallable( 'abs', 100000 );
90 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
91
92 $memoized = new MemoizedCallable( 'abs', -10 );
93 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
94 }
95
96 /**
97 * Closure names should be distinct.
98 */
99 public function testMemoizedClosure() {
100 $a = new MemoizedCallable( function () {
101 return 'a';
102 } );
103
104 $b = new MemoizedCallable( function () {
105 return 'b';
106 } );
107
108 $this->assertEquals( $a->invokeArgs(), 'a' );
109 $this->assertEquals( $b->invokeArgs(), 'b' );
110
111 $this->assertNotEquals(
112 $this->readAttribute( $a, 'callableName' ),
113 $this->readAttribute( $b, 'callableName' )
114 );
115
116 $c = new ArrayBackedMemoizedCallable( function () {
117 return rand();
118 } );
119 $this->assertEquals( $c->invokeArgs(), $c->invokeArgs(), 'memoized random' );
120 }
121
122 /**
123 * @expectedExceptionMessage non-scalar argument
124 * @expectedException InvalidArgumentException
125 */
126 public function testNonScalarArguments() {
127 $memoized = new MemoizedCallable( 'gettype' );
128 $memoized->invoke( new stdClass() );
129 }
130
131 /**
132 * @expectedExceptionMessage must be an instance of callable
133 * @expectedException InvalidArgumentException
134 */
135 public function testNotCallable() {
136 $memoized = new MemoizedCallable( 14 );
137 }
138 }