Sunday 21 November 2010 2:19:21 pm
By : Jean-Luc Chassaing
Ok, I 've been taken by other problems but it's time for me to give you the last post and yet the most important part of three, dedicated to unit testing on eZ Publish.
In the two previous posts, I've detailed how to get a unit testing environment and in the second one I gave you some clues to begin writing your first tests.
the two previous posts on the subject are :
It's time for me to give you a few time saving clues. Don't think you'll find here all about unit testing. My goal is to help go true some problems I've had to face and save your time by the way.
I'd like to come back to the suite.php script of my previous post.
class MyFirstTestSuite extends ezpDatabaseTestSuite { public function __construct() { parent::__construct(); $this->insertDefaultData= false; $this->sqlFiles= array('my_schema.sql','my_data.sql'); $this->setName( "My Own Test Suite" ); $this->addTestSuite( 'MyClassTest' ); } public static unction suite() { return new self(); } }
So imaging you've written your own extension and as you make some special things out of ez, your written a few classes and you want to perform unit testing on them. So you write your test cases and register them in a test suite. As you make real special thing you need to have access to the database. And to do so, you've used the ezpDatabaseTestSuite and ezpDatabaseTestCase for the suite class and the test cases.
Let's see how we'll run the test :
php test/runtests.php -D mysql://user@localhost/mydb extension/myextension
ok so here we tell runtests wich database to use to perform the tests. Here is my firs advise. Just use an empty database for your tests. When you starts performing a test sequence using a database connection, one of the first things that is done is to set up the database injecting the defaut system schema and database scripts. Just for memory does scripts are located in kernel/sql/...
So that leads us back to the suit.php script just above. In fact it would be good to have your own database (with your bran new classes) injected in the tests dedicated database. So here's my second advise use those two lines :
$this->insertDefaultData= false; $this->sqlFiles= array('my_schema.sql','my_data.sql');
the first line just tells that we don't want the default database to be loaded and so we give our specific sql scripts.
So now things are rather good. You can play all your Unit test as long as you want and get them performed on your database.
Let's keep going on the datase stuff. know you have some tests written and there's a new one to come that need a specific table. And in fact you don't want to rebuild new schema and data sql exports.
In this case, you can add the next line in your test case script :
ezpTestDatabaseHelper::insertSqlData( $this->sharedFixture, array('extension/gcusermanager/sql/mysql/schema.sql') );
Just take care to add a blank line at the end of all your sql scripts. And by the way I'd like to thanks Bertrand for his posts here who where helpful.
An other thing very useful when writing tests is to have data provider functions. If you want them to work you'll have to take care of how you write your test and suite class constructors.
so for your testCase it should look like something like that :
public function __construct($name = NULL, array $data = array(), $dataName = '') { parent::__construct($name,$data,$dataName);
...
Well .. it look like this post suffers of a lack of structure. However, I hope it will help does of you who, as I did, want to perform unit test on their scripts.