Ticket #526 (closed enhancement: fixed)

Opened 2 years ago

Last modified 19 months ago

more flexible dataset filtering in dbunit

Reported by: daniel Owned by: mlively
Priority: trivial Milestone: PHPUnit 3.4
Component: phpunit Version:
Keywords: DbUnit Cc:
PHP Version: 5.2.4

Description

Filtering a dataset is one of those features I use pretty often and it works pretty well. However there are cases, that some more robust dataset filtering would be more handy. Imagine having a table with 15 columns, while you want to only test 2 of them. AFAIK you have to exclude the other 13 by explicitly passing their names to PHPUnit_Extensions_Database_DataSet_DataSetFilter constructor or include all of them in your xml files (php dataset pretty please?:)) It would be much easier to exclude everything and include only the columns we need. So, instead of calling:

...DataSetFilter($dataset, array('tab1' => array('col1', 'col2',[...], 'col13')));

I could call the filter with e.g. additional param

...DataSetFilter($dataset, array('tab1' => '*'), array('tab1' => array('col14', 'col15')));

So in other words, exclude everything ('*') from table tab1, except col14 and col15. Of course, it's just an example how it could be much shorter than the current syntax. For now '*' works a bit different, but I don't see a problem to modify its behaviour in case the 3rd param is defined.

But regardless, any solution that would shorten the dataset filter code would be welcome

What are your thoughts about it?

Thanks

Change History

Changed 2 years ago by sb

  • owner changed from sb to mlively
  • status changed from new to assigned
  • milestone set to PHPUnit 3.4

Changed 2 years ago by mlively

  • status changed from assigned to accepted

Changed 19 months ago by mlively

I have introduced some new functions to the DataSetFilter. You can now set excluded columns or excluded columns.

For instance, if you wanted to specify an include filter you can do the following:

<?php //... $filteredDataSet = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($dataSet);

$filteredDataSet->addIncludeTables(array('table1', 'table3')); $filteredDataSet->setIncludeColumnsForTable('table1', array('column1', 'column2', 'column3', 'column4')); $filteredDataSet->setIncludeColumnsForTable('table3', array('column9', 'column10', 'column11', 'column12'));

//... ?>

I have also deprecated passing the filter to the constructor. The reason for this is that I would like to begin adding a few more features to the filter and the current method of passing the parameters on the constructor would be unintuitive for these planned features.

So, for exclude filters, instead of specifying them in the constructor, the new method will be: <?php //...

$filteredDataSet = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($dataSet);

$filteredDataSet->addExcludeTables(array('table2')); $filteredDataSet->setExcludeColumnsForTable('table1', array('table1_id')); $filteredDataSet->setExcludeColumnsForTable('table3', array('table3_id'));

//... ?>

While you can't mix include and exclude filters for tables (wouldn't make sense right now) you CAN do this for table columns of seperate tables:

<?php //...

$filteredDataSet = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($dataSet);

$filteredDataSet->addIncludeTables(array('table1', 'table3')); $filteredDataSet->setExcludeColumnsForTable('table1', array('table1_id')); $filteredDataSet->setIncludeColumnsForTable('table3', array('column9', 'column10', 'column11', 'column12'));

//... ?>

Changed 19 months ago by mlively

  • status changed from accepted to closed
  • resolution set to fixed

(In [4591]) fix #526

Note: See TracTickets for help on using tickets.