8

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?

rkeet
  • 3,406
  • 2
  • 23
  • 49
  • Something you've probably already got setup/tried, but have you setup mySQL so that it accepts remote connections? – Alex Feb 01 '19 at 21:37
  • Yes, (it's MS SQL in this case btw) the database runs in it's own Docker setup. In this case a "duplicate of live". It's a setup for dev that's never been fully setup, hence I'm now trying to fix this. Current test method is to run commands by logging into instances (like my successful bit in the question) instead of "on-change" or "on Run" directly in PhpStorm. – rkeet Feb 01 '19 at 23:07
  • 1
    I have the same issue. Did you find a solution to run from PHPStorm? – keyboardSmasher May 23 '19 at 17:28
  • 2
    @keyboardSmasher No I did not. Have been using Phpunit from inside of the Docker instances. This issue is still open to this day. – rkeet May 23 '19 at 18:03
  • @rkeet - have you raised this issue in the PhpStorm forums? I think the issue may have something to do with the fact it spawns a new container rather than using an existing one.. but I haven't got to the end of this rabbit hole yet. – mikey Sep 18 '19 at 06:46
  • 1
    @mikeyjk not specifically this question no. There are other questions asking for the same or related functionality already present in their Youtrack though, [see here](https://youtrack.jetbrains.com/issues/WI?q=phpstorm%20docker%20phpunit). – rkeet Sep 18 '19 at 07:48

1 Answers1

2

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:)

Mat
  • 2,378
  • 3
  • 26
  • 35