コード品質の間接的な指標の1つは、コードカバレッジと見なされます-テストによるカバレッジの程度(原則として、これは単体テストを指します)。 ほとんどの場合、カバレッジは、テスト実行中に制御が入るコードの行数の、モジュールコードの重要な(コメント、空の行、またはブロックの開始または終了を示す中括弧ではない)行の総数に対する比率として取得されます。
適切なテストの別の条件は、ファイルの作成/削除、ネットワーク接続の確立、ポートへの書き込みなどの副作用がないことです。
ただし、外の世界と相互作用するモジュールになると、これら2つの要件は競合します。 ファイル操作に関しては、 vfsStream が 助けになります。 しかし、たとえばcurl_ *関数を使用するソケットまたはコードで直接作業をテストする必要がある場合はどうすればよいでしょうか?
カットの下に私の解決策があり、ボーナスとして、カール用の別の OPPラッパーがあり、テストで完全にカバーされています。
Runkitを使用して、このようなコードの単体テストを作成できます。 PHPUnitのTestCaseの拡張機能を作成しました。これは、rankitの助けを借りて、Sebastian Bergmanのアサートとモックの全機能を使用して組み込み関数を置き換えることができます。 相続人はとても単純なので、彼のコードをここに完全に持ち込むことができます。
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
気配りのある読者は、クラスのドッキングブロックでの最も簡単な使用例に既に気付いています。必要であれば、トピックへのコメントで喜んで説明します。 参照によってパラメーターの転送をテストする、もう少し複雑なユースケースは、 私が約束したKurlへのOOPラッパーのテストにあります 。
ありがとう、私は批判に感謝します。