Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Mock Integration Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Test-wide setup via trait: Use MockTrait in base test classes to auto-register mocks per test method. Avoids manual setUp() boilerplate.
  • Namespace isolation: Mock time\date() to avoid interference between namespaces — critical when using namespaced code that calls internal functions.
  • Per-test fixture control: Call $this->unmockAll() or rely on automatic teardown after each test. Use $this->mockFunction() inside setUp() for shared state.
  • Framework alignment: In Laravel, combine with php-mock-laravel or use the base trait in feature tests to mock config(), config_path(), or file_get_contents().
  • Minimal侵入性 (non-invasive): No need for dependency injection or wrapper classes — mock directly where functions are called (e.g., inside services, controllers, CLI commands).

Gotchas and Tips

  • Autoloader order matters: Ensure php-mock’s function interceptor loads before your app code. Use require in bootstrap.php, not require_once, and avoid conflicting autoloading.
  • Function name collisions: 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.
  • Extension points: The package exposes MockTrait as its main integration surface — avoid extending it directly; compose instead via traits or custom base classes.
  • Debugging: Enable 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.
  • Not for runtime: This is test-only — double-check it’s excluded from production composer.json via replace-dev or require-dev scoping.
  • Stars ≠ adoption: Only 14 stars — tread carefully; verify open issues for PHPUnit 10+/11 compatibility, especially since the last release is listed as 2026 (likely a typo — check composer.lock version at runtime).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport