'PhpStorm cannot find vendor/autoload.php on Docker WSL when running PHPUnit
My current setup is: PhpStorm, Docker WSL, Ubuntu (WSL). All my project files are local and inside WSL Ubuntu. Docker Container and CLI Interpreters are set up on PhpStorm.
It seems to recognize PHP inside my 'php-fpm' container (it pulls the PHP info). When I run PHPUnit test it looks like it connects to my container.
If I understand correctly, PhpStorm is mounting my project files from WSL Ubuntu into a new container that uses the Docker Container 'php-fpm' to run the unit tests? But it doesn't seem to mount my project files inside the Unit test container. I get the following error:
Testing started at 11:54 AM ...
[docker://laradock/php-fpm:latest-7.3/]:php /opt/.phpstorm_helpers/phpunit.php --no-configuration --filter Tests\\Feature\\ExampleTest --test-suffix ExampleTest.php /opt/project/tests/Feature
The value of autoloader is specified, but file doesn't exist '/opt/project/vendor/autoload.php'
Process finished with exit code 1
When I manually start up the Docker container (via Docker Desktop) PhpStorm created and get into the bash, there's no files in /opt/project
but has /opt/.phpstorm_helpers/phpunit.php
. Which therein lies the problem. The actual project files that my Docker has mounted from WSL are in /var/www
. I've tried changing the Path Mappings to point to those files but still get the same error.
Any suggestions appreciated.
Solution 1:[1]
As per the comments thread, the Docker WSL PHPUnit testing enviorment is currently unsupported by Phpstorm.
Solution 2:[2]
I struggled with this too, but I do have a workaround. I needed this mainly for package development.
I have a pre-build custom docker image based on the official PHP CLI image with the various modules I need for most of my development.
Create a docker-compose.yml
file with just the PHP service, eg, mine is:
version: "3.7"
services:
php:
container_name: pkg
image: my.private.registry/php7-cli:7.4.10
volumes:
- ./:/opt/project
entrypoint: /bin/bash
tty: true
Run this in WSL with docker-compose up -d
Then when selecting a remote CLI interpreter, select Docker Compose
and select your service:
Choose to connect to an existing container:
In test frameworks, select the remote interpreter we just configured and PHPUnit will show up:
Run the tests and smile =)
A hacky method, especially when creating the likes of packages when you don't need a service running like you would with a site, but for now at least it's usable and you gain the performance benefits of WSL2 rather than hyper-v backends.
Solution 3:[3]
I've found a workaround.
I've just put execution .phar
files to /usr/local/bin/
directory. What I mean. I've just changed my Dockerfile
and added there
RUN curl -L https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar > /usr/local/bin/phpcs \
&& chmod +x /usr/local/bin/phpcs \
&& curl -L https://phar.phpunit.de/phpunit-9.5.phar > /usr/local/bin/phpunit \
&& chmod +x /usr/local/bin/phpunit \
&& curl -L https://phpmd.org/static/latest/phpmd.phar > /usr/local/bin/phpmd \
&& chmod +x /usr/local/bin/phpmd
It downloads and moves .phar files exactly to the /usr/local/bin/
directory. This means that it is callable in any place of the docker container.
Then you can just copy my configuration by screenshots. Make sure that paths match.
- PHP fpm settings
- PHP unit settings
Accordingly, it also works for phpcs, phpmd, etc. Right now, you can just call execution files without slashes
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | l3fty |
Solution 2 | Dharman |
Solution 3 | HariKrishnaRajoli-MT |