- How do I install mpyw/mockery-pdo in a Laravel project?
- Run `composer require mpyw/mockery-pdo:VERSION@alpha` in your project root. Ensure your project uses PHP 8.2+ and Mockery 1.6.12+. The package is experimental, so check the latest alpha release for compatibility.
- Can I use this package with Laravel 9 or older?
- No, this package requires PHP 8.2+, which aligns with Laravel 10+. For Laravel 9 or older, you’ll need to either upgrade or explore alternatives like SQLite in-memory databases or legacy Mockery-based solutions.
- How do I mock a PDO statement with named parameters like `:email`?
- Use the fluent chain: `$pdo->shouldPrepare('SELECT * FROM users WHERE email = :email')->shouldBind()->value('email', 'test@example.com')`. This sets up expectations for the named parameter binding before execution.
- Does this package support binding boolean values in PDO?
- Yes, use `->boolValue('active', true)` in your expectation chain to mock boolean parameters. This ensures type safety when testing queries with boolean conditions.
- How do I simulate row-by-row fetching instead of `fetchAll()`?
- Use `->shouldStartFetching()->fetchReturns((object)['id' => 1])->fetchEnds()` to define progressive row fetching. This mimics PDO’s `fetch()` behavior for iterative result handling.
- Will this work with Laravel Eloquent queries?
- Yes, but you’ll need to mock the underlying PDO instance Eloquent uses. For complex queries (e.g., raw SQL with dynamic parameters), ensure your mock expectations align with the actual SQL generated by Eloquent.
- Are there any known issues with PHP 8.2’s new features (e.g., named args, read-only properties)?
- The package is designed for PHP 8.2+ and should work with its features, but test edge cases like sealed classes or constructor property promotion. Report issues to the GitHub repo if you encounter compatibility problems.
- What alternatives exist if I can’t upgrade to PHP 8.2?
- Consider Laravel’s built-in `DatabaseMocker` trait, SQLite in-memory databases, or forking this package to drop the PHP 8.2 requirement. Alternatives like `dbal/mock` (Doctrine) may also fit legacy stacks.
- How do I test transactions or error cases with this package?
- Extend the mock to include `shouldBeginTransaction()` or `shouldThrow(new PDOException())` in your expectation chain. For transactions, ensure rollback behavior is mocked if needed.
- Does this package support testing `bindValue()` vs. `execute([...])` parameter binding?
- Yes, you can mock both. Use `->shouldBind()->value('key', 'value')` for `bindValue()` or `->shouldExecute(['value1', 'value2'])` for array binding in `execute()`. The package validates both approaches.