How to insert database rows for specific cases using PHPUnit

When using PHPUnit to test a class that relies on a database, the getDataSet () method provides data that will serve as test equipment for the entire test suite. This is great, but how to provide data for a specific test? This is not uncommon, especially when using minimized data sets; for each test, different database data is required on top of the common data used by all tests. I know that a PDO object is available, and in our case, our own application database object is also available (which means that we can run raw queries or use other functions in the application), but it would be nice to have a way to insert data that supplanted PHPUnit DataSet containers so that all test data is processed the same way to improve clarity and simplify maintenance.

Is there any way to do this?

+6
source share
2 answers

This is a late answer, but it may be useful to some people, I think ...

You can accomplish this by calling the execute IDatabaseOperation method, which you can get from PHPUnit_Extensions_Database_Operation_Factory . Basically you would use CLEAN_INSERT or INSERT .

As a second zerkms approach , you would name it at the beginning of every test that needs specific data. For instance:

 public function testA() { PHPUnit_Extensions_Database_Operation_Factory::INSERT() ->execute($this->getConnection(), $this->createXMLDataSet(__DIR__.'/fixtureA.xml')); // Test code } 

However, the advantage of this solution is that the data set of the entire test case remains unchanged, therefore:

  • Test methods remain independent of each other (whereas in the zerkms approach, if you remember to specify the data set in another testing method, it will reuse the data set from the previous (random?) Method, which changed the data set, which is quite ugly and very subject to errors IMHO). Here, any other test without such a call at the beginning will simply use the test case dataset, as usual.
  • You can create a test data set on top of a common data set (test case) . If you use the INSERT operation (not CLEAN_INSERT ), it will insert test-specific rows after all rows from the shared dataset. You can also use the DELETE operation to remove some unwanted rows from this shared dataset.
  • BTW, without calling onSetUp() , does this work, even if the work on setting up a test case has been changed.
+4
source

You can do such a dirty trick:

 protected function getDataSet() { if (in_array($this->getName(), array('testA', 'testB', '...'))) { return $this->createXMLDataSet(__DIR__ . '/_fixtures/fistureA.xml'); } return $this->createXMLDataSet(__DIR__ . '/_fixtures/fixtureB.xml'); } 

A $this->getName() note: $this->getName() returns the name of the current test method

An alternative approach is to restart setup operations at the beginning of the test:

 public function testA() { $this->getDatabaseTester()->setDataSet($this->createFlatXMLDataSet(__DIR__ . '/_fixtures/fixtureForTestA.xml')); $this->getDatabaseTester()->onSetUp(); /* your test code */ } 
+8
source

Source: https://habr.com/ru/post/909018/


All Articles