phpunit/phpunit-mock-objects
Default mock object library for PHPUnit. Create test doubles (mocks, stubs, spies) to isolate units under test, define expectations, control method returns, and verify interactions. Designed for PHPUnit projects and compatible with PHP 7.1+.
In modern Laravel development, you do not install phpunit/phpunit-mock-objects directly—it was fully merged into PHPUnit’s core by PHPUnit 5.4 (2015) and the package is now archived/unmaintained. To start mocking today:
composer require --dev phpunit/phpunit).PHPUnit\Framework\TestCase), use $this->createMock(Foo::class) or $this->getMockBuilder(Foo::class) immediately.$paymentGateway = $this->createMock(PaymentGateway::class);
$paymentGateway->method('charge')->willReturn(['status' => 'succeeded']);
$controller = new CheckoutController($paymentGateway);
createMock() is the idiomatic Laravel test pattern: concise, chainable, and type-hint safe:
$mailer = $this->createMock(Mailer::class);
$mailer->method('send')->willReturn(true);
onlyMethods() for legacy or fragile classes:
$order = $this->getMockBuilder(Order::class)
->onlyMethods(['validateStock'])
->disableOriginalConstructor()
->getMock();
$order->method('validateStock')->willReturn(false);
$logger->expects($this->once())
->method('error')
->with($this->stringContains('payment failed'));
atLeast()/atMostOnce() over at() to avoid brittle test order dependencies (common in parallel test runs).$repository = $this->createMock(UserRepository::class);
app()->instance(UserRepository::class, $repository);
phpunit/phpunit-mock-objects as a project dependency—its API is now in phpunit/phpunit and conflicts will arise (e.g., class redefinition, autoloader chaos).setCloneArguments(false) could preserve references—but modern PHPUnit uses shallow cloning by default and this setting is irrelevant unless maintaining ancient code.phpspec/prophecy (via phpspec/prophecy-phpunit) for its decoupled, intent-based mocking. If starting new, consider adopting Prophecy—it avoids mocking implementation details (e.g., constructor signatures).$this->mock() in TestCase (Laravel’s custom helper), but it’s just a Prophecy wrapper—not tied to this package.phpunit/phpunit-mock-objects: ^1.2), update to phpunit/phpunit:^9.6 and ensure tests use createMock()—no code changes are needed since the API is identical.composer.lock for orphaned phpunit/phpunit-mock-objects—remove it and regenerate autoloader (composer dump-autoload).How can I help you explore Laravel packages today?