Using Fakes
NativePHP provides fake classes for all its contracts in theNative\Desktop\Fakes namespace. These fakes implement the same interfaces as the real services but provide testing utilities.
Available Fakes
The following fakes are available:WindowManagerFake- For testing window operationsQueueWorkerFake- For testing queue worker operationsPowerMonitorFake- For testing power monitoringGlobalShortcutFake- For testing global shortcutsShellFake- For testing shell operationsChildProcessFake- For testing child processes
Queue Worker Testing
Test queue worker operations using theQueueWorkerFake:
QueueWorkerFake Methods
Power Monitor Testing
Test power monitoring features using thePowerMonitorFake:
PowerMonitorFake Methods
Testing with Facades
If you use facades, you can swap them for fakes:Testing Service Providers
Test that your service provider correctly configures NativePHP:Mocking for Custom Behavior
If you need custom return values, mock the services:Integration Testing
Test the integration between your code and NativePHP:Testing Database Operations
Test database operations with the NativePHP database:Testing Storage Disks
Test storage disk operations:Best Practices
- Use Fakes: Prefer fakes over mocks when available for better test reliability
- Test Isolation: Each test should be independent and not rely on other tests
- Assert Behavior: Focus on testing behavior, not implementation details
- Mock External: Mock external services and APIs, not your own code
- Test Edge Cases: Test error conditions, empty states, and boundary conditions
Common Test Patterns
Troubleshooting Tests
Fake Not Working
If fakes aren’t being used:- Ensure you’re binding to the correct contract interface
- Check that you’re resolving from the container, not using
new - Verify the fake is set up before the code under test runs
- Use
$this->app->instance()for proper container binding
Assertions Failing
If assertions fail unexpectedly:- Check that methods are actually being called
- Verify you’re asserting on the correct fake instance
- Use inspection properties to debug what was actually called
- Add temporary dumps to see the fake’s state
Database Issues in Tests
If database tests fail:- Use
RefreshDatabasetrait to reset between tests - Configure an in-memory SQLite database for speed
- Run migrations in
setUp()if needed - Check that you’re using the correct database connection