I'm beginning with testing my code under PHP 7.0, but I found one problem running tests for type-hinting.
Let's have a simple class:
<?php
class foo {
public function bar(foo $x) {
}
}
Test for that class will contain something like this:
<?php
class fooTest {
public function barWillRejectUnknownObject() {
$foo = new foo;
$this->setExpectedException(get_class(new \PHPUnit_Framework_Error("", 0, "", 1)));
$foo->bar(new stdClass());
}
}
That code, found at StackOverflow, worked without any problems under PHP 5. I used this hack because of #178.
Unfortunately it seems that it's no longer valid approach, since TypeError exception is thrown. The only method I found is to use standard try/catch block.
I tried using setExpectedException('TypeError') but it doesn't work. I think reason for that is TypeError does not extends Exception.
There is no point in (trying to) test whether an error is raised when trying to invoke a method with an argument that does not match a type declaration in the signature.
Unfortunately I have to disagree with you @sebastianbergmann. What about situation presented below?
Let's have a class A:
class A
{
/**
* @var FooInterface
*/
private $foo;
/**
* @param $foo FooInterface
*/
public function setFoo($foo)
{
$this->foo = $foo;
}
/**
* @return FooInterface
*/
public function getFoo()
{
return $this->foo;
}
}
Some code depends on object of class A and perform call $objectOfA->getFoo()->getName(); - if some other code will be able to use setFoo() with something different than object implementing FooInterface it could be fatal for whole app.
In my opinion type declaration is important part of code integrity and must be tested. Of course setter could just check using instanceof if given parameter implements FooInterface, but why not do the same using language built-in feature?
Anyway I see it was fixed in https://github.com/sebastianbergmann/phpunit/commit/16b5b198d4b73e6713eae5ffbd6da2f66b0e2ea2.
Most helpful comment
There is no point in (trying to) test whether an error is raised when trying to invoke a method with an argument that does not match a type declaration in the signature.