I have the following abstract class which is extended by another class:
abstract class A_Class{
protected function returnSomething(array $param = ['some_argument' => false])
{
if(!is_bool($param))
{
throw new Exception('some message goes here');
}
}
}
class B_Class extends A_Class{}
And I'm using PHPUnit 4.8.27 by Sebastian Bergmann and contributors.
.
I have the following test
/**
* @expectedException \Exception
*/
public function testException()
{
$method = new ReflectionMethod(B_Class::class, 'returnSomething');
$method->setAccessible(true);
$method->invokeArgs(new B_Class(), ['some_argument' => 'string']);
}
When I run my tests the following message appears:
Failed asserting that exception of type "\Exception" is thrown.
I've google'd around a bit, I can't really find and answer as to what I'm doing wrong. To be perfectly honest I'm not even sure that I'm doing anything wrong. The problem itself may not be with my code as much as it is with the Reflection
class. I don't know much about it and all the documentation is kind of, uhm, lacking. It may not be able to throw the exception defined in the reflected class.
Any pointers in the right direction here would be greatly appreciated.
What I've tried so far:
Using ReflectionClass
instead of ReflectionMethod
:
/**
* @expectedException \Exception
*/
public function testGetExcerptException()
{
$method = new ReflectionClass(new B_Class()::class);
$methodToCall = $method->getMethod('returnSomething');
$methodToCall->setAccessible(true);
$methodToCall->invokeArgs(new B_Class(), ['some_argument' => 'string']);
}
Setting the visibility to public, which of course works, but that kind of defeats the purpose.
Aucun commentaire:
Enregistrer un commentaire