2

I have a phpunit.xml file which works for me for Kernel Base tests and which allows me to connect to a SQLite database. I want to use it for Functional tests also. I am new to Functional tests. My phpunit.xml file looks like this:

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

  <env name="PHP_IDE_CONFIG" value="PHPSTORM"/>

  <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://intellipass:8888/"/>
    <!--Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
    <env name="SIMPLETEST_DB" value="sqlite://localhost//sites/default/files/db.sqlite"/>
    <!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->
    <env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/tmp"/>
  </php>

  <testsuites>
    <testsuite name="Intellipass Functional Test Suite">
      <directory>./modules/custom/intellipass/tests/src/Functional</directory>
    </testsuite>
  </testsuites>

</phpunit>

And my Functional test class looks like this:

<?php

namespace Drupal\Tests\intellipass\Functional;

use Drupal\Tests\BrowserTestBase;

class EncryptionSecretTest extends BrowserTestBase {

  /**
   * Modules to install.
   *
   * @var array
   */
  public static $modules = ['system', 'intellipass'];

  protected function setUp() {
    parent::setUp();
  }

  /**
   * Tests comments in book export.
   */
  public function testRedirect() {
    $authenticatedUser = $this->drupalCreateUser();
    $this->drupalLogin($authenticatedUser);
  }

}

I am execution the test by the following command:

./phpunit-4.8.9.phar --testsuite="Intellipass Functional Test Suite"

Then I am getting this error:

peter@NB32 intellipass (master) $ ./phpunit-4.8.9.phar --testsuite="Intellipass Functional Test Suite"
PHPUnit 4.8.9 by Sebastian Bergmann and contributors.

E

Time: 3.73 seconds, Memory: 10.00Mb

There was 1 error:

1) Drupal\Tests\intellipass\Functional\EncryptionSecretTest::testRedirect
Exception: PDOException: SQLSTATE[HY000] [2002] Connection refused
Drupal\Component\DependencyInjection\PhpArrayContainer->createService()() (Line: 79)


/Users/peter/DevPrivat/intellipass/core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php:44
/Users/peter/DevPrivat/intellipass/vendor/guzzlehttp/promises/src/Promise.php:203
/Users/peter/DevPrivat/intellipass/vendor/guzzlehttp/promises/src/Promise.php:156
/Users/peter/DevPrivat/intellipass/vendor/guzzlehttp/promises/src/TaskQueue.php:47
/Users/peter/DevPrivat/intellipass/vendor/guzzlehttp/promises/src/Promise.php:246
/Users/peter/DevPrivat/intellipass/vendor/guzzlehttp/promises/src/Promise.php:223
/Users/peter/DevPrivat/intellipass/vendor/guzzlehttp/promises/src/Promise.php:267
/Users/peter/DevPrivat/intellipass/vendor/guzzlehttp/promises/src/Promise.php:225
/Users/peter/DevPrivat/intellipass/vendor/guzzlehttp/promises/src/Promise.php:62
/Users/peter/DevPrivat/intellipass/vendor/guzzlehttp/guzzle/src/Client.php:131
/Users/peter/DevPrivat/intellipass/vendor/fabpot/goutte/Goutte/Client.php:155
/Users/peter/DevPrivat/intellipass/vendor/symfony/browser-kit/Client.php:312
/Users/peter/DevPrivat/intellipass/vendor/behat/mink-browserkit-driver/src/BrowserKitDriver.php:144
/Users/peter/DevPrivat/intellipass/vendor/behat/mink/src/Session.php:148
/Users/peter/DevPrivat/intellipass/core/tests/Drupal/Tests/BrowserTestBase.php:333
/Users/peter/DevPrivat/intellipass/core/tests/Drupal/Tests/BrowserTestBase.php:472
/Users/peter/DevPrivat/intellipass/modules/custom/intellipass/tests/src/Functional/EncryptionSecretTest.php:17
/Users/peter/DevPrivat/intellipass/vendor/phpunit/phpunit/src/Framework/TestCase.php:764
/Users/peter/DevPrivat/intellipass/vendor/phpunit/phpunit/src/Framework/TestResult.php:612
/Users/peter/DevPrivat/intellipass/vendor/phpunit/phpunit/src/Framework/TestCase.php:724

FAILURES!
Tests: 1, Assertions: 1, Errors: 1.

The SQLite file definitely exists. I have tried the relative and absolute path.

How can I fix this

Exception: PDOException: SQLSTATE[HY000] [2002] Connection refused

error?

2
  • The test cannot connect to your database; either because your database is not running, it does not accept connections (firewall?) or your path is not right (I'd expect it to look like sqlite://sites/default/files/db.sqlite, but have very little experience with sqlite). – Neograph734 Feb 17 '18 at 15:03
  • Firewall or path cannot be the problem. Does anyone use functional tests with sqlite or uses them in mysql on a mac environment with mamp and can post example config? – jepster Feb 17 '18 at 15:21
1

I have found the solution. Drupal tries to use my default DB connection at first. This connection does not work for the test. Therefor I have placed an if-case around my DB-connection in my settings.php file. Then the DB connection in my phpunit.xml file will be used.

I have extended the testing documentation at https://www.drupal.org/docs/8/phpunit/phpunit-browser-test-tutorial for this (scroll down to the SQLite headline).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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