0

I am running a test that extends a BrowserTestBase and indicated a custom module as a dependency. The module has a mymodule_module_preinstal() that is supposed to perform a DB query using Drupal::database().

After wasting some time on figuring out why it does not work I realized that the query is run against the main databse of the installation, not that which I had set up in phpunit.xml.

That's really confusing - why is it working like that and why on earth would I want to work with the main database while running a test which is supposed to be run in an isolated database?

And, most importantly, how do I run the query against the test database?

Excerpt from documentation:

It's terribly important to realize that each test runs in a completely new Drupal instance, which is created from scratch for the test. In other words, none of your configuration and none of your users exists!

Here's code of my test:

class FormTest extends BrowserTestBase
{
    public static $modules = ['mymodule', 'mymodule2'];

    public function testFormPage()
    {
        $response = $this->drupalGet('/mymodule/register');
    }
}

I've also copied phpunit.xml.dist to phpunit.xml and set up this line:

<env name="SIMPLETEST_DB" value="mysql://mydb_tests:password@localhost/mydb_tests"/>

I run my test like this:

cd core
phpunit ../modules/mymodule/tests/src/MyTest.php

I have installed the version of phpunit from core/composer.json globally.

The preinstall hook looks like this:

/**
 * Module preinstall hook
 * @param $module_name
 */
function mymodule_module_preinstall($module_name)
{
    var_dump($module_name);

    print_r(Drupal::database()->query("SHOW TABLES")->fetchAll());
    exit();
}
4
  • Can you share your preinstall and also at least the set up method of your test?
    – Berdir
    Commented Oct 14, 2016 at 20:34
  • Specifically, are you calling parent::setUp() and are you calling it first? Otherwise your code simply runs, setUp() is where all the work happens to install and create your isolated environment.
    – Berdir
    Commented Oct 14, 2016 at 20:35
  • I have added the above details to the question. Thanks.
    – Okneloper
    Commented Oct 15, 2016 at 15:29
  • Just to confirm, I don't override setUp at all as I rely on the module installer to what I need.
    – Okneloper
    Commented Oct 15, 2016 at 15:39

1 Answer 1

1

Show tables doesn't work like that. I doesn't know anything about prefixes, it lists all tables in the database.

The schema API has an API method to list tables, which is DB agnostic and respects the prefix.

8
  • Sorry, what do you mean it doesn't work like that? It does work and returns me a list of tables from my main drupal database. The point being Drupal::database() returns a connection to my site's database, not test database. Looking at errors I got executing other queries, this same connection is used for the test, which is wrong.
    – Okneloper
    Commented Oct 16, 2016 at 11:39
  • Yes, it lists tables. All of them, including those with prefixes.
    – Berdir
    Commented Oct 16, 2016 at 11:42
  • If you could tell me what is this API method to list tables I could test that to prove it's still using wrong onnection (or prove me wrong). Drupal's documentation is useless for looking this up.
    – Okneloper
    Commented Oct 16, 2016 at 11:42
  • Maybe your problem is running tests in the UI or run-tests.sh. If you want it to respect the phpunit configuration, you need to run them with phpunit. run-tests.sh uses a database table prefix. Always wrap all your tables with {} to make sure that they work then.
    – Berdir
    Commented Oct 16, 2016 at 11:43
  • What I mean is Database::getConnection()->schema()->findTables('%');
    – Berdir
    Commented Oct 16, 2016 at 11:45

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.