0

i'm kinda noobie in PHPUnit Testing and I need to cover multiple scenarios of a method. The method takes the rows of a table and make decisions based on it, so if I have 0 rows matching my query, it will execute some action, else, it will execute another.

I did the setup of DBUnit and everything is running, but the whole class is running the same getDataSet method so its running the same MySQLXMLDump.

For example:

testScenarioA -> Empty table.

testScenarioB -> Table has data.

I need to each test function on my class load a foo XML. How can I accomplish that?

gfviegas
  • 58
  • 4
  • What is the class? What methods does it have? What datasets are you using? – Can O' Spam Dec 02 '15 at 14:04
  • It doesnt matter what is the class neither the methods it has. The thing is: How can I dynamically load a dataset based on the test function, not the class? – gfviegas Dec 02 '15 at 14:06
  • Without **any** information, we cannot help you. Please add some or you will be unable to get an answer. – Can O' Spam Dec 02 '15 at 14:07
  • What information is required to know that I simply need to load a dataset for each method? I just dont get it... The problem is not on my code, I didnt implemented this yet, im searching for information on how do I do that and I wasnt lucky on the docs, so I went here ofc – gfviegas Dec 02 '15 at 15:54
  • We are not the group of people to give tutorials, we help people fix what's broken. Without anything to work on we cannot do that. If you want a tutorial, go to Google, see also the [on topic](http://stackoverflow.com/help/on-topic) docs – Can O' Spam Dec 02 '15 at 16:04
  • I appreciate your help, thank you. – gfviegas Dec 02 '15 at 16:07

1 Answers1

0

So if you have a dataset in your Unit Test class, you could have the dataset return empty results and some data

public static function dataForTest() {

    return [
        'empty' => [getEmptyDataset()]
        'results' => [getResults()]
    ];
}

Then in the function used for the tests use that with your dataset

/**
 * @dataProvider dataForTest
 */
public function testSyncUser($dataTypes) {
    foreach ($dataTypes as $dataType) {
        // Run tests
    } 
}

Alternatively you could create a helper class to use alongside your unit tests to get data for the tests on the fly. PHPUnit generates all the data sets prior to instantiation of the Test Unit class so sometimes this can be a useful approach.

Jamie S
  • 76
  • 7
  • The main problem is dumping my MySQL database with data. Im using this guy here: https://phpunit.de/manual/current/en/database.html#database.implementing-getdataset – gfviegas Dec 02 '15 at 16:15