Hi Chris,
On Thu, Jun 28, 2012 at 08:25:05AM -0600, Chris McMahon wrote:
P.S.: On a
related note ... one could think about mocking the database
as a whole for PHPUnit tests. Thereby, one would get rid of
unnecessary database coupling for unit testing, get better
control/detection of side effects, and really solve the database
performance problem for unit tests in one go.
I'd like to hear more about this.
Luckily enough, we're in a pretty much standard situation here.
Object A relies on an Object B that it receives from a factory C.
For unit testing Object A, you do not want to use a real heavy-weight
Object B, but much rather a fake Object B, say B'. B' does not need /
incorporate the business logic of B. Only for some well-defined cases,
B' should give the same answers that B would give. B' is an extremely
double-plus lightweight B. What B' should however do is book keeping:
Which method has been called using what parameters.
So when unit testing Object A using B'
- the tests typically run faster because
- B' contains no business logic of B or objects B depends on.
- B' already "knows" (instead of B having to compute them) the
correct return values for the defined method invocations.
- we can check what methods Object A invoked on B' and thereby assert
that the expected functions (and only the expected ones) have been
invoked on the factory provided object [1].
- failed tests typically signify problems closely related to Object A,
and no longer problems caused by Object X, that is used by Object Y,
that ... is used by Object B.
In our case, Object A is the object we want to test.
B is an implementation of DatabaseBase.
B' is a mock implementation of DatabaseBase.
C is the load balancer / wfGetDB(...) / ... whatever layer you choose.
Typically, mocking the database is a huge issue, and hardly
feasible. One would have to abstract database access above the SQL
layer (separate methods for select, insert, ...) to be able to get a
meaningful and usable interface for the tests.
But wait!
We already have all that ;-)
Kind regards,
Christian
[1] Note that we do /not/ have to change B for this, as it all happens
on B'.
--
---- quelltextlich e.U. ---- \\ ---- Christian Aistleitner ----
Companies' registry: 360296y in Linz
Christian Aistleitner
Gruendbergstrasze 65a Email: christian(a)quelltextlich.at
4040 Linz, Austria Phone: +43 732 / 26 95 63
Fax: +43 732 / 26 95 63
Homepage:
http://quelltextlich.at/
---------------------------------------------------------------