📜 ⬆️ ⬇️

Runkit + PHPUnit = 100% test coverage

Dear colleagues.

One of the indirect indicators of code quality is considered code coverage - the degree of coverage of its tests (as a rule, we mean unit tests). In most cases, coverage is taken as the ratio of the number of lines of code that gets control during the test run to the total number of significant (non-commentary, empty line, or, for example, one curly brace, indicating the beginning or end of the block) lines of module code.

Another condition for good tests is the absence of side effects, such as creating / deleting files, setting up network connections, writing to ports, etc.
')
However, when it comes to the module that interacts with the outside world, these two requirements conflict. And well, if we are talking about file operations, when vfsStream comes to the rescue . But what to do when you need to test, say, direct work with sockets or code that uses curl_ * functions?

Under the cut, you will find my solution and, as a bonus, another OPP wrapper to the Kurlu, fully covered with tests.


You can use Runkit to write unit tests for such code. I wrote the PHPUnit extension TestCase, which with the help of the rankit allows you to replace the built-in functions using the full power of asserts and mocks from Sebastian Bergman. The heir is so simple that I will allow myself to bring his code here in its entirety.

<?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 .
  1. <?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 .
  2. <?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 .
  3. <?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 .
  4. <?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 .
  5. <?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 .
  6. <?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 .
  7. <?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 .
  8. <?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 .
  9. <?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 .
  10. <?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 .
  11. <?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 .
  12. <?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 .
  13. <?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 .
  14. <?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 .
  15. <?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 .
  16. <?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 .
  17. <?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 .
  18. <?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 .
  19. <?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 .
  20. <?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 .
  21. <?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 .
  22. <?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 .
  23. <?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 .
  24. <?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 .
  25. <?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 .
  26. <?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 .
  27. <?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 .
  28. <?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 .
  29. <?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 .
  30. <?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 .
  31. <?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 .
  32. <?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 .
  33. <?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 .
  34. <?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 .
  35. <?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 .
  36. <?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 .
  37. <?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 .
  38. <?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 .
  39. <?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 .
  40. <?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 .
  41. <?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 .
  42. <?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 .
  43. <?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 .
  44. <?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 .
  45. <?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 .
  46. <?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 .
  47. <?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 .
  48. <?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 .
  49. <?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 .
  50. <?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 .
  51. <?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 .
  52. <?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 .
  53. <?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 .
  54. <?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 .
  55. <?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 .
  56. <?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 .
  57. <?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 .
  58. <?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 .
  59. <?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 .
  60. <?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 .
  61. <?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 .
  62. <?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 .
  63. <?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 .
  64. <?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 .
  65. <?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 .
  66. <?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 .
  67. <?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 .
  68. <?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 .
  69. <?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 .
  70. <?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 .
  71. <?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 .
  72. <?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 .
  73. <?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 .
  74. <?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 .
  75. <?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 .
  76. <?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 .
  77. <?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 .
  78. <?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 .
  79. <?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 .
  80. <?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 .
  81. <?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 .
  82. <?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 .
  83. <?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 .
  84. <?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 .
  85. <?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 .
  86. <?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 .
  87. <?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 .
  88. <?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 .
  89. <?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 .
  90. <?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 .
  91. <?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 .
  92. <?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 .
  93. <?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 .
  94. <?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 .
  95. <?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 .
  96. <?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 .
  97. <?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 .
  98. <?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 .
  99. <?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 .
  100. <?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 .
  101. <?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 .
  102. <?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 .
  103. <?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 .
  104. <?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 .
  105. <?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 .
  106. <?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 .
  107. <?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 .
  108. <?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 .
  109. <?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 .
  110. <?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 .
  111. <?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 .
  112. <?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 .
  113. <?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 .
  114. <?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 .
  115. <?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 .
  116. <?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 .
  117. <?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 .
  118. <?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 .
  119. <?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 .
  120. <?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 .
  121. <?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 .
  122. <?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 .
  123. <?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 .
  124. <?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 .
  125. <?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 .
  126. <?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 .
  127. <?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 .
  128. <?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 .
  129. <?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 .
  130. <?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 .
  131. <?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 .
  132. <?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 .
  133. <?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 .
  134. <?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 .
  135. <?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 .
  136. <?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 .
  137. <?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 .
  138. <?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 .
  139. <?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 .
  140. <?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 .
  141. <?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 .
  142. <?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 .
  143. <?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 .
  144. <?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 .
  145. <?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 .
  146. <?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 .
  147. <?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 .
  148. <?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 .
  149. <?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 .
  150. <?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 .
  151. <?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 .
  152. <?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 .


The simplest example of using attentive readers have already noticed in the class dock, I will gladly give explanations if they are needed, in the comments to the topic. A slightly more complicated use case, testing the transfer of parameters by reference, you will find in the tests of the OOP wrapper I promised me to the Kurlu .

Thank you, I will be grateful for the criticism.

Source: https://habr.com/ru/post/124915/


All Articles