After upgrading to PHPUnit 13, you may run into this deprecation warning in Laravel test suites:
Using with*() without expects() is deprecated and will no longer be possible in PHPUnit 14.
At first sight, the code often already looks correct:
$mock->expects($this->once())
->method('runSequenceOfCommands')
->with([...]);
So what is actually causing the warning?
The real issue
In many Laravel applications, the problem comes from using Laravel’s createPartialMock() helper together with newer PHPUnit versions.
Example:
$class = $this->createPartialMock(
NightlyCommand::class,
['runSequenceOfCommands']
);
While this worked fine in older PHPUnit versions, PHPUnit 13 tightened internal mock expectation handling and now emits deprecation warnings in some cases when using Laravel’s wrapper helpers.
The fix
Instead of createPartialMock(), use PHPUnit’s native mock builder API.
Replace this:
$class = $this->createPartialMock(
NightlyCommand::class,
['runSequenceOfCommands']
);
With this:
$class = $this->getMockBuilder(NightlyCommand::class)
->onlyMethods(['runSequenceOfCommands'])
->getMock();
And for multiple mocked methods:
$class = $this->getMockBuilder(NightlyCommand::class)
->onlyMethods([
'runSequenceOfCommands',
'confirm',
])
->getMock();
Why this works
getMockBuilder() uses PHPUnit’s modern native mock API directly, avoiding the legacy compatibility layer behind Laravel’s partial mock helpers.
This makes your tests:
- compatible with PHPUnit 13
- future-proof for PHPUnit 14
- less dependent on framework-specific mock wrappers
Final result
Your expectations can stay exactly the same:
$class->expects($this->once())
->method('runSequenceOfCommands')
->with(
$this->isInstanceOf(NightlyCommand::class),
[
// expected commands
]
);
But by switching to getMockBuilder(), the deprecation warning disappears cleanly.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.