secret
Last active

  • Download Gist
instanceMock.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
<?php
 
class safeBlaine {
 
public function foo($item = null) {
$this->protectedFoo('foo', 'bar', 'baz');
}
 
protected function protectedFoo() {
echo 'dont get here!';
exit;
}
}
 
class publicSafeBlaine extends safeBlaine {
 
public $parent;
 
public function __construct() {
$args = func_get_args();
$this->parent = array_shift($args);
call_user_func_array('parent::__construct', $args);
}
 
public function foo($item = null) {
$args = func_get_args();
$count = count($args);
$method = 'foo';
if ($count < 1 || $args[$count - 2] !== spl_object_hash($this)) {
return call_user_func_array(array($this->parent, $method), $args);
}
return call_user_func_array('parent::' . $method, $args);
}
public function protectedFoo($name = 'baz') {
$args = func_get_args();
$count = count($args);
$method = 'protectedFoo';
if ($count < 1 || $args[$count - 2] !== spl_object_hash($this)) {
return call_user_func_array(array($this->parent, $method), $args);
}
return call_user_func_array('parent::' . $method, $args);
}
}
 
class mockSafeBlaine extends publicSafeBlaine {
 
protected $_mocker;
 
public function __construct() {
$args = func_get_args();
array_unshift($args, $this);
$class = new ReflectionClass('publicSafeBlaine');
$this->_mocker = $class->newInstanceArgs($args);
}
 
public function foo($item = null) {
$args = func_get_args();
$args[] = spl_object_hash($this->_mocker);
return call_user_func_array(array($this->_mocker, 'foo'), $args);
}
 
public function protectedFoo($name = 'baz') {
echo 'get here';
exit;
}
 
}
 
$foo = new mockSafeBlaine('nikkie');
 
print_r($foo->foo('baz'));
staticMock.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
<?php
 
class staticBlaine {
 
public static function foo($item = null) {
static::protectedFoo();
}
 
protected static function protectedFoo() {
echo 'dont get here!';
exit;
}
}
 
class publicStaticBlaine extends staticBlaine {
 
public static function foo($item = null) {
$args = func_get_args();
$count = count($args);
$method = 'foo';
if ($count < 1 || $args[$count - 1] !== '1f3870be274f6c49b3e31a0c6728957f') {
return call_user_func_array('mockSafeBlaine::' . $method, $args);
}
return call_user_func_array('parent::' . $method, $args);
}
public static function protectedFoo($name = 'baz') {
$args = func_get_args();
$count = count($args);
$method = 'protectedFoo';
if ($count < 1 || $args[$count - 1] !== '1f3870be274f6c49b3e31a0c6728957f') {
return call_user_func_array('mockSafeBlaine::' . $method, $args);
}
return call_user_func_array('parent::' . $method, $args);
}
}
 
class mockSafeBlaine extends publicStaticBlaine {
 
protected $_mocker;
 
public static function foo($item = null) {
$args = func_get_args();
$args[] = '1f3870be274f6c49b3e31a0c6728957f';
return call_user_func_array(array('publicStaticBlaine', 'foo'), $args);
}
 
public static function protectedFoo($name = 'baz') {
echo 'get here';
exit;
}
 
}
 
$foo = new mockSafeBlaine('nikkie');
 
print_r($foo->foo('baz'));

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.