Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

PHPUnit - test the validity of an SQL Query.

3

56 views

Hi,

I'm in the process of testing a factory class. One of the method must load the data in an array for the object that another method will instantiate.

That method contain the SQL query that hold a crutial condition that must be tested. ( in that case ask only for the records that are "published". Ex.: WHERE published=1 ). That distinction in the SQL Query is the only details that make that method differ from another one, and I want to test the query execution behaviour.

Now, I can't really mock my PDO object and ask him to return a fixed result as I would not test the execution of the query by mySQL, that would make a useless test.

That lead me to think that I'll need to setup a static database with fixed test data inside it. I'm I right on this or do I miss something ? Should I separate test requiring the "test database" from the tests that are autonomous ?

thanks.

asked March 23, 2011 8:59 am CDT
posted via StackOverflow

2 Answers

2
 

Yes, it is the common practice.

It is also the special methods for loading fixtures: http://www.phpunit.de/manual/3.6/en/database.html

What about second question: no, you should not separate them. Your tests should test the behaviour, not the details of implementation. Join test cases into test classes by logic.

answered March 23, 2011 9:23 am CDT
1
 

I strongly agree on the part of not mocking out the PDO. At some point i want to make sure my queries work against a real database. While this might not be a unit test anymore, technically. For me it offers so much more to know that my code that handles data storage really works against the db.

What i tend to do is creating a sort of Data Access class for each Class that needs to talk to the DB and so separating out most of the business logic from the db access code.

That way i can mock out the data access when testing the classes and after that set up a "Test Database" for each "data access class" and see if those work.

@zerkms Answer (+1) already linked http://www.phpunit.de/manual/3.6/en/database.html and the only other resource i have found to be of value when it comes to DB-Testing is the Book Real-World Solutions for Developing High-Quality PHP Frameworks and Applications that has a big chapter covering this subject.


Should I separate test requiring the "test database" from the tests that are autonomous ?

Only if your testsuite gets really really big and you have runtime issues that force you to say "hitting even a testing database just takes to long for all my tests so i run those only on a continuous integration server and not while developing.

answered March 23, 2011 9:23 am CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions
And not this is a test
January 22, 2011
And now this is a test
January 22, 2011
Just a test question
February 1, 2011
Another test question?
February 1, 2011
Test of highlighter
December 28, 2010
Another question test
January 17, 2011
A small test for men
February 20, 2011
test of some stuff
January 22, 2011
another test question
January 22, 2011
Me test question
February 21, 2011
another test and stuff
January 22, 2011
Test data with tomcat
March 13, 2011