Updates to our test harnesses
Hi all. I'd like to let you all know of some updates I made to our test harnesses.
New Base Classes
Firstly, there are two new test base classes: BootstrapTestCase and SiteTestCase. These base classes use the BootstrapTrait and SiteTestTrait respectively and make them easier to use. I highly recommend you start your tests from one of our base classes rather than inheriting PHPUnit's default class. Here is a guide:
- Subclass the
VanillaTestCaseif you are writing basic unit tests on classes that don't interact with other classes. This class just provides useful assertions beyond what PHPUnit includes. - Subclass the
BootstrapTestCaseif you are writing functional tests that need the container, but don't need a copy of Vanilla installed. This usually means you aren't accessing the database, but you can if you create the tables in the test yourself. - Subclass the
SiteTestCaseif you need a copy of Vanilla installed for your tests. This class lets you make APIv2 calls with theapi()method. You can also make calls to old controllers with thebessy()method. - The
AbstractAPIV2Testsubclasses theSiteTestCase. This will most likely end up being used less and less as its main methods get pulled up to theSiteTestCase. - Subclass the
ResourceTestif you've made an APIv2 resource that supports al the main CRUD methods in a standard way. TheResourceTestincludes standard tests to get decent coverage of your endpoint with very little effort.
Quality Of Life Improvements
I've made a few quality of life improvements to cut down on common mistakes when writing tests.
- If you inherit one of the above base classes then you can use other test traits and their set up and tear down methods will automatically be called. Just make sure you call parent life cycle methods if you override them in your subclass. It is pretty easy to make a mistake when using the
BootstrapTraitorSiteTestTraitdirectly. - The user session will automatically be backed up and restored in any class that uses the
SiteTestTrait. This means that you can start a new user session in your test and it won't affect other tests. - Bessy will now throw an exception if there are any form errors during the call. This gives some protection where you won't be wondering what went wrong if there was an error in your call. If you want to test against a form error then you can make a call to before your call, or you can pass
TestDispatcher::OPT_THROW_FORM_ERRORS => falseas an option to the call. - The
VanillaTestCase::sprintfCounter()is a nice little method for augmenting records that need unique records such as usernames or group names. When you wrap a row in the method then any occurrence of%swill be replace with a unique counter so that your records won't clash when inserting. - The
VanillaTestCase::assertArraySubsetRecursive()method works well when comparing database records to an expected value. It will order all keys and only compare the subset of expected keys. This works well when you insert a record to the database and want to assert that it matches an expected value, but you don't want to compare against auto-generated fields likeinsertDate.
These utilities were made to make all of our lives easier. If you take the time to learn some of our standard test harnesses I guarantee you will start writing higher quality tests in a much shorter time.
3