php7 phpunit,Make phpunit catch php7 TypeError
原文:
I am trying to validate that a php7 function accept only integers.
This is the class:
declare(strict_types=1);
class Post
{
private $id;
public function setId(int $id)
{
$this->id = $id;
}
}
And this is the test:
declare(strict_types=1);
class PostTest extends \PHPUnit_Framework_TestCase
{
private function getPostEntity()
{
return new Post();
}
public function testSetId()
{
$valuesExpected = [123, '123a'];
foreach ($valuesExpected as $input) {
$this->getPostEntity()->setId($input);
}
}
}
The error I get is:
TypeError: Argument 1 passed to Post::setId() must be of the type integer, string given, called in /path/test/PostTest.php on line 35
Is it possible to validate such error? also, does it make any sense to run such a check?
# Answer 1
Yes, you can test for TypeError the same way you would use for any other exception.
However, I would not test that PHP emits a type error in case of a type mismatch. This is the kind of test that becomes superfluous with PHP 7 code.
# Answer 2
Sadly, TypeError is not a subclass of Exception (reference), whilst it extends Error. The only thing they are really sharing is the the Throwable interface. The ThrowMatcher can't actually catch a TypeError.
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.
# Answer 3
Try this:
$this->expectException(TypeError::class);
# Answer 4
For newer PHP versions try:
$this->expectError(TypeError::class);
php7 phpunit,Make phpunit catch php7 TypeError相关推荐
- 【PHP7源码分析】PHP7到底有多快,基准测试与特性分析告诉你
顺风车运营研发团队 王坤 发表至21CTO公众号(https://mp.weixin.qq.com/s/ph...) PHP是一种跨平台开源语言,也是迄今为止最流行的Web开发语言,全球有超过80%的 ...
- linux phpunit 安装,PHPUnit安装教程
使用PHPUnit的第一步就是安装PHPUnit. 1 准备条件 1.1 PHPUnit版本选择 最新版PHPUnit并不能在所有的PHP环境下使用. 例如,PHPUnit 6.4 不能在PHP 5. ...
- php7与php5的区别,PHP7和PHP5区别
PHP7比PHP5.0 ~ 5.6快了近5倍,同时还降低了对系统资源的占用.主要是PHP7对Zend引擎进行了深度优化. 之前在书上看到过两者比较全面的区别,回头去翻了翻书[眼过千遍,不如手过一遍啊] ...
- php7协程通信使用,PHP7下的协程实现
原标题:PHP7下的协程实现 什么是协程 先搞清楚,什么是协程. 你可能已经听过『进程』和『线程』这两个概念. 进程就是二进制可执行文件在计算机内存里的一个运行实例,就好比你的.exe文件是个类,进程 ...
- ubuntu php7 memcache,linux上安装php7 memcache扩展
php7安装memcache扩展需要memcache php7的分支 否则安装会失败 php7的memcache扩展安装,真的很让人心碎! 下面则是php7的扩展memcache安装了. 用之前的 ...
- php7 apache mysql_Windows下安装php7+Apache2.4+MySQL
因为学习需要,所以在Windows下安装php7+Apache2.4+MySQL 1.安装Apache 到官网下载Apache2.4 ,选择Windows版本,这里本人下载2.4版本 下载之后解压 修 ...
- linux php7扩展查看,linux安装PHP7以及扩展
Linux下安装PHP7 事先升级gcc4.8,然后安装PHP7,安装步骤参考:CentOS安装PHP7 1.Linux下编译的php没有php.ini 解决办法:从源代码目录中复制php.ini-d ...
- 如何验证php7安装成功_linux安装php7的方法详解
linux如何安装php7? 1.安装依赖包 yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjp ...
- php7.3 安装详解,php7.3.20以及常用拓展安装
瞎几把装点依赖吧,要不要用的都给装上: yum install -y gcc gcc-c++ libxml2-devel openssl-devel curl-devel libjpeg-devel ...
最新文章
- 2020 CCPC-Wannafly Winter Camp Day1 总结
- 第三周项目一-个人所得税计算器
- [partial] C#里partial关键字的作用
- 股票交易应用系统的性能分析工具介绍(一)
- 矩阵二范数(norm)
- fms3 NetConnection.Connect.Rejected 问题
- 第一章 简单工厂模式
- PowerPC VxWorks BSP分析(2)--PowerPC汇编
- c和JAVA的安全编码_C、C++ 和 Java安全编码实践提示与技巧
- 谷歌为什登不上去github_安卓开源是假的,谷歌随时禁用,华为手机在海外市场已经快跌没了...
- Java 即将迎来转折点
- android studio聊天跳转_android app跳转到微信的示例
- packages are looking for funding
- 小米虚拟键透明方法_小米 6 用户该换机了!小米 6 Pro 曝光
- 2009 中国协同软件机遇年?
- java 区分中英文_Java 区分文本中的中英文字符函数
- 【免费抢票】6月9日杭州,产品经理会议
- 二、什么是asp网站
- 配置 SQL Server 以便使用 2 GB 以上的物理内存
- 无损音乐知识收集3(转)