PHPSpec Catching TypeError in PHP7

偶尔善良 提交于 2019-12-05 04:12:40

For me it works if I annotate the unit test with this:

/**
 * @expectedException \TypeError
 */

Then my test is green.

Upon further investigation, this is a PHPSpec bug, and has been reported here. The bug hasn't been fixed in several months, so I would suggest commenting on it.

If you look at the code in src/PhpSpec/Matcher/ThrowMatcher.php, you can see that PHPSpec catches Exceptions that inherit 'Exception' and then checks the instance type of that exception. But, TypeError doesn't inherit from Exception, it inherits from Error. The only thing it has in common with an Exception, is that they both implement the Throwable interface.

For example:

101     public function verifyPositive($callable, array $arguments, $exception = null)
102     {
103         try {
104             call_user_func_array($callable, $arguments);
105         } catch (\Exception $e) {
106             if (null === $exception) {
107                 return;
108             }
109
110             if (!$e instanceof $exception) {
111                 throw new FailureException(sprintf(
112                     'Expected exception of class %s, but got %s.',
113                     $this->presenter->presentValue($exception),
114                     $this->presenter->presentValue($e)
115                 ));
116             }

Report the bug, explain these details, and show them this documentation about the inheritance of TypeError.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!