3

I'm using headless phpunit tests. I noticed that the first time I do this it pauses and says about installing schema for xxxx - a test database.

If you break stuff enough, you can end up with a corrupted/no-good test CRM database. How do you get a fresh one back?

I tried dropping the test database, but that just caused errors.

Thanks.

2 Answers 2

6

ah-ha!

In your test class edit the setUpHeadless() method which might look at first like this:

  public function setUpHeadless() {                                                                                                                                                                                                           
    // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
    // See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
    return \Civi\Test::headless()
      ->installMe(__DIR__)
      ->apply();

But pass TRUE to apply(), ie

  public function setUpHeadless() {                                                                                                                                                                                                           
    // Civi\Test has many helpers, like install(), uninstall(), sql(), and sqlFile().
    // See: https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
    return \Civi\Test::headless()
      ->installMe(__DIR__)
      ->apply(TRUE);

Ta-daa! And now your tests will run super slow as the schema will be reinstalled every time. But helps you out of a spot, and you can of course remove that param.

1

It's also worth noting that if you're using civicrm-buildkit to create your test sites (highly recommended) you can also rebuild a site with civibuild reinstall.

3
  • Using civi-buildkit is the only way I've gotten tests to run at all, as required files are not present in standards installs. I'm not sure a site rebuild would result in a schema rebuild (?) since I don't think any of the data used to create the comparison hash would have changed. I think I tried that once. Commented Oct 18, 2017 at 6:31
  • I didn't realize you needed a schema rebuild - for that you can always run bin/regen.sh, then do the reinstall. Commented Oct 18, 2017 at 6:38
  • I think what you're suggesting will rebuild the main schema of the civi-buildkit site. When you run headless (the default type) tests a separate new database is created; the original buildkit one is supposedly untouched. I'll check out that command though, thanks Commented Oct 18, 2017 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.