Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit - test the validity of an SQL Query

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

That method contains the SQL query that holds a critical condition that must be tested. ( in this case only ask for the records that are "published". Ex.: WHERE published=1 ). That distinction in the SQL Query is the only detail that makes 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 it to return a fixed result as I would not test the execution of the query by mySQL. That would make a useless test.

That leads me to think that I'll need to set up a static database with fixed test data inside it. Am I right on this or have I missed something?

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

like image 903
FMaz008 Avatar asked Mar 23 '11 13:03

FMaz008


People also ask

How do I check if a SQL query is correct?

Check - The check is a way for you to check if you have written a legal SQL query. Arrow - This is the execute command button. This will send the query to the server and the server will write back the result to you. Square - This is the stop execution command.

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.


2 Answers

Yes, it is the common practice.

It is also the special methods for loading fixtures: http://phpunit.de/manual/current/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.

like image 126
zerkms Avatar answered Sep 30 '22 06:09

zerkms


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://phpunit.de/manual/current/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.

like image 29
edorian Avatar answered Sep 30 '22 05:09

edorian