laravel - Mock a method in enum using PHPunit Mockery in PHP
one text
I have run into a problem when mocking Enums using Mockery + PHPUnit in PHP 8.1 (im using the laravel framework but I'm not sure this is relevant).
I have defined an enum:
<?php namespace MyNamespace\Enums;
enum MyEnum {
case A;
case B;
public function method1(): string {
return match ($this) {
self::A => 'A',
self::B => 'B',
};
}
}
I am trying to mock MyEnum. I have created a test that looks something like this:
public function testShouldCanReturnsB() {
$mock = \Mockery::mock('\MyNamespace\Enums\MyEnum');
$mock->shouldReceive('method1')->andReturn('B');
}
When I run the test I receive the message:
Mockery\Exception : The class \MyNamespace\Enums\MyEnum is marked final and its methods cannot be replaced. Classes marked final can be passed in to \Mockery::mock() as instantiated objects to create a partial mock, but only if the mock is not subject to type hinting checks.
How could I mock the method inside Enum?
Source