本文整理汇总了PHP中PHPUnit_Framework_TestCase::returnCallback方法的典型用法代码示例。如果您正苦于以下问题:PHP PHPUnit_Framework_TestCase::returnCallback方法的具体用法?PHP PHPUnit_Framework_TestCase::returnCallback怎么用?PHP PHPUnit_Framework_TestCase::returnCallback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PHPUnit_Framework_TestCase
的用法示例。
在下文中一共展示了PHPUnit_Framework_TestCase::returnCallback方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: mockLocalDriver
function mockLocalDriver(PHPUnit_Framework_TestCase $testcase, $root)
{
$driver = $testcase->getMockBuilder(LocalDriver::class)->setConstructorArgs([$root])->setMethods(['instantiateFile', 'instantiateDir'])->getMockForAbstractClass();
$driver->method('instantiateFile')->will($testcase->returnCallback(function ($path) use($driver) {
return new LocalFile($driver, $path);
}));
$driver->method('instantiateDir')->will($testcase->returnCallback(function ($path) use($driver) {
return new LocalDirectory($driver, $path);
}));
return $driver;
}
示例2: getAuthenticationSubject
/**
*
* @param string $username
* @return \Nethgui\Authorization\UserInterface
*/
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;
}
示例3: buildMethod
/**
* @param string $methodName
*/
private function buildMethod($methodName)
{
$self = $this;
$callBackFunction = function () use($self, $methodName) {
$inputParams = func_get_args();
$return = $self->getReturn($methodName);
$returnValue = $return['value'];
$returnType = $return['type'];
$self->addInvocation($methodName, $inputParams);
switch ($returnType) {
case 'exception':
throw $returnValue;
break;
case 'value':
return $returnValue;
break;
case 'self':
return $self->getMock();
break;
case 'argument':
return isset($inputParams[$returnValue]) ? $inputParams[$returnValue] : null;
break;
case 'callback':
$inputParams[] = count($self->getInvocations($methodName));
return call_user_func_array($returnValue, $inputParams);
break;
default:
throw new Exception("unknown method return type {$returnType}");
break;
}
};
$method = $this->mock->expects($this->testCase->any());
$method->method($methodName);
$method->will($this->testCase->returnCallback($callBackFunction));
}
示例4: getBuilder
/**
* Get data object builder
*
* @param string $className
* @param array $arguments
* @return object
*/
protected function getBuilder($className, array $arguments)
{
$objectFactory = $this->_testObject->getMock('Magento\\Framework\\Api\\ObjectFactory', [], [], '', false);
if (!isset($arguments['objectFactory'])) {
$arguments['objectFactory'] = $objectFactory;
}
$constructArguments = $this->getConstructArguments($className, $arguments);
$reflectionClass = new \ReflectionClass($className);
$builderObject = $reflectionClass->newInstanceArgs($constructArguments);
$objectFactory->expects($this->_testObject->any())->method('populateWithArray')->will($this->_testObject->returnSelf());
$objectFactory->expects($this->_testObject->any())->method('populate')->will($this->_testObject->returnSelf());
$objectFactory->expects($this->_testObject->any())->method('create')->will($this->_testObject->returnCallback(function ($className, $arguments) {
$reflectionClass = new \ReflectionClass($className);
$constructorMethod = $reflectionClass->getConstructor();
$parameters = $constructorMethod->getParameters();
$args = [];
foreach ($parameters as $parameter) {
$parameterName = $parameter->getName();
if (isset($arguments[$parameterName])) {
$args[] = $arguments[$parameterName];
} else {
if ($parameter->isArray()) {
$args[] = [];
} elseif ($parameter->allowsNull()) {
$args[] = null;
} else {
$mock = $this->_getMockWithoutConstructorCall($parameter->getClass()->getName());
$args[] = $mock;
}
}
}
return $reflectionClass->newInstanceArgs($args);
}));
return $builderObject;
}
示例5: mockFileWriter
function mockFileWriter(PHPUnit_Framework_TestCase $testcase, File $file)
{
$in = $testcase->getMockForAbstractClass(FileWriter::class, [$file]);
$in->method('write')->will($testcase->returnCallback(function ($data) {
return strlen($data);
}));
return $in;
}
示例6: mockFileOutputStream
function mockFileOutputStream(PHPUnit_Framework_TestCase $testcase, File $file)
{
$in = $testcase->getMockForAbstractClass('BapCat\\Interfaces\\Persist\\FileOutputStream', [$file]);
$in->method('write')->will($testcase->returnCallback(function ($data) {
return strlen($data);
}));
return $in;
}
示例7: getMysqli
public static function getMysqli(\PHPUnit_Framework_TestCase $testCase)
{
$mysqli = $testCase->getMockBuilder('\\mysqli')->disableOriginalConstructor()->getMock();
$mysqli->method('real_escape_string')->will($testCase->returnCallback(function ($v) {
return addcslashes($v, "\"\n\r\\'");
// not safe implementation!!!
}));
return $mysqli;
}
示例8: addMethodExpectation
protected function addMethodExpectation(\ReflectionClass $reflection, MockObject $mock, array $expectation)
{
if (is_null($expectation['will'])) {
return;
}
$expect = $reflection->hasMethod($expectation['method']) && $reflection->getMethod($expectation['method'])->isStatic() ? $mock::staticExpects($expectation['expects']) : $mock->expects($expectation['expects']);
$expect->method($expectation['method'])->will($expectation['will'] instanceof \Closure ? PhpUnitTestCase::returnCallback(version_compare(PHP_VERSION, '5.4.0', '>=') ? $expectation['will']->bindTo($mock, $reflection->getName()) : $expectation['will']) : $expectation['will']);
if (!is_null($expectation['with'])) {
call_user_func_array(array($expect, 'with'), $expectation['with']);
}
}
示例9: assignMockCallbacks
/**
* Assigns mock callbacks to methods.
*
* This method assumes that the mock callback is named {mock}{method name}.
*
* @param \PHPUnit_Framework_MockObject_MockObject $mockObject The mock object that the callbacks are being assigned to.
* @param \PHPUnit_Framework_TestCase $test The test.
* @param array $array An array of methods names to mock with callbacks.
*
* @return void
*
* @since 2.0
*/
public static function assignMockCallbacks(\PHPUnit_Framework_MockObject_MockObject $mockObject, \PHPUnit_Framework_TestCase $test, $array)
{
foreach ($array as $index => $method) {
if (is_array($method)) {
$methodName = $index;
$callback = $method;
} else {
$methodName = $method;
$callback = array(get_called_class(), 'mock' . $method);
}
$mockObject->expects($test->any())->method($methodName)->will($test->returnCallback($callback));
}
}
示例10: init
public function init()
{
$wikiaAppArgs = array();
$globalRegistryMock = null;
$functionWrapperMock = null;
$globalRegistryMock = $this->testCase->getMock('WikiaGlobalRegistry', array('get', 'set'));
$globalRegistryMock->expects($this->testCase->any())->method('get')->will($this->testCase->returnCallback(array($this, 'getGlobalCallback')));
if (in_array('runFunction', $this->methods)) {
$functionWrapperMock = $this->testCase->getMock('WikiaFunctionWrapper', array_keys($this->mockedFunctions));
foreach ($this->mockedFunctions as $functionName => $functionData) {
$functionWrapperMock->expects($this->testCase->exactly($functionData['calls']))->method($functionName)->will($this->testCase->returnValue($functionData['value']));
}
}
$wikiaAppArgs[] = $globalRegistryMock;
$wikiaAppArgs[] = null;
// WikiaLocalRegistry
$wikiaAppArgs[] = null;
// WikiaHookDispatcher
$wikiaAppArgs[] = $functionWrapperMock;
$this->mock = $this->testCase->getMock('WikiaApp', array('ajax'), $wikiaAppArgs, '');
F::setInstance('App', $this->mock);
}
示例11: getBuilder
/**
* Get data object builder
*
* @param string $className
* @param array $arguments
* @return object
*/
protected function getBuilder($className, array $arguments)
{
$objectFactory = $this->_testObject->getMock('Magento\\Framework\\Service\\Data\\ObjectFactory', [], [], '', false);
if (!isset($arguments['objectFactory'])) {
$arguments['objectFactory'] = $objectFactory;
}
$constructArguments = $this->getConstructArguments($className, $arguments);
$reflectionClass = new \ReflectionClass($className);
$builderObject = $reflectionClass->newInstanceArgs($constructArguments);
$objectFactory->expects($this->_testObject->any())->method('create')->will($this->_testObject->returnCallback(function ($className, $arguments) {
$reflectionClass = new \ReflectionClass($className);
return $reflectionClass->newInstanceArgs($arguments);
}));
return $builderObject;
}
示例12: _getTranslatorMock
/**
* Retrieve mock of core translator model
*
* @return Mage_Core_Model_Translate|PHPUnit_Framework_MockObject_MockObject
*/
protected function _getTranslatorMock()
{
$translator = $this->_testObject->getMockBuilder('Mage_Core_Model_Translate')->disableOriginalConstructor()->setMethods(array('translate'))->getMock();
$translateCallback = function ($arguments) {
$result = '';
if (is_array($arguments) && current($arguments) instanceof Mage_Core_Model_Translate_Expr) {
/** @var Mage_Core_Model_Translate_Expr $expression */
$expression = array_shift($arguments);
$result = vsprintf($expression->getText(), $arguments);
}
return $result;
};
$translator->expects($this->_testObject->any())->method('translate')->will($this->_testObject->returnCallback($translateCallback));
return $translator;
}
示例13: create
/**
* Creates and instance of the mock object.
*
* @param PHPUnit_Framework_TestCase $test A test object.
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
public static function create($test)
{
// Collect all the relevant methods in JDatabase.
$methods = array('getIssueId', 'setIssueId', 'getIssue', 'getIssueProject', 'getIssues');
// Create the mock.
$mockObject = $test->getMock('MonitorModelIssue', $methods, array(), '', false);
self::createSampleData();
$setterCallback = function ($id) {
self::$issueId = $id;
};
$mockObject->expects($test->any())->method('setIssueId')->will($test->returnCallback($setterCallback));
$mockObject->expects($test->any())->method('getIssueId')->will($test->returnValue(self::$issueId));
$mockObject->expects($test->any())->method('getIssue')->will($test->returnValue(self::$data[self::$issueId]));
$mockObject->expects($test->any())->method('getIssueProject')->will($test->returnValue(1));
$mockObject->expects($test->any())->method('getIssues')->will($test->returnValue(self::$data));
return $mockObject;
}
示例14: create
/**
* Creates an instance of the mock JMenu object.
*
* @param PHPUnit_Framework_TestCase $test A test object.
*
* @return PHPUnit_Framework_MockObject_MockObject
*
* @since 3.4
*/
public static function create(PHPUnit_Framework_TestCase $test, $setDefault = true, $setActive = false)
{
// Collect all the relevant methods in JMenu (work in progress).
$methods = array('getItem', 'setDefault', 'getDefault', 'setActive', 'getActive', 'getItems', 'getParams', 'getMenu', 'authorise', 'load');
// Create the mock.
$mockObject = $test->getMock('JMenu', $methods, array(), '', false);
self::createMenuSampleData();
$mockObject->expects($test->any())->method('getItem')->will($test->returnValueMap(self::prepareGetItemData()));
$mockObject->expects($test->any())->method('getItems')->will($test->returnCallback(array(__CLASS__, 'prepareGetItemsData')));
$mockObject->expects($test->any())->method('getMenu')->will($test->returnValue(self::$data));
if ($setDefault) {
$mockObject->expects($test->any())->method('getDefault')->will($test->returnValueMap(self::prepareDefaultData()));
}
if ($setActive) {
$mockObject->expects($test->any())->method('getActive')->will($test->returnValue(self::$data[$setActive]));
}
return $mockObject;
}
示例15: mockEnvelope
/**
* @param Identifies $id
* @param int $version
* @param Metadata|null $metadata
* @return \PHPUnit_Framework_MockObject_MockObject
*/
public function mockEnvelope(Identifies $id, $version, Metadata $metadata = null)
{
$class = 'SimpleES\\EventSourcing\\Event\\EnvelopsEvent';
$envelope = $this->testCase->getMock($class);
$envelope->expects($this->testCase->any())->method('eventId')->will($this->testCase->returnValue($this->mockIdentifier()));
$envelope->expects($this->testCase->any())->method('eventName')->will($this->testCase->returnValue('event_' . ($version + 1)));
$envelope->expects($this->testCase->any())->method('event')->will($this->testCase->returnValue($this->getEventStreamEventOne($id)));
$envelope->expects($this->testCase->any())->method('aggregateId')->will($this->testCase->returnValue($id));
$envelope->expects($this->testCase->any())->method('aggregateVersion')->will($this->testCase->returnValue($version));
$envelope->expects($this->testCase->any())->method('tookPlaceAt')->will($this->testCase->returnValue(Timestamp::now()));
if ($metadata === null) {
$metadata = new Metadata([]);
}
$envelope->expects($this->testCase->any())->method('metadata')->will($this->testCase->returnValue($metadata));
$envelope->expects($this->testCase->any())->method('enrichMetadata')->will($this->testCase->returnCallback(function (Metadata $newMetadata) use($id, $version, $metadata) {
return $this->mockEnvelope($id, $version, $metadata->merge($newMetadata));
}));
return $envelope;
}