2

My sites directory looks like this:

default, site1, site2, site3.

The default directory is empty. I want to run tests against my site1 database. So I have put its details in phpunit.xml

Here is my phpunit.xml set up.

  <php>
    <!-- Set error reporting to E_ALL. -->
    <ini name="error_reporting" value="32767"/>
    <!-- Do not limit the amount of memory tests take to run. -->
    <ini name="memory_limit" value="-1"/>
    <!-- Example SIMPLETEST_BASE_URL value: http://localhost -->
    <env name="SIMPLETEST_BASE_URL" value="http://d8.site1.dev"/>
    <!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
    <env name="SIMPLETEST_DB" value="mysql://site1:site1@localhost/site1"/>
    <!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->
    <env name="BROWSERTEST_OUTPUT_DIRECTORY" value=""/>
  </php>

And then my actual test:

<?php

namespace Drupal\Tests\my_module\Functional;

use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;

/**
 * Simple test to ensure that main page loads with module enabled.
 *
 * @group my_module_tests
 */
class LoadTest extends BrowserTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['my_module'];

  /**
   * A user with permission to administer site configuration.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $user;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->user = $this->drupalCreateUser(['administer site configuration']);
    $this->drupalLogin($this->user);
  }

  /**
   * Tests that the home page loads with a 200 response.
   */
  public function testLoad() {
    $this->drupalGet('node/add/product_list_page');
    $this->assertResponse(200);
  }

}

Then from the core directory of Drupal I run:

../../vendor/bin/phpunit --group my_module_group

Then I get this error:

1) Drupal\Tests\my_module\Functional\LoadTest::testLoad copy(/var/www/site/docroot/sites/default/default.settings.php): failed to open stream: No such file or directory

Why is it looking in my empty default directory and not site1 for this file?

1 Answer 1

0

It is possible to have a multisite directory setup with PHP Unit.

Since your default directory is empty, I suggest creating a symbolic link from the default directory to the site1 directory. You can do that by using commands similar to this one from sites directory.

ln -s sites1 default

Not sure, but you may also need to adjust the $sites variable in sites.php.

Also, make sure you specify a printerClass like this in the last line.

<phpunit bootstrap="tests/bootstrap.php" colors="true"
         beStrictAboutTestsThatDoNotTestAnything="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutChangesToGlobalState="true"
         checkForUnintentionallyCoveredCode="false"
         printerClass="\Drupal\Tests\Listeners\HtmlOutputPrinter">

Here is what my php tag looks like in phpunit.xml (I use SQLite though and specify an output directory.) You may need to adjust directory permissions so that it is writable. I test on my remote machine with a FQDN, not sure if that is ideal though.

<php>
    <!-- Set error reporting to E_ALL. -->
    <ini name="error_reporting" value="32767"/>
    <!-- Do not limit the amount of memory tests take to run. -->
    <ini name="memory_limit" value="-1"/>
    <!-- Example SIMPLETEST_BASE_URL value: http://localhost -->
    <env name="SIMPLETEST_BASE_URL" value="https://dev.example.com"/>
    <!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
    <env name="SIMPLETEST_DB" value="sqlite://dev.example.com/sites/dev.example.com/files/.ht.sqlite"/>
    <!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->
    <env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/path/to/document/root/sites/dev.example.com/files/simpletest/browser_output"/>
</php>

Also, I run the phpunit command from the Drupal core directory like this (I do not use group ID but I know it may be possible.

sudo -u www-data ../vendor/bin/phpunit ../modules/contrib/example/tests/src/Functional/ExampleHomepageTest.php

Here is what my test looks like. It will need the correct directory structure and classname.

<?php

namespace Drupal\Tests\example\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Tests to ensure Example.com Homepage is reachable
 *
 * @group example_ui
 */
class ExampleHomepageTest extends BrowserTestBase {

/**
 * Modules to enable.
 *
 * @var array
 */
protected static $modules = ['node', 'user'];

protected $profile = 'standard';

/**
 * {@inheritdoc}
 */
public function setUp() {
parent::setUp();

}

/**
  * Tests that the reaction rule listing page works.
  */
public function testHomepage() {

  $account = $this->drupalCreateUser(array('administer site configuration'));
  $this->drupalLogin($account);

  $this->drupalGet('<front>');
  $this->assertSession()->statusCodeEquals(200);
  $this->assertSession()->pageTextContains('cleaning');
 }
}

This page will be useful: https://www.drupal.org/docs/8/phpunit

3
  • If you have to create a symlink from default to the multisite then that must mean phpunit is using sites/default and you can effectively only test against one site, not multiple sites. This is what I am running into right now and it appears phpunit is only looking in sites/default, no matter the URI passed. I will look at setting, sites.php too, that is a good next step. Commented Aug 7, 2019 at 4:12
  • sites.php isn't directing the incoming phpunit request to the correct folder after updating sites.php. The hostname was already the same as the sites/foldername. Commented Aug 7, 2019 at 4:17
  • We are using <phpunit bootstrap="./docroot/vendor/weitzman/drupal-test-traits/src/bootstrap.php`> so that is a variable. Commented Aug 7, 2019 at 4:18

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.