Merge "Update comment about enabled extensions"
[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.
5 */
6 class ArrayBackedMemoizedCallable extends MemoizedCallable {
7 public $cache = array();
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 /**
25 * PHP Unit tests for MemoizedCallable class.
26 * @covers MemoizedCallable
27 */
28 class MemoizedCallableTest extends PHPUnit_Framework_TestCase {
29
30 /**
31 * The memoized callable should relate inputs to outputs in the same
32 * way as the original underlying callable.
33 */
34 public function testReturnValuePassedThrough() {
35 $mock = $this->getMock( 'stdClass', array( 'reverse' ) );
36 $mock->expects( $this->any() )
37 ->method( 'reverse' )
38 ->will( $this->returnCallback( 'strrev' ) );
39
40 $memoized = new MemoizedCallable( array( $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
49 */
50 public function testCallableMemoized() {
51 $observer = $this->getMock( 'stdClass', array( 'computeSomething' ) );
52 $observer->expects( $this->once() )
53 ->method( 'computeSomething' )
54 ->will( $this->returnValue( 'ok' ) );
55
56 $memoized = new ArrayBackedMemoizedCallable( array( $observer, 'computeSomething' ) );
57
58 // First invocation -- delegates to $observer->computeSomething()
59 $this->assertEquals( 'ok', $memoized->invoke() );
60
61 // Second invocation -- returns memoized result
62 $this->assertEquals( 'ok', $memoized->invoke() );
63 }
64
65 /**
66 * @covers MemoizedCallable::invoke
67 */
68 public function testInvokeVariadic() {
69 $memoized = new MemoizedCallable( 'sprintf' );
70 $this->assertEquals(
71 $memoized->invokeArgs( array( 'this is %s', 'correct' ) ),
72 $memoized->invoke( 'this is %s', 'correct' )
73 );
74 }
75
76 /**
77 * @covers MemoizedCallable::call
78 */
79 public function testShortcutMethod() {
80 $this->assertEquals(
81 'this is correct',
82 MemoizedCallable::call( 'sprintf', array( 'this is %s', 'correct' ) )
83 );
84 }
85
86 /**
87 * Outlier TTL values should be coerced to range 1 - 86400.
88 */
89 public function testTTLMaxMin() {
90 $memoized = new MemoizedCallable( 'abs', 100000 );
91 $this->assertEquals( 86400, $this->readAttribute( $memoized, 'ttl' ) );
92
93 $memoized = new MemoizedCallable( 'abs', -10 );
94 $this->assertEquals( 1, $this->readAttribute( $memoized, 'ttl' ) );
95 }
96
97 /**
98 * Closure names should be distinct.
99 */
100 public function testMemoizedClosure() {
101 $a = new MemoizedCallable( function () {
102 return 'a';
103 } );
104
105 $b = new MemoizedCallable( function () {
106 return 'b';
107 } );
108
109 $this->assertEquals( $a->invokeArgs(), 'a' );
110 $this->assertEquals( $b->invokeArgs(), 'b' );
111
112 $this->assertNotEquals(
113 $this->readAttribute( $a, 'callableName' ),
114 $this->readAttribute( $b, 'callableName' )
115 );
116 }
117
118 /**
119 * @expectedExceptionMessage non-scalar argument
120 * @expectedException InvalidArgumentException
121 */
122 public function testNonScalarArguments() {
123 $memoized = new MemoizedCallable( 'gettype' );
124 $memoized->invoke( new stdClass() );
125 }
126
127 /**
128 * @expectedExceptionMessage must be an instance of callable
129 * @expectedException InvalidArgumentException
130 */
131 public function testNotCallable() {
132 $memoized = new MemoizedCallable( 14 );
133 }
134 }