Sunday, September 2. 2007PHPUnit Database Extension (DBUnit Port)Trackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
I love it!
> For large test suites you may want to use a static singleton method of a test utility class to help get rid of some of the overhead of opening and closing connections
With a single connection there is a (small) risk that connection state left behind by one test might contaminate the next. More here: http://aperiplus.sourceforge.net/testing-data-access-classes.htm
That is a good point and definantly something that should be kept in mind when working with your test suites. It also makes another good point of the fact that it is safest to use a seperate connection for your fixture. Again because of the potential of auto insert IDs, affected rows, current errors etc.
Nice addition!
I wonder if inheritance (dbtestcase extending the regular one) is the Right Thing; I think people would need to use databases in existing testcases etc. Probably using a composition to do this would be cleaner. Is it also possible to use a 'fixture' like approach such as is common in ruby? Or php-based datasets instead of XML? That would make it easier to create pseudo-recent datasets such as 'create a record with its date set to yesterday'.
I am actually planning on slipping a method to add DB tests to existing test cases in before the 3.2 release. That is actually the purpose of the PHPUnit_Extensions_Database_*DatabaseTester classes.
I read a little bit about ruby fixtures and I am not sure what aspects of it you are wanting to use. I do have plans for adding a csv format to the dataset types as well as a composite dataset (to allow for combining multiple datasets.) I could also add a ruby fixture as a potential data set style. As far as loading fixtures in the ruby style, I don't know that I would want to depart that much from how PHPUnit already does things. If you have some more info that would helpful in understanding ruby fixtures that would help. Lasty there is sort of a way to build datasets in php using the DefaultDataSet/DefaultTable/DefaultTableMetaData classes. I will probably refine this to allow using a multi-dimensional array to create these data sets. IE $phpDataSet = $this->createPHPDataSet(array( 'table1' => array( array('col1' => 1, 'col2' => 2, 'col3' => 3), ... ), ... )); Thank you for the feedback it is extremely useful. If you have any other ideas feel free to open tickets at the PHPUnit Trac: http://www.phpunit.de/newticket
In ruby it's common to use 'YAML' as format for the fixtures; but the format isn't really that important. The important feature of fixtures is that you can have 'named sets'. For example, you could create 2 fixtures:
account: balance: 100 account_negative: balance: -100 When running testcases that do tests on accounts that have a negative balance, you'd tell your testcase to load 'account_negative' and your other testcases to use 'account'. This way, you can create specific sets of testdata for specific situations.
Great addition, thank you very much. Can't wait to start using it for our tests.
Is there a way (in this extension) that I easily can produce the XML-dataset seed from an existing database?
Not yet, but this is one of the features I have planned for the near future.
I wanted to be able to do this for use in our projects now so I have subclassed PHPUnit_Extensions_Database_DB_FilteredDataSet to create a new class WriteableFilteredDataset. To this I added a method writeFlatXmlDataset to write out an XML file that adheres to the FlatXMLDataSet XML structure. You can then load this using the standard Database TestCase delivered in Mike's work (ie using the createFlatXmlDataSet method).
I also subclassed PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection to create a new class SeedDatabaseConnection and added a new method called createWriteableDataSet which returns an instance of the aforementioned subclass WriteableFilteredDataset. Props to you Mike for keeping it extensible! So in your test suite you can create a new SeedDatabaseConnection object that connects to a seed database containing tables full of extractable data. Then when you want to, in your test cases you can extract the data you want out from the seed database into xml files and then load those xml files into the database that your tests are running against. So I guess my idea is to have this seed database that contains a clone of data from the production install. Then if you need some test data to run a certain test against you go into the seed database, refine that set of test data manually using SQL, then extract that refined data to xml files that your tests will use to set up their fixtures. Then you can refresh the seed database again from production and repeat the process to capture different test data sets when you need to. Does that make sense? If anyone is interested in the aforementioned subclasses I can send em...I'm contactable via the contact link on the workingsoftware website. cheers, dan
This is going to be a really useful addition to PHPUnit. I'm glad to see this get rolling.
Is there any significant advantage of using XML files to define the datasets, versus plain old PHP arrays? Personally, I would prefer just including PHP arrays instead. Also, I'm not familiar with Ruby and its testing systems, but I do like the idea of the named data sets mentioned by Ivo Jansch, above.
Hi, Is there any possibility to use PHPUnit for FTP
so I can test the connection and other stuff before using the real one. Greets ken,
This is an excellent extension to PHPUnit. I am impressed that it provides a mechanism to compare database contents with an expected dataset. That is a really great functionality!
It's probably me being stupid, but...
I don't really get it. You create a PDO-object and then you send it to the function you are testing. I am not prepared to rewrite my project to make all functions accept a PDO-object. What am I missing?
Can you give an example of what BankAccount() and createTable looks like?
you don't have to use a pdo object in the code you are testing. In fact it is preferred (imo) that you use a completely different connection anyways. I just used the same connection for brevity of the example.
The only requirement for PDO is for the setting up of the tests themselves. You are not required to use PDO in your code.
Ahh, you use the same object for both the system and the test.
Thanks for the answer! PS: When are you and Sebastian going to update the handbook? http://www.phpunit.de/pocket_guide/3.2/en/database.html |
Paying for the SiteQuicksearchCategoriesDaily Readsadam trachtenberg
Ben Ramsey Chris Shiflett Dynamically Typed (Harry Fuecks) george schlossnagle John Coggeshall John Lim Marco Tabini PHP Application Tools PHPCommunity.org Planet PHP Sebastian Bergmann Sklar The PHP WTF Tobias Schlitt Tom Sommer Wez Furlong |
