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: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122:
<?php
namespace Nethgui\Test\Tool;
class MockFactory
{
public static function getMockDatabase(\PHPUnit_Framework_TestCase $testcase, \Nethgui\Test\Tool\DB $db)
{
$databaseMethods = array(
'setProp' => TRUE,
'delProp' => TRUE,
'deleteKey' => TRUE,
'setKey' => TRUE,
'setType' => TRUE,
'getAll' => FALSE,
'getKey' => FALSE,
'getProp' => FALSE,
'getType' => FALSE,
);
$dbMock = $testcase->getMockBuilder('Nethgui\System\EsmithDatabase')
->disableOriginalConstructor()
->setMethods(array_keys($databaseMethods))
->getMock();
$methodStub = new MockObject($db);
foreach (array_keys($databaseMethods) as $method) {
$dbMock
->expects($testcase->any())
->method($method)
->will($methodStub);
}
return $dbMock;
}
public static function getAuthenticationSubject(\PHPUnit_Framework_TestCase $testcase, $username = FALSE, $groups = array())
{
$subject = $testcase->getMock('Nethgui\Authorization\User', array('authenticate', 'isAuthenticated', 'getCredential', 'hasCredential', 'getLanguageCode', 'asAuthorizationString', 'getAuthorizationAttribute'));
$subject->expects($testcase->any())
->method('isAuthenticated')
->will($testcase->returnValue(is_string($username)));
$subject->expects($testcase->any())
->method('getCredential')
->with('username')
->will($testcase->returnValue(is_string($username) ? $username : NULL));
$subject->expects($testcase->any())
->method('hasCredential')
->with('username')
->will($testcase->returnValue(is_string($username)));
$getAttribute = function($attName) use ($username, $groups) {
if ($attName === 'username') {
return is_string($username) ? $username : NULL;
} elseif ($attName === 'authenticated') {
return is_string($username) ? TRUE : FALSE;
} elseif ($attName == 'groups') {
return $groups;
}
return NULL;
};
$subject->expects($testcase->any())
->method('getAuthorizationAttribute')
->withAnyParameters()
->will($testcase->returnCallback($getAttribute));
$subject->expects($testcase->any())
->method('asAuthorizationString')
->will($testcase->returnValue(is_string($username) ? $username : 'Anonymous'));
$subject->hasCredential('username');
$subject->getCredential('username');
$subject->isAuthenticated();
$subject->getAuthorizationAttribute('username');
$subject->asAuthorizationString();
return $subject;
}
}