bobbingwide / oik-batch

Batch interface to WordPress
https://www.oik-plugins.com/oik-plugins/oik-batch
GNU General Public License v2.0
0 stars 0 forks source link

Improve support for PHPUnit testing plugins in situ #11

Open bobbingwide opened 8 years ago

bobbingwide commented 8 years ago

The tests in wordpress-develop-tests assume that everything has been reset to zero. It's a risky business running attempting to run these tests in a live database. If you want to run your PHPUnit tests in situ, then you have to develop them in such a way that the system state can be restored to as close as it was when you started.

Rather than extending WP_UnitTestCase, which includes methods such as reset_post_types() which aren't particularly useful for plugin tests, we should develop a class called BW_UnitTestCase which plugins (and themes) will extend.

This class should contain helper methods to reduce the amount of code needed in the tests. For instance, to test allow-reinstalls we need to confirm the following.

For other plugins that need to create, update and delete data we need methods to reset the system to the required initial state. And a method to define this state in the first place.

The class will be loaded by oik-batch's bootstrap routine and will perform the reset logic if so required.

This issue requires more work than #9, which is one reason why it's a separate issue.

bobbingwide commented 7 years ago

With WordPress 4.7 code, running tests in situ is far worse than risky.

Without looking at the changes to the WordPress unit test code, I upgraded to 4.7 and ran some tests. Before I realised something was very wrong I had managed to empty two of my most important development environments.

Whereas the call to _delete_all_posts() was only in bootstrap.php, which we didn't call when running in situ, the tearDownAfterClass() method of WP_UnitTestCase now calls _delete_all_data() and commits the transaction.

And this is the exact opposite of what we want, which is:

I'm sure I read a solution to this back in August, by the clever people at deliciousbrains.

bobbingwide commented 6 years ago

Since this is still open... it would be nice if we could actually run the tests in wordpress-develop-tests just like any other plugin, using in-situ testing method. If this is not possible, then we should document what is achievable. We’re trying to support running core tests whilst avoiding the possibility of accidentally destroying a DB simply because you ran PHPUnit from the wrong folder.