Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

untymage's avatar

Mockery - How to set argument for constructor?

class ScanEpisode
{
    public function __construct(
        public int $episode,
    ){}

    public function isOk(): bool
    {
	   $this->episode;
       // ...
    }
}
    public function test_is_ok_method()
    {
        $m = $this->partialMock(ScanEpisode::class);
        // assertions ...
        $m->isOk();
    }

Hey i'm new at mockey, How can i set argument ($episode) for the ScanEpisode class ? when i do that above it says:

Error : Typed property App\ScanEpisode::$episode must not be accessed before initialization

0 likes
7 replies
tykus's avatar
tykus
Best Answer
Level 104

This should do what you want.

$m = \Mockery::mock(ScanEpisode::class, [1])->makePartial();

Edit

2 likes
untymage's avatar

@tykus They're not all of my code (i'm calling third-party apis on another methods)

Btw, i got this error :

Error : Call to undefined method Mock_ScanEpisode_725fcb95::makePartial()

Also phpstorm says :

img

tykus's avatar

@untymage sorry, of course makePartial is a method on a Mockery\Mockery instance 🤦‍♂️

untymage's avatar

@tykus if you mean:

$m = \Mockery::mock(ScanEpisode::class, [ 'episode' => 1])->makePartial();

Not working:

Error : Typed property App\ScanEpisode::$episode must not be accessed before initialization
untymage's avatar

@tykus it's working but we should not pass assoc array, edit you post then i mark it as best thanks

1 like
paulpreibisch's avatar

@untymage I got same error 'must not be accessed before initialization', even with an indexed array. How can I pass constructor args properly?

1 like
flavioludi's avatar

It worked passing it without the index, as @tykus wrote:

$m = \Mockery::mock(ScanEpisode::class, [1])->makePartial();

In this case, only the integer 1 as an argument for the constructor

Please or to participate in this conversation.