G
G
ganjo8882020-09-08 11:50:58
PHP
ganjo888, 2020-09-08 11:50:58

How to properly lock __invoke method?

Tested class

class Handler {

public function __invoke($params)
{
// У TemplateService тоже есть метод __invoke()
 $templateService = new TemplateService();
 $templateService($string);
}
}

i want to lock new TemplateService() i do it like this
class HandlerTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        $this->mockTemplateService();
    }

    public function testInvoke()
    {
        $handler = new Handler();
        $handler->__invoke($params);
    }

    private function mockTemplateService()
    {
        $templateService = \Mockery::mock(TemplateService::class);
        $templateService->shouldReceive('__invoke')
            ->with(__DIR__ .'templates/template.docx')
            ->andReturn(file_get_contents(__DIR__ . '../../data/template.docx'));
        return $this->app->instance(TemplateService::class, $templateService);
    }
}

But for some reason I don't get what I messed up. The test knocks on the original new TemplateService(). I will be grateful for help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Derepko, 2020-09-08
@uDenX

As far as I understand, you add a mock to the container, but then manually create an object, and do not request from the container

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question