php - PHPUnit how to avoid "OutOfRangeException" in __construct()
one text
I have a PHP unit that looks like this:
class Challenge1Test extends TestCase
{
/**
* @dataProvider invalidConstructorValues
*/
public function test_throws_exception_for_initial_value(int $value): void
{
$this->expectException(\OutOfRangeException::class);
new ImmutableWeekDay($value);
}
//...
}
It's testing the __construct() of my class and if the value is out of range is gives back OutOfRangeException. I am testing with data that is expected to throw that error.
/**
* @throws \OutOfRangeException
*/
public function __construct(int $value)
{
$this->value = $value;
}
The above gives the expected error on bad data entry.
I am trying to pass the test so I am only initializing that var when it meets the range requirement
/**
* @throws \OutOfRangeException
*/
public function __construct(int $value)
{
$refl = new \ReflectionClass($this);
$this->value = null;
foreach($refl->getConstants() as $k=> $v){
if ($v = $value){
$this->value = $value;
}
}
}
But I still get the out of range exception on bad data. Is there a way in my controller without modifying the test to pass it?
Link to my php sandbox code here: https://phpsandbox.io/n/old-term-kkap-0hqmq?files=%2Fsrc%2FChallenge1%2FImmutableWeekDay.php
Source