Download the PHP package czukowski/phpunit-mock-db without Composer
On this page you can find all versions of the php package czukowski/phpunit-mock-db. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download czukowski/phpunit-mock-db
More information about czukowski/phpunit-mock-db
Files in czukowski/phpunit-mock-db
Package phpunit-mock-db
Short Description Database Abstraction Layer mocking helpers for PHPUnit
License MIT
Homepage https://github.com/czukowski/phpunit-mock-db
Informations about the package phpunit-mock-db
Database Abstraction Layer mocking helpers for PHPUnit
A mock-object library for database queries testing, without having to initialize in-memory database from fixtures. Rather, every query executed by a tested code can be set to return a pre-defined result set, affected rows count or last insert ID. All with a familiar interface similar to PHPUnit Mock Objects.
Installation
Version numbering follows major PHPUnit version numbers, so for a given PHPUnit N.x, you'll get the appropriate version of this package (this should happen automatically).
Usage
Use Cz\PHPUnit\MockDB\MockTrait
trait in a test case class, this will enable methods for
creating database mock instances. A 'fake' driver for a database abstraction layer used in the
tested code must be used, which implements both Cz\PHPUnit\MockDB\DatabaseDriverInterface
interface and that of the database abstraction layer's, additionally getDatabaseDriver
method
must be implemented by the test case class, that returns an instance of that driver.
Note: This covers just the most simple and the most common use case for testing against a single
database connection, and the trait has been designed accordingly. If it is required to have multiple
database connections mocked at the same time or a different way to inject dependencies into the
tested code, a different implementation of MockTrait
may be needed. But the trait is extremely
simple, especially in comparison to the 'fake' driver implementation that is needed anyway, you can
clone and adjust the trait for your project or come up with a completely different implementation.
Examples:
Return a pre-defined result set on any database query:
Return a pre-defined result set on any database query and expect it to be executed exactly once:
Return a pre-defined result set on each specific database query, expecting each query to be executed exactly once:
Note: the order in which the query expectations are being set up doesn't have to be same as the order in which the queries will be executed.
Also note: the whitespaces will be ignored in query constraints, so they can be loaded from well-formatted files, which could be especially useful for long and complex queries.
Expect mixed queries, some at specific invocations (note: SELECT query is set to return an empty result set):
Expect same query executed exactly three times and return different last insert IDs on each consecutive call, also note how this query is parametrized:
Return affected rows count:
Match SQL query using PHPUnit constraint (note: whitespace will not be ignored when using default PHPUnit constraints):
Set up different outcomes on consecutive calls for INSERT queries using a consecutive calls stub builder:
Set up custom callbacks to handle database queries (callbacks don't have to return anything):
By default, mock object is set to throw an exception if an unknown (unmatched) query is executed, but this can be disabled:
License
This work is released under the MIT License. See LICENSE.md for details.