nunomaduro/mock-final-classes
Dev-only helper that automatically enables mocking of final classes and methods in PHP tests. Works with PHPUnit, Pest, and ParaTest by removing final keywords on the fly via dg/bypass-finals—install with Composer and it just works.
composer require --dev nunomaduro/mock-final-classesphpunit, pest, or paratest as usual.final class from an external SDK (e.g., Stripe, AWS SDK, or Laravel’s own Application class pre-9.0).
// Works seamlessly in PHPUnit, Pest, and ParaTest
$sdkClient = $this->createMock(FinalSdkClient::class);
$sdkClient->method('send')->willReturn('mocked response');
💡 It works out of the box in all supported frameworks — no need to extend custom TestCase, add annotations, or adjust test runners.
createMock() or getMockBuilder() normally: The package intercepts PHP’s final enforcement transparently. Use existing mocking workflows ($this->createMock(), mock(), Prophecy, Mockery if manually integrated).final for API stability — now also in parallel test suites via ParaTest.// Mocking a final command from a third-party library
$command = $this->createMock(FinalCommand::class);
$command->method('execute')->willReturn(['status' => 'ok']);
$service = new MyService($command);
$this->assertEquals(['status' => 'ok'], $service->run());
use NunoMaduro\MockFinalClasses\MockFinalClassesTrait;
Mockery::mock(FinalClass::class . '[method]', null, true)
->shouldAllowMockingProtectedMethods()
->viaMockFinalClasses();
mock() helper as usual — no extra steps required:
$mock = mock(FinalSdkClient::class);
$mock->allowMockingProtectedMethods();
$mock->shouldReceive('send')->andReturn('mocked');
final is still enforced outside test context.final methods inside final classes (only class-level final).nunomaduro/mock-final-classes appears after phpunit/phpunit (or pestphp/pest) in composer.json’s require-dev.--bootstrap or phpunit.xml inclusion).--stop-on-failure and inspect for ReflectionException or TypeError.#[\AllowDynamicProperties] and readonly alongside final — this package does not bypass readonly (only final), so test against such classes may still fail.ReflectionClass::setExportOptions() (PHP ≥8.1) — ensure your test environment matches PHP ≥8.1 to leverage all features.final-class mocking across parallel or Pest-based test suites.How can I help you explore Laravel packages today?