Skip to content

Instantly share code, notes, and snippets.

@bogdananton
Last active August 29, 2015 14:09
Show Gist options
  • Save bogdananton/ce128f0389ea57d2d3d3 to your computer and use it in GitHub Desktop.
Save bogdananton/ce128f0389ea57d2d3d3 to your computer and use it in GitHub Desktop.
test static methods 1
<?php
class Signup
{
protected static function getSellerInstance($IdSeller = 0)
{
return '-' . $IdSeller . '-';
}
protected static function getCountryDetails($CountryName)
{
return '=' . $CountryName . '=';
}
public function createMerchant($RemoteMerchantObj, $IdSeller, $CountryName){
$CSeller = static::getSellerInstance($IdSeller);
$CountryDetails = static::getCountryDetails($CountryName);
return array(
$CSeller, $CountryDetails, $RemoteMerchantObj
);
}
}
class Signup_createMerchant extends PHPUnit_Framework_TestCase {
public function testNoMocks()
{
$Signup = new Signup;
$expected = array(
'-1000-',
'=RO=',
'RemoteMerchantObj'
);
$return = $Signup->createMerchant('RemoteMerchantObj', 1000, 'RO');
$this->assertEquals($expected, $return);
}
public function testWithExtends()
{
$Signup = new Signup_underTest_createMerchant;
$expected = array(
'*1000*',
'xROx',
'RemoteMerchantObj'
);
$return = $Signup->createMerchant('RemoteMerchantObj', 1000, 'RO');
$this->assertEquals($expected, $return);
}
public function testWithMonkeyPatching()
{
runkit_method_rename('Signup', 'getSellerInstance', 'getSellerInstance_BACKUP');
runkit_method_rename('Signup', 'getCountryDetails', 'getCountryDetails_BACKUP');
$mock = $this->getMock('Signup', array('getSellerInstance', 'getCountryDetails'));
$mock->expects($this->any())
->method('getSellerInstance')
->will($this->returnCallback(function($SellerId){
return '-->' . $SellerId . '<--';
}));
$mock->expects($this->any())
->method('getCountryDetails')
->will($this->returnCallback(function($CountryCode){
return '--|' . $CountryCode . '|--';
}));
$expected = array(
'-->1000<--',
'--|RO|--',
'RemoteMerchantObj'
);
Signup_underTest_createMerchant_withMocks::$mock = $mock;
$classUnderTest = new Signup_underTest_createMerchant_withMocks;
$return = $classUnderTest->createMerchant('RemoteMerchantObj', 1000, 'RO');
// revert
runkit_method_rename('Signup', 'getSellerInstance_BACKUP', 'getSellerInstance');
runkit_method_rename('Signup', 'getCountryDetails_BACKUP', 'getCountryDetails');
$this->assertEquals($expected, $return);
}
}
class Signup_underTest_createMerchant extends Signup
{
protected static function getSellerInstance($IdSeller = 0)
{
return '*' . $IdSeller . '*';
}
protected static function getCountryDetails($CountryName)
{
return 'x' . $CountryName . 'x';
}
}
class Signup_underTest_createMerchant_withMocks extends Signup
{
public static $mock;
public static function __callStatic($method, $args)
{
$mock = self::$mock;
return call_user_func_array(array(&$mock, $method), $args);
}
public static function __call($method, $args)
{
$mock = self::$mock;
return call_user_func_array(array(&$mock, $method), $args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment