php-mock/php-mock-integration
Integration layer for php-mock, making it easy to use PHP function mocks within popular test frameworks. Helps you stub built-in functions like time(), rand(), and file operations in specific namespaces, enabling reliable, isolated unit tests without touching global state.
Start by installing the package alongside php-mock/php-mock and your test runner (e.g., PHPUnit):
composer require --dev php-mock/php-mock php-mock/php-mock-integration
Then, in your test suite (e.g., phpunit.xml or bootstrap file), load the integration traits or use the provided MockTrait. The simplest first use case is mocking date() in a test:
usephpmock\integration\MockTrait;
usephpmock\PHPMock;
class TimeTest extends \PHPUnit\Framework\TestCase {
use MockTrait;
public function testDateIsMocked(): void {
$this->mockFunction('date', function() { return '2023-01-01'; });
$this->assertEquals('2023-01-01', date('Y-m-d'));
}
}
Check tests/ in the repo for quickstart examples — this package assumes familiarity with php-mock basics.
MockTrait in base test classes to auto-register mocks per test method. Avoids manual setUp() boilerplate.time\date() to avoid interference between namespaces — critical when using namespaced code that calls internal functions.$this->unmockAll() or rely on automatic teardown after each test. Use $this->mockFunction() inside setUp() for shared state.php-mock-laravel or use the base trait in feature tests to mock config(), config_path(), or file_get_contents().php-mock’s function interceptor loads before your app code. Use require in bootstrap.php, not require_once, and avoid conflicting autoloading.php-mock mocks functions only in the current namespace. Mock rand() in App\Services but not global\rand() unless explicitly targeted — use phpmock\PHPMock::setNamespace() or qualify function calls.MockTrait as its main integration surface — avoid extending it directly; compose instead via traits or custom base classes.PHP_MOCK_DEBUG=1 to log mock registration/failure. Common failure: functions not mocked because the test file’s namespace doesn’t match the production code.composer.json via replace-dev or require-dev scoping.composer.lock version at runtime).How can I help you explore Laravel packages today?