phpspec / prophecy

Highly opinionated mocking framework for PHP 5.3+
MIT License
8.53k stars 241 forks source link

Chained methods should return self, is there any workarounds? #596

Closed emircanerkul closed 1 year ago

emircanerkul commented 1 year ago

Test File:

    $query = $this->prophesize(QueryInterface::class);
    $this->entityStorage->getQuery()->willReturn($query->reveal());

    $entityTypeManager = $this->prophesize(EntityTypeManager::class);
    $entityTypeManager->getStorage('trophy')->willReturn($this->entityStorage->reveal());

I got Call to a member function sort() on null error. But it should return itself.

image

At the end execute method I'll override the data as needed but before this, I need to run chained methods which return themselves. How can I do that? Shouldn't this be done automatically?

stof commented 1 year ago

The issue here is that you don't configure what Query::condition should return (and as there is no native return type, it returns null by default)

emircanerkul commented 1 year ago

@stof thank you

This is how I did. If there is any better way, please let me know.

$query = $this->prophesize(QueryInterface::class);
$query->condition(Argument::any(), Argument::any())->willReturn($query);
$query->sort(Argument::any(), Argument::any())->willReturn($query);
$query->range(Argument::any(), Argument::any())->willReturn($query);
$query->execute()->willReturn([1]);
emircanerkul commented 1 year ago

Okay, thank you