首页 \ 问答 \ 在php中使用callable(use of callable in php)

在php中使用callable(use of callable in php)

我试图在下面的实现中模仿php的内置usort函数定义:

class heapSort {
    static function hsort(array &$array, callable $cmp_function){
       // logic
    }
}

class utility{
    static function mycomparator(){
        // logic
    }
}

$array = array(5,3,8,1);
$callback = array('utility','mycomparator');
heapSort::hsort($array, $callback);

虽然变量$callback是“可调用的”,但为什么我会遇到致命错误?

传递给heapSort :: hsort()的参数2必须是可调用的实例。

更具体地说,如何使$variable可以调用/类型化?


I am trying to mimic php's inbuilt usort function definition in my implementation below:

class heapSort {
    static function hsort(array &$array, callable $cmp_function){
       // logic
    }
}

class utility{
    static function mycomparator(){
        // logic
    }
}

$array = array(5,3,8,1);
$callback = array('utility','mycomparator');
heapSort::hsort($array, $callback);

While the variable $callback is "callable" why do I get below fatal error?

Argument 2 passed to heapSort::hsort() must be an instance of callable.

More specifically, how do I make/typecast a $variable to callable?


原文:https://stackoverflow.com/questions/15818301
更新时间:2023-08-03 09:08

最满意答案

getBicycle()方法永远不会返回RuntimeException。 代码本身正在捕获RuntimeException,并在被捕获时写入记录器。 方法本身将返回Bicycle或null。

您需要重新考虑getBicycle方法的运行方式。 如果您希望RuntimeException通过,它可能会重新抛出RuntimeException atfer logging。 但是,根据编写的方式,RuntmeException永远不会进入JUnit测试


The getBicycle() method will never return a RuntimeException. The code itself is catching the RuntimeException and, when caught, writes to the logger. The method itself will either return the Bicycle or null.

You will need to rethink how you want the getBicycle method operates. It could re-throw the RuntimeException atfer logging if you want the RuntimeException to bubble through. But, based on how that's written, the RuntmeException will never make it out to the JUnit test

相关问答

更多
  • 为什么它不起作用。 那么你的问题在于你的测试中的TransactionTemplate是一个模拟。 因此它具有与TransactionTemplate相同的接口,但它不知道如何表现。 你负责它的实现 - 这就是嘲笑的重点。 您在代码中明确调用了template.execute() ,这就是您的第一次验证通过的原因。 但是execute()并不是Spring的一个(或者更准确地说,在你的测试中template不是Spring的TransactionTemplate的一个实例,它只是它的一个模拟) - 也就是说 ...
  • 您可以在testcase中创建新的内部类,扩展EmployeeRegistry,并覆盖方法“findEmployeeById”并调用新类的“createEmployee”。 -- Answer posted below I learnt that i don't actually need to spy, i can just mock the internal method as well and that works :) e.g., @Test public void createEmployeeTh ...
  • 您需要先模拟包含selectSomethingBySomething()的类并记录此行为。 在mockito中你会说: SomeDao someDaoMock = mock(SomeDao.class); willThrow(new SQLException())).given(someDaoMock).selectSomethingBySomething(); 然后在测试中注入一些someDaoMock到你的类中,当它调用一些someDaoMock.selectSomethingBySomething ...
  • 你的问题实际上是两个: 1:我如何避免执行模拟方法? 2:我如何模拟static方法? 这里是答案: Mockito提供了2个API来配置你的模拟。 更常见的(也是更可读的)是: when( mock.mockedMethod() ) .thenReturn(someValue); 问题在于真正的方法实际上是被执行的,只是结果被替换了。 通常这不是问题,除非您的方法基于同一对象中其他(模拟的但可能未配置的)方法的返回值抛出(未经检查的)异常,或者尝试访问模拟对象的依赖关系上的方法,因为它们为null所以一 ...
  • 我终于找到了解决方案:我的第一个测试是一个Kotlin,在一个“公开课”中,但是是一个未打开的方法声明。 Mockito 不能用包访问或最终方法来嘲笑一种方法 ! 在这种特殊情况下,当你试图嘲笑它时,它就会调用真正的方法,而不会告诉你任何事情...... 也许这篇文章对别人有用! - I finally ended finding the solution : My first test was a Kotlin one, in a "open class" but a non-opened method ...
  • 我不知道你的className,你需要将你的className替换为你自己的className。 也许,你可以做这样的事情,只是检查方法真正调用方法。 @Mock private INotificationDao iNotificationDao; private ClassName className; @Before public void init() { className = spy(new ClassName()); } @Test public vo ...
  • getBicycle()方法永远不会返回RuntimeException。 代码本身正在捕获RuntimeException,并在被捕获时写入记录器。 方法本身将返回Bicycle或null。 您需要重新考虑getBicycle方法的运行方式。 如果您希望RuntimeException通过,它可能会重新抛出RuntimeException atfer logging。 但是,根据编写的方式,RuntmeException永远不会进入JUnit测试 The getBicycle() method will ...
  • 我在这里为mapper提供了一个示例测试课程。 减速机的测试也可以用相同的方式编写。 @RunWith(MockitoJUnitRunner.class) public class SampleMapperTest { @Mock private Mapper.Context mockContext; // declare your mocks @Mock Counter mockCounter; // mocked hadoop counter SampleM ...
  • 我使用了以下方法: 我的TestClass扩展了BaseServiceTestCase(在liferay src中可用) 将portal-test.properties保留在test文件夹中(带有测试值)。 运行测试用例。 在这种情况下,liferay会加载所有的属性以及弹簧初始化。 I used the following method : My TestClass extends BaseServiceTestCase (available in liferay src) Keep portal-tes ...
  • 那么,代码可以改进,使其实际可测试... 目前,没有很好的方法为代码编写单元测试,因为构造函数创建了一个JasperserverRestClient而没有任何改变它的机会。 您可以做的最少是添加另一个构造函数(可能是包访问)以允许使用另一个JasperserverRestClient。 (或者你可以考虑使用Factory模式。但这可能会很复杂。) 然后你可以嘲笑...... JasperserverRestClient jasperServerClient = Mockito.mock( Jasperser ...

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)