Catch an error as exception

倖福魔咒の 提交于 2019-12-13 03:58:32

问题


Given the following code:

<?php

class Language
{
    private $code;

    public function __construct(string $code)
    {
        $this->code = $code;
    }
}

$lang = new Language();

I will get an ArgumentCountError like this:

Uncaught ArgumentCountError: Too few arguments

Now I would like to test and ensure this beahviour with a phpUnit unit test like this one:

class LanguageTest
{
    public function testLanguageConstructorWillRequireACode()
    {
        $language = new Language();

        $this->expectException(MissingArgumentException::class);
    }
}

But php interpeter raise an error and not an exception.

What would be the most idiomatical and correct way to do this?

来源:https://stackoverflow.com/questions/48486568/catch-an-error-as-exception

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