SimpleTest: How to claim that a PHP error is being called?

If I'm right, SimpleTest will allow you to assert a PHP error. However, I cannot figure out how to use it, based on the documentation. I want to claim that the object I pass to my constructor is an instance of MyOtherObject

 class Object { public function __construct(MyOtherObject $object) { //do something with $object } } //...and in my test I have... public function testConstruct_ExpectsAnInstanceOfMyOtherObject() { $notAnObject = 'foobar'; $object = new Object($notAnObject); $this->expectError($object); } 

Where am I going wrong?

+7
php unit-testing simpletest
source share
4 answers

Print the E_RECOVERABLE_ERROR hints that SimpleTest can catch with PHP version 5.2. The following errors will contain any error containing the text "must be an instance". The PatternExpectation constructor accepts the regular expression perl.

 public function testConstruct_ExpectsAnInstanceOfMyOtherObject() { $notAnObject = 'foobar'; $this->expectError(new PatternExpectation("/must be an instance of/i")); $object = new Object($notAnObject); } 
+13
source share

PHP has both errors and exceptions that work a little differently. Passing the wrong type to the typehinted function will throw an exception. You should catch this in your test case. For example:.

 public function testConstruct_ExpectsAnInstanceOfMyOtherObject() { $notAnObject = 'foobar'; try { $object = new Object($notAnObject); $this->fail("Expected exception"); } catch (Exception $ex) { $this->pass(); } } 

or simply:

 public function testConstruct_ExpectsAnInstanceOfMyOtherObject() { $this->expectException(); $notAnObject = 'foobar'; $object = new Object($notAnObject); } 

But note that this will stop the test after the line where the exception occurs.

+2
source share

Turns out SimpleTest doesn't actually support this. You cannot catch Fatal PHP errors in SimpleTest. The hinting type is wonderful, except that you cannot verify it. The hint type generates fatal PHP errors.

+2
source share

you should expect an error before this happens, then SimpleTest will swallow it and count the pass, if the test is completed to the end and there is no error, then it will fail. (there expectError and expectException, which act the same, for PHP (non-fatal) errors and Exceptions, respectively.)

+1
source share

All Articles