рдирдорд╕реНрдХрд╛рд░ рдкреНрд░рд┐рдп рд╕рд╛рдерд┐рдпреЛрдВред
рдХреЛрдб рдЧреБрдгрд╡рддреНрддрд╛ рдХреЗ рдЕрдкреНрд░рддреНрдпрдХреНрд╖ рд╕рдВрдХреЗрддрдХреЛрдВ рдореЗрдВ рд╕реЗ рдПрдХ рдХреЛрдб рдХрд╡рд░реЗрдЬ рдорд╛рдирд╛ рдЬрд╛рддрд╛ рд╣реИ - рдкрд░реАрдХреНрд╖рдгреЛрдВ рджреНрд╡рд╛рд░рд╛ рдХрд╡рд░реЗрдЬ рдХреА рдбрд┐рдЧреНрд░реА (рдПрдХ рдирд┐рдпрдо рдХреЗ рд░реВрдк рдореЗрдВ, рдпрд╣ рдЗрдХрд╛рдИ рдкрд░реАрдХреНрд╖рдгреЛрдВ рдХреЛ рд╕рдВрджрд░реНрднрд┐рдд рдХрд░рддрд╛ рд╣реИ)ред рдЬреНрдпрд╛рджрд╛рддрд░ рдорд╛рдорд▓реЛрдВ рдореЗрдВ, рдХрд╡рд░реЗрдЬ рдХреЛрдб рдХреА рдкрдВрдХреНрддрд┐рдпреЛрдВ рдХреА рд╕рдВрдЦреНрдпрд╛ рдХрд╛ рдЕрдиреБрдкрд╛рдд рд╣реЛрддрд╛ рд╣реИ, рдЬрд┐рд╕рдореЗрдВ рдкрд░реАрдХреНрд╖рдг рд░рди рдХреЗ рджреМрд░рд╛рди рдорд╣рддреНрд╡рдкреВрд░реНрдг рдХреА рдХреБрд▓ рд╕рдВрдЦреНрдпрд╛ (рдХреЛрдИ рдЯрд┐рдкреНрдкрдгреА рдирд╣реАрдВ, рдЦрд╛рд▓реА рд▓рд╛рдЗрди, рдпрд╛, рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП, рдПрдХ рдШреБрдВрдШрд░рд╛рд▓реЗ рдмреНрд░реЗрд╕) рдмреНрд▓реЙрдХ рдореЙрдбреНрдпреВрд▓ рдХреА рдкрдВрдХреНрддрд┐рдпреЛрдВ рдХреА рд╢реБрд░реБрдЖрдд рдпрд╛ рдЕрдВрдд рдХреЛ рдЗрдВрдЧрд┐рдд рдХрд░рддрд╛ рд╣реИред
рдЕрдЪреНрдЫреЗ рдкрд░реАрдХреНрд╖рдгреЛрдВ рдХреЗ рд▓рд┐рдП рдПрдХ рдЕрдиреНрдп рд╢рд░реНрдд рд╕рд╛рдЗрдб рдЗрдлреЗрдХреНрдЯреНрд╕ рдХреА рдЕрдиреБрдкрд╕реНрдерд┐рддрд┐ рд╣реИ, рдЬреИрд╕реЗ рдлрд╛рдЗрд▓реЗрдВ рдмрдирд╛рдирд╛ / рд╣рдЯрд╛рдирд╛, рдиреЗрдЯрд╡рд░реНрдХ рдХрдиреЗрдХреНрд╢рди рд╕реНрдерд╛рдкрд┐рдд рдХрд░рдирд╛, рдмрдВрджрд░рдЧрд╛рд╣реЛрдВ рдХреЛ рд▓рд┐рдЦрдирд╛ рдЖрджрд┐ред
рд╣рд╛рд▓рд╛рдВрдХрд┐, рдЬрдм рдпрд╣ рдПрдХ рдореЙрдбреНрдпреВрд▓ рдХреА рдмрд╛рдд рдЖрддреА рд╣реИ рдЬреЛ рдмрд╛рд╣рд░реА рджреБрдирд┐рдпрд╛ рдХреЗ рд╕рд╛рде рдмрд╛рддрдЪреАрдд рдХрд░рддрд╛ рд╣реИ, рддреЛ рдпреЗ рджреЛ рдЖрд╡рд╢реНрдпрдХрддрд╛рдПрдВ рдЯрдХрд░рд╛рддреА рд╣реИрдВред рдФрд░ рдареАрдХ рд╣реИ, рдЬрдм рдлрд╝рд╛рдЗрд▓ рд╕рдВрдЪрд╛рд▓рди рдХреА рдмрд╛рдд рдЖрддреА рд╣реИ, рдЬрдм
vfsStream рдмрдЪрд╛рд╡ рдореЗрдВ рдЖрддрд╛ рд╣реИ ред рд▓реЗрдХрд┐рди рдЬрдм рдЖрдкрдХреЛ рдХрд░реНрд▓_ рдлрд╝рдВрдХреНрд╢рдВрд╕ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рд╡рд╛рд▓реЗ рд╕реЙрдХреЗрдЯреНрд╕ рдпрд╛ рдХреЛрдб рдХреЗ рд╕рд╛рде рд╕реАрдзреЗ рдХрд╛рдо рдХрд░рдиреЗ рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реЛ, рддреЛ рдХреНрдпрд╛ рдХрд░реЗрдВ?
рдХрдЯреМрддреА рдХреЗ рддрд╣рдд рдЖрдкрдХреЛ рдореЗрд░рд╛ рд╕рдорд╛рдзрд╛рди рдорд┐рд▓реЗрдЧрд╛ рдФрд░, рдПрдХ рдмреЛрдирд╕ рдХреЗ рд░реВрдк рдореЗрдВ, рдХрд░реНрд▓ рдХреЗ рд▓рд┐рдП
рдПрдХ рдФрд░ рдУрдкреАрдкреА рдЖрд╡рд░рдг, рдкреВрд░реА рддрд░рд╣ рд╕реЗ рдкрд░реАрдХреНрд╖рдгреЛрдВ рд╕реЗ рдврдВрдХрд╛ рд╣реБрдЖ рд╣реИред
рдЖрдк рдРрд╕реЗ рдХреЛрдб рдХреЗ рд▓рд┐рдП рдпреВрдирд┐рдЯ рдЯреЗрд╕реНрдЯ рд▓рд┐рдЦрдиреЗ рдХреЗ рд▓рд┐рдП
рд░рдирдХрд┐рдЯ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВред рдореИрдВрдиреЗ PHPUnit рдХреЗ TestCase рдХрд╛ рд╡рд┐рд╕реНрддрд╛рд░ рд▓рд┐рдЦрд╛ рд╣реИ, рдЬреЛ рдХрд┐ рд░реИрдВрдХрд┐рдЯ рдХреА рдорджрдж рд╕реЗ, рдЖрдк рд╕реЗрдмрд╛рд╕реНрдЯрд┐рдпрди рдмрд░реНрдЧрдореИрди рд╕реЗ рдкреВрд░реА рддрд╛рдХрдд рдФрд░ рдореЛрдХреНрд╕ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдХреЗ рдЕрдВрддрд░реНрдирд┐рд╣рд┐рдд рдХрд╛рд░реНрдпреЛрдВ рдХреЛ рдмрджрд▓рдиреЗ рдХреА рдЕрдиреБрдорддрд┐ рджреЗрддрд╛ рд╣реИред рд╡рд╛рд░рд┐рд╕ рдЗрддрдирд╛ рд╕рд░рд▓ рд╣реИ рдХрд┐ рдореИрдВ рдЕрдкрдиреЗ рдЖрдк рдХреЛ рдЙрд╕рдХреЗ рдХреЛрдб рдХреЛ рдЙрд╕рдХреА рд╕рдВрдкреВрд░реНрдгрддрд╛ рдореЗрдВ рд▓рд╛рдиреЗ рдХреА рдЕрдиреБрдорддрд┐ рджреЗрддрд╛ рд╣реВрдВред
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
<?php namespace Base\UnitTest; /** * Test case which enables overriding of functions with runkit. * * 1. To override a number of system function do * * $mock = $this->runkitMockFunctions(array(...)); * * 2. To define expected behaivour use $mock as an ordinary phpunit mock object. * * 3. To revert overridden functions back call $this->runkitRevertAll(); * * Example: * * class MyCurlTest * extends \Base\UnitTest\RunkitTestCase * { * protected $mock; * * protected function setUp() * { * $this->mock = $this->runkitMockFunctions(array( * 'curl_init', * 'curl_close', * )); * } * * protected function tearDown() * { * $this->runkitRevertAll(); * } * * public function testInitClose() * { * $this->mock * ->expects($this->at(0)) * ->method('curl_init') * ->with() * ->will($this->returnValue('my_handle')); * * $this->mock * ->expects($this->at(1)) * ->method('curl_close') * ->with('my_handle'); * * $handle = curl_init(); * $this->assertEquals('my_handle', $handle); * curl_close($handle); * } * } * * @package Base\UnitTest * @version $id$ * @author Alexey Karapetov <karapetov@gmail.com> */ abstract class RunkitTestCase extends \PHPUnit_Framework_TestCase { private static $mockedFunctions = array(); const BACKUP_SUFFIX = '_runkit_mocker_backup' ; /** * Method to call from overridden functions. * Calls given mock's method with given arguments. * * @param string $method Mock's method to call * @param array $args Arguments to pass to @link $method * @return void */ public static function call($func, array $args) { return call_user_func_array(array(self::$mockedFunctions[$func], $func), $args); } /** * Mark test skipped if runkit is not enabled * * @return void */ protected function skipTestIfNoRunkit() { if (!extension_loaded( 'runkit' )) { $ this ->markTestSkipped( 'Runkit extension is not loaded' ); } } /** * Override given functions with mock * * @param array $funcList Functions to override * @return stdClass Mock object */ protected function runkitMockFunctions(array $funcList) { $ this ->skipTestIfNoRunkit(); $mock = $ this ->getMock( 'stdClass' , $funcList); foreach ($funcList as $func) { $ this ->runkitOverride($func, '' , 'return ' . __CLASS__ . "::call('{$func}', func_get_args());" , $mock); } return $mock; } /** * Override function * * @param string $func * @param string $args * @param string $body * @param mixed $mock Mock object for the function * @return void */ protected function runkitOverride($func, $args, $body, $mock = null ) { $ this ->skipTestIfNoRunkit(); if (array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is marked as mocked already" ); } self::$mockedFunctions[$func] = $mock; \runkit_function_copy($func, $func . self::BACKUP_SUFFIX); \runkit_function_redefine($func, $args, $body); } /** * Revert previously overridden function * * @param string $func * @return void */ protected function runkitRevert($func) { $ this ->skipTestIfNoRunkit(); if (!array_key_exists($func, self::$mockedFunctions)) { throw new \RuntimeException( "Function '{$func}' is not marked as mocked" ); } unset(self::$mockedFunctions[$func]); \runkit_function_remove($func); \runkit_function_copy($func . self::BACKUP_SUFFIX, $func); \runkit_function_remove($func . self::BACKUP_SUFFIX); } /** * Revert all previously overridden functions * * @return void */ protected function runkitRevertAll() { foreach (array_keys(self::$mockedFunctions) as $func) { $ this ->runkitRevert($func); } } } * This source code was highlighted with Source Code Highlighter .
рдЪреМрдХрд╕ рдкрд╛рдардХреЛрдВ рдиреЗ рдХрдХреНрд╖рд╛ рдХреЗ рдбреЙрдХрд┐рдВрдЧ рдмреНрд▓реЙрдХ рдореЗрдВ рдЙрдкрдпреЛрдЧ рдХреЗ рд╕рдмрд╕реЗ рд╕рд░рд▓ рдЙрджрд╛рд╣рд░рдг рдХреЛ рдкрд╣рд▓реЗ рд╣реА рджреЗрдЦрд╛ рд╣реИ, рдореИрдВ рдЦрд╝реБрд╢реА рд╕реЗ рд╕реНрдкрд╖реНрдЯреАрдХрд░рдг рджреВрдВрдЧрд╛, рдпрджрд┐ рдЙрдирдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реИ, рддреЛ рд╡рд┐рд╖рдп рдХреЗ рд▓рд┐рдП рдЯрд┐рдкреНрдкрдгреА рдореЗрдВред рдереЛрдбрд╝рд╛ рдФрд░ рдЕрдзрд┐рдХ рдЬрдЯрд┐рд▓ рдЙрдкрдпреЛрдЧ рдорд╛рдорд▓рд╛, рд╕рдВрджрд░реНрдн рджреНрд╡рд╛рд░рд╛ рдорд╛рдкрджрдВрдбреЛрдВ рдХреЗ рд╣рд╕реНрддрд╛рдВрддрд░рдг рдХрд╛ рдкрд░реАрдХреНрд╖рдг, рдЖрдк
рдХреБрд░реНрд▓ рдХреЛ рдУрдУрдкреА рдЖрд╡рд░рдг рдХреЗ рдкрд░реАрдХреНрд╖рдгреЛрдВ рдореЗрдВ рдкрд╛рдПрдВрдЧреЗ
рдЬреЛ рдореИрдВрдиреЗ рд╡рд╛рджрд╛ рдХрд┐рдпрд╛ рдерд╛ ред
рдзрдиреНрдпрд╡рд╛рдж, рдореИрдВ рдЖрд▓реЛрдЪрдирд╛ рдХреЗ рд▓рд┐рдП рдЖрднрд╛рд░реА рд░рд╣реВрдВрдЧрд╛ред