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