rkeet
rkeet

Reputation: 3468

PhpUnit not executing via PhpStorm remote interpreter in Docker

The issue I've got is that PhpUnit does not function (properly, something does happen) when just plain clicking "Run" (Shift + F10 on Windows) in PhpStorm.


First up, followed tutorials/setup guides:

So now, pretty much got a working setup, apart from it doesn't.

Testing started at 15:21 ...
[docker://IMAGE_NAME:latest/]:php bin/.phpunit/phpunit-6.5/phpunit --configuration /var/www/html/phpunit.xml.dist --teamcity
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Testing Project Test Suite

Fatal error: Uncaught PDOException: SQLSTATE[HYT00]: [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired in /var/www/html/src/Legacy/Connection/MssqlConnection.php on line 178

PDOException: SQLSTATE[HYT00]: [unixODBC][Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired in /var/www/html/src/Legacy/Connection/MssqlConnection.php on line 178

Call Stack:
    0.0003     393408   1. {main}() /var/www/html/bin/.phpunit/phpunit-6.5/phpunit:0
    0.0571     923544   2. PHPUnit\TextUI\Command::main() /var/www/html/bin/.phpunit/phpunit-6.5/phpunit:17
    0.0571     923656   3. Symfony\Bridge\PhpUnit\Legacy\CommandForV6->run() /var/www/html/bin/.phpunit/phpunit-6.5/src/TextUI/Command.php:148
    0.2019    4269152   4. Symfony\Bridge\PhpUnit\Legacy\TestRunnerForV6->doRun() /var/www/html/bin/.phpunit/phpunit-6.5/src/TextUI/Command.php:195
    0.2158    4697272   5. PHPUnit\Framework\TestSuite->run() /var/www/html/bin/.phpunit/phpunit-6.5/src/TextUI/TestRunner.php:545
    0.2181    4702968   6. PHPUnit\Framework\TestResult->startTestSuite() /var/www/html/bin/.phpunit/phpunit-6.5/src/Framework/TestSuite.php:689
    0.2233    4717824   7. App\Tests\Helper\DeleteDBOnceListener->startTestSuite() /var/www/html/bin/.phpunit/phpunit-6.5/src/Framework/TestResult.php:368
    0.2270    4739216   8. App\Legacy\Connection\MssqlConnection->databaseExists() /var/www/html/tests/Helper/DeleteDBOnceListener.php:55
    0.2270    4739216   9. App\Legacy\Connection\MssqlConnection->findDbFromDSN() /var/www/html/src/Legacy/Connection/MssqlConnection.php:38
    0.2271    4740104  10. PDO->__construct() /var/www/html/src/Legacy/Connection/MssqlConnection.php:178


Process finished with exit code 255

Obviously this reads as: cannot connect to DB. But!

If I log into the Docker instance, and then run the command, it works! Command:

php bin/.phpunit/phpunit-6.5/phpunit --configuration /var/www/html/phpunit.xml.dist

Generates output:

user@hash:/var/www/html# php bin/.phpunit/phpunit-6.5/phpunit --configuration /var/www/html/phpunit.xml.dist
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Testing Project Test Suite
Dropping current database...
.Creating database..
................................................................ 65 / 80 ( 81%)
...............                                                   80 / 80 (100%)

Time: 3.25 minutes, Memory: 56.12MB

OK (80 tests, 336 assertions)

So why, when executing using "Run", does this fail when doing it from PhpStorm? Did I miss a setting?

Upvotes: 8

Views: 2015

Answers (1)

Mat
Mat

Reputation: 2428

Since you asked your question 3 years ago you probably also answered it yourself. However I will leave my answer for people who enter here in future.

The error HYT00 is a timeout error from MSSQL client. This basically means that your client does not have access to the server (or that it responding very slow).

If you configure PHPStorm to use the docker container as an interpreter it's doing exactly this - only using the PHP you have inside the docker container as an interpreter but not running it inside of the container. You are still running it in your host environment, in your host network etc.

Make sure you have access to the database from your host machine and that you are using a correct connection string while running the tests. In your test configuration you may have the internal hostnames of docker (between docker containers you may for example use container names as their hostnames). When running from the host machine you will need an accessible hsotname, like localhost:1433 if you have the ports mapped to host machine.

With this checked the tests should execute correctly.

Just an offtopic - unless you are doing integration tests it's not a good idea to connect to SQL in Unit tests. Better to use mocks:)

Upvotes: 2

Related Questions