|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once dirname(__FILE__) . '/../../PHPUnit/Extensions/MockFunction.php'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @covers PHPUnit_Extensions_MockFunction |
| 7 | + */ |
| 8 | +class Tests_Extensions_MockFunctionTest extends PHPUnit_Framework_TestCase |
| 9 | +{ |
| 10 | + |
| 11 | + /** |
| 12 | + * Object to test. |
| 13 | + * |
| 14 | + * @var PHPUnit_Extensions_MockFunction |
| 15 | + */ |
| 16 | + protected $object; |
| 17 | + |
| 18 | + /** |
| 19 | + * Scope object to use for calling mocked functions. |
| 20 | + * |
| 21 | + * @var PHPUnit_Extensions_MockFunction |
| 22 | + */ |
| 23 | + protected $test_scope_object; |
| 24 | + |
| 25 | + /** |
| 26 | + * Name of the mocked function. |
| 27 | + * |
| 28 | + * @var PHPUnit_Extensions_MockFunction |
| 29 | + */ |
| 30 | + protected $test_function_name; |
| 31 | + |
| 32 | + /** |
| 33 | + * Setting up. |
| 34 | + */ |
| 35 | + protected function setUp() |
| 36 | + { |
| 37 | + $this->test_scope_object = new TestScopeObject(); |
| 38 | + } |
| 39 | + |
| 40 | + protected function onNotSuccessfulTest( Exception $e ) |
| 41 | + { |
| 42 | + var_dump( $e->getMessage() ); |
| 43 | + var_dump( $e->getTraceAsString() ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Test simple function return faking without consraints. |
| 48 | + */ |
| 49 | + public function testMockWithReturn() |
| 50 | + { |
| 51 | + $this->test_function_name = self::getFunctionName( 'time' ); |
| 52 | + $this->object = new PHPUnit_Extensions_MockFunction( $this->test_function_name, $this->test_scope_object ); |
| 53 | + |
| 54 | + // Back to the future: |
| 55 | + $einsteins_clock = time() + 60; |
| 56 | + $this->object->expects( $this->atLeastOnce() )->will( $this->returnValue( $einsteins_clock ) ); |
| 57 | + |
| 58 | + // Einstein's clock is exactly one minute behind mine. |
| 59 | + $this->assertSame( $einsteins_clock, $this->test_scope_object->callFunction( $this->test_function_name, array() ) ); |
| 60 | + |
| 61 | + $this->object->restore(); |
| 62 | + |
| 63 | + // We are back in 1985. |
| 64 | + $this->assertSame( time(), $this->test_scope_object->callFunction( $this->test_function_name, array() ) ); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test more advanced mocking with return callback and constraints. |
| 69 | + */ |
| 70 | + public function testMockWithOriginal() |
| 71 | + { |
| 72 | + $this->test_function_name = self::getFunctionName( 'strrev' ); |
| 73 | + $this->object = new PHPUnit_Extensions_MockFunction( $this->test_function_name, $this->test_scope_object ); |
| 74 | + |
| 75 | + // Return normally, only checks the call. |
| 76 | + $this->object->expects( $this->once() )->with( $this->equalTo( 'abc' ) )->will( $this->returnCallback( 'strrev' ) ); |
| 77 | + |
| 78 | + // The same output is returned. |
| 79 | + $this->assertSame( 'cba', $this->test_scope_object->callFunction( $this->test_function_name, array( 'abc' ) ) ); |
| 80 | + |
| 81 | + $this->object->restore(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Testing newly created function. |
| 86 | + */ |
| 87 | + public function testMockNewFunction() |
| 88 | + { |
| 89 | + $this->test_function_name = 'new_random_function_' . uniqid(); |
| 90 | + $this->object = new PHPUnit_Extensions_MockFunction( $this->test_function_name, $this->test_scope_object ); |
| 91 | + |
| 92 | + // Return normally, only checks the call. |
| 93 | + $this->object->expects( $this->any() )->will( $this->returnValue( 'OK' ) ); |
| 94 | + |
| 95 | + $this->assertSame( 'OK', $this->test_scope_object->callFunction( $this->test_function_name, array() ) ); |
| 96 | + |
| 97 | + $this->object->restore(); |
| 98 | + } |
| 99 | + |
| 100 | + protected static function getFunctionName( $function_name ) |
| 101 | + { |
| 102 | + // Memoization for config value. |
| 103 | + static $internal_override_on; |
| 104 | + |
| 105 | + if ( !isset( $internal_override_on ) ) |
| 106 | + { |
| 107 | + $internal_override_on = (bool) ini_get( 'runkit.internal_override' ); |
| 108 | + } |
| 109 | + |
| 110 | + if ( $internal_override_on ) |
| 111 | + { |
| 112 | + return $function_name; |
| 113 | + } |
| 114 | + |
| 115 | + $proxy_function_name = 'proxy_to_' . $function_name . '_' . uniqid(); |
| 116 | + |
| 117 | + eval( <<<PROXY |
| 118 | + function $proxy_function_name() |
| 119 | + { |
| 120 | + \$arguments = func_get_args(); |
| 121 | + return call_user_func_array( '$function_name', \$arguments ); |
| 122 | + } |
| 123 | +PROXY |
| 124 | + ); |
| 125 | + |
| 126 | + return $proxy_function_name; |
| 127 | + } |
| 128 | + |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Class to be used as scope object for mocked function calls. |
| 133 | + */ |
| 134 | +class TestScopeObject |
| 135 | +{ |
| 136 | + /** |
| 137 | + * Simply calls a PHP callback with the passed parameters. |
| 138 | + * |
| 139 | + * @throws InvalidArgumentException In case $callback is not callable. |
| 140 | + * @param callback $callback |
| 141 | + * @param array $params |
| 142 | + * @return mixed The result of the callback execution. |
| 143 | + */ |
| 144 | + public function callFunction( $callback, array $params ) |
| 145 | + { |
| 146 | + if ( !is_callable( $callback ) ) |
| 147 | + { |
| 148 | + throw new InvalidArgumentException( 'Invalid callback at parameter 1st' ); |
| 149 | + } |
| 150 | + return call_user_func_array( $callback, $params ); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +?> |
0 commit comments