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 Mockery Laravel Package

php-mock/php-mock-mockery

Integrates php-mock with Mockery to mock PHP built-in functions in tests. Create namespaced function mocks with PHPMockery::mock() and set expectations/returns, then call Mockery::close(). Supports only unqualified calls; define early if needed.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install via Composer:
    composer require --dev php-mock/php-mock-mockery
    
  2. Enable the trait in your PHPUnit test class:
    use PHPMock\Mockery\PHPMock;  
    class MyTest extends TestCase  
    {  
        use PHPMock;  
        // ...  
    }  
    
  3. Start mocking functions by name (e.g., time()), using Mockery’s familiar syntax:
    $timeMock = $this->getFunctionMock(__NAMESPACE__, 'time');  
    $timeMock->expects($this->once())->willReturn(1234567890);  
    
    First use case: Test a timestamp-dependent function (date('Y', time())) deterministically without relying on real time.

Implementation Patterns

  • Use getFunctionMock($namespace, $functionName) to get a Mockery-compatible mock for any PHP function, even internal ones like file_exists, rand, microtime.
  • Target namespaces precisely: Always pass the target namespace (often __NAMESPACE__) to avoid clobbering global functions and ensure isolation.
  • Combine with Mockery expectations: Use expects($this->exactly(3)), andReturn(), andThrow() etc. exactly as you would for objects.
  • Leverage with setUp/tearDown or @before/@after annotations to isolate mocks per test:
    protected function setUp(): void {  
        $this->getFunctionMock(__NAMESPACE__, 'time')->willReturn(0);  
    }  
    
  • Mock side-effecting functions (e.g., sleep, shuffle) to avoid delays or randomness:
    $this->getFunctionMock(__NAMESPACE__, 'sleep')->andReturn(0); // skips delay  
    

Gotchas and Tips

  • Namespace matters: If your test class lives in Tests\, but the code under test is in App\Services, pass App\Services::class or the string 'App\\Services' to getFunctionMock(). Using __NAMESPACE__ in a test trait may mislead you!
  • Call order / timing: Function mocks are only active after the mock is defined in the current test. Define before invoking the code under test.
  • No mocking across namespaces: A mock for App\time() won’t affect calls to time() in the global namespace. Always target the calling namespace.
  • Autoloading quirks: php-mock replaces internal functions by registering a runtime autoloader. This only works in CLI (CLI unit tests), not in web contexts. Always use in unit/integration tests, never in dev/staging app bootstrapping.
  • Debugging tip: If mocks behave unexpectedly, verify namespace and that the mocked function is actually being called in the target namespace (e.g., date() calls use global time() unless namespaced).
  • Extension points: While not extendable per se, combine with php-mock/php-mock-phpunit for trait-based mocks across multiple test suites, or use PHPMock::disableGlobalMock() to reset mocks between groups of tests.
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
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
twbs/bootstrap4