How to catch PHP Warning in PHPUnit

后端 未结 6 1033
迷失自我
迷失自我 2021-01-01 15:47

I am writing test cases and here is a question I have.

So say I am testing a simple function someClass::loadValue($value)

The normal test case

6条回答
  •  耶瑟儿~
    2021-01-01 16:14

    Using Netsilik/BaseTestCase (MIT License) you can test directly for triggered Errors/Warnings, without converting them to Exceptions:

    composer require netsilik/base-test-case


    Testing for an E_USER_NOTICE:

    _convertNoticesToExceptions  = false;
            $this->_convertWarningsToExceptions = false;
            $this->_convertErrorsToExceptions   = true;
        }
    
        public function test_whenNoticeTriggered_weCanTestForIt()
        {
            $foo = new Foo();
            $foo->bar();
    
            self::assertErrorTriggered(E_USER_NOTICE, 'The warning string');
        }
    }
    

    Hope this helps someone in the future.

提交回复
热议问题