Mockery::on() — тестирование closure?

Есть код с closure - который никак не получается покрыть тестами. Смотрел документацию laravel, phpunit, но четкого разъяснения для себя не смог найти.

Вот участок кода, который не могу покрыть тестами
$ganeshaRateBuilder = $this->rateBuilder
            ->adapter($adapter)
            ->storageKeys(new StorageKeys())
            ->minimumRequests(10)
            ->failureRateThreshold(50)
            ->intervalToHalfOpen(10)
            ->timeWindow(30)
            ->build();

//Здесь упростил код, на самом деле в close есть условия, которые выполняются в зависимости от значения $event
        $ganeshaRateBuilder->subscribe(function ($event, $service, $message) {
            $this->toSlack(config('slack.channels.circuit'), "circuit is open for $service");
        });


Вот тест, который не покрывает и вылетает с ошибкой
/** @var Ganesha|MockObject $ganesha */
        $ganesha = $this->getMockBuilder(Ganesha::class)
            ->disableOriginalConstructor()
            ->onlyMethods(['subscribe'])
            ->getMock();

$closure = Mockery::on(function ($closure) use ($event, $service, $message) {
            $circuitBreaker = Mockery::mock(CircuitBreaker::class);
            $circuitBreaker->shouldReceive('toSlack')->once()->with(config('slack.channels.circuit'), "circuit is open for $service");

            $closure($event, $service, $message);

            return true;
        });

$ganesha
            ->expects($this->once())
            ->method('subscribe')
            ->with($closureMethod)
            ->willReturnSelf();


но если в with() подставить
$closureMethod = function ($event, $service, $message) {
            
        };


То тест успешно выполнится, но правда, покрытие тестами выходит 0.
Вот текст ошибки


1) Tests\Unit\Services\CircuitBreaker\CircuitBreakerTest::it_should_return_circuit_breaker_instance
Expectation failed for method name is "subscribe" when invoked 1 time(s)
Parameter 0 for invocation Ackintosh\Ganesha::subscribe(Closure Object (...)): void does not match expected value.
Closure Object &0000000001f786fa0000000008db000d (
0 => Closure Object &0000000001f786fa0000000008db000d
) is not instance of expected class "Mockery\Matcher\Closure".
--- Expected
+++ Actual
@@ @@
-Mockery\Matcher\Closure Object &0000000001f786d70000000008db000d (
- '_expected' => Closure Object &0000000001f786ab0000000008db000d (
- 0 => Closure Object &0000000001f786ab0000000008db000d
- )
+Closure Object &0000000001f786fa0000000008db000d (
+ 0 => Closure Object &0000000001f786fa0000000008db000d
)

Где я ошибаюсь?
  • Вопрос задан
  • 50 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы