我目前正在使用Symfony 4.2开发REST Api,并使用Docker(php-fpm
)进行部署。
当尝试在内置容器中使用phpunit运行测试(为ci设置)时,它的确会失败,并给出以下输出:
$ docker run api:latest bin/phpunit
PHP Warning: require(/srv/api/bin/.phpunit/phpunit-6.5/vendor/composer/../symfony/phpunit-bridge/bootstrap.php): failed to open stream: No such file or directory in /srv/api/bin/.phpunit/phpunit-6.5/vendor/composer/autoload_real.php on line 66
PHP Stack trace:
PHP 1. {main}() /srv/api/bin/phpunit:0
PHP 2. require() /srv/api/bin/phpunit:19
PHP 3. include() /srv/api/vendor/symfony/phpunit-bridge/bin/simple-phpunit:244
PHP 4. require() /srv/api/bin/.phpunit/phpunit-6.5/phpunit:4
PHP 5. ComposerAutoloaderInitfa3eadee0e53d49c951bc803cfc74f21::getLoader() /srv/api/bin/.phpunit/phpunit-6.5/vendor/autoload.php:7
PHP 6. composerRequirefa3eadee0e53d49c951bc803cfc74f21() /srv/api/bin/.phpunit/phpunit-6.5/vendor/composer/autoload_real.php:56
PHP Fatal error: require(): Failed opening required '/srv/api/bin/.phpunit/phpunit-6.5/vendor/composer/../symfony/phpunit-bridge/bootstrap.php' (include_path='.:/usr/local/lib/php') in /srv/api/bin/.phpunit/phpunit-6.5/vendor/composer/autoload_real.php on line 66
PHP Stack trace:
PHP 1. {main}() /srv/api/bin/phpunit:0
PHP 2. require() /srv/api/bin/phpunit:19
PHP 3. include() /srv/api/vendor/symfony/phpunit-bridge/bin/simple-phpunit:244
PHP 4. require() /srv/api/bin/.phpunit/phpunit-6.5/phpunit:4
PHP 5. ComposerAutoloaderInitfa3eadee0e53d49c951bc803cfc74f21::getLoader() /srv/api/bin/.phpunit/phpunit-6.5/vendor/autoload.php:7
PHP 6. composerRequirefa3eadee0e53d49c951bc803cfc74f21() /srv/api/bin/.phpunit/phpunit-6.5/vendor/composer/autoload_real.php:56
Composer会安装所有必需的软件包,包括dev-packages。这是作曲家的输出:
Package operations: 94 installs, 0 updates, 0 removals
[...]
You are using the deprecated option "dev". Dev packages are installed by default now.
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Prefetching 10 packages � �
- Downloading (100%)
Package operations: 10 installs, 0 updates, 0 removals
- Installing doctrine/data-fixtures (v1.3.1): Loading from cache
- Installing doctrine/doctrine-fixtures-bundle (3.1.0): Loading from cache
- Installing symfony/process (v4.2.3): Loading from cache
- Installing symfony/polyfill-php72 (v1.10.0): Loading from cache
- Installing php-cs-fixer/diff (v1.3.0): Loading from cache
- Installing composer/xdebug-handler (1.3.2): Loading from cache
- Installing composer/semver (1.4.2): Loading from cache
- Installing friendsofphp/php-cs-fixer (v2.14.2): Loading from cache
- Installing fzaninotto/faker (v1.8.0): Loading from cache
- Installing symfony/phpunit-bridge (v4.2.3): Loading from cache
Generating autoload files
ocramius/package-versions: Generating version class...
ocramius/package-versions: ...done generating version class
Executing script cache:clear [OK]
Executing script assets:install public [OK]
Generated autoload files containing 50 classes
错误消息中命名的文件确实存在:
$ ls -al vendor/composer/../symfony/phpunit-bridge
total 88
drwxr-xr-x 6 root root 4096 Feb 21 12:59 .
drwxr-xr-x 1 root root 4096 Feb 21 12:59 ..
-rw-r--r-- 1 root root 34 Feb 21 12:59 .gitignore
-rw-r--r-- 1 root root 1296 Feb 21 12:59 CHANGELOG.md
-rw-r--r-- 1 root root 2687 Feb 21 12:59 ClockMock.php
-rw-r--r-- 1 root root 868 Feb 21 12:59 CoverageListener.php
-rw-r--r-- 1 root root 13954 Feb 21 12:59 DeprecationErrorHandler.php
-rw-r--r-- 1 root root 6013 Feb 21 12:59 DnsMock.php
-rw-r--r-- 1 root root 1065 Feb 21 12:59 LICENSE
drwxrwxrwx 2 root root 4096 Feb 21 12:59 Legacy
-rw-r--r-- 1 root root 500 Feb 21 12:59 README.md
-rw-r--r-- 1 root root 896 Feb 21 12:59 SymfonyTestsListener.php
drwxrwxrwx 4 root root 4096 Feb 21 12:59 Tests
drwxrwxrwx 2 root root 4096 Feb 21 12:59 TextUI
drwxrwxrwx 2 root root 4096 Feb 21 12:59 bin
-rw-r--r-- 1 root root 1330 Feb 21 12:59 bootstrap.php # <-----
-rw-r--r-- 1 root root 1400 Feb 21 12:59 composer.json
-rw-r--r-- 1 root root 914 Feb 21 12:59 phpunit.xml.dist
$ ls -al bin/.phpunit/phpunit-6.5/vendor/composer
total 268
drwxr-xr-x 2 root root 4096 Feb 10 12:03 .
drwxr-xr-x 14 root root 4096 Feb 10 12:03 ..
-rwxr-xr-x 1 root root 13459 Feb 10 12:03 ClassLoader.php
-rwxr-xr-x 1 root root 1070 Feb 10 12:03 LICENSE
-rwxr-xr-x 1 root root 83087 Feb 10 12:03 autoload_classmap.php
-rwxr-xr-x 1 root root 440 Feb 10 12:03 autoload_files.php
-rwxr-xr-x 1 root root 214 Feb 10 12:03 autoload_namespaces.php
-rwxr-xr-x 1 root root 765 Feb 10 12:03 autoload_psr4.php
-rwxr-xr-x 1 root root 2414 Feb 10 12:03 autoload_real.php # <-----
-rwxr-xr-x 1 root root 93179 Feb 10 12:03 autoload_static.php
-rwxr-xr-x 1 root root 48783 Feb 10 12:03 installed.json
在我的(windows)开发环境中执行测试时(通过src/
将srv/api/
目录安装到docker-compose exec php bin/phpunit
上),一切正常。
编辑:
# Run from where i ran ls -al vendor/composer/../symfony/phpunit-bridge
$ pwd
/srv/api
$ ls -la /srv
total 12
drwxr-xr-x 1 root root 4096 Feb 21 13:16 .
drwxr-xr-x 1 root root 4096 Feb 21 13:17 ..
drwxr-xr-x 1 root root 4096 Feb 21 13:17 api
答案 0 :(得分:0)
在Windows Wamp Environment中也遇到了同样的问题(我将项目源从linux复制粘贴到了Windows)。我设法通过删除symfony / phpunit-bridge(删除了composer)解决了问题,然后在项目bin目录中手动删除了.phpunit目录和phpunit文件,然后用composer重新安装了symfony / phpunit-bridge。 希望这会有所帮助。
答案 1 :(得分:0)
ffouillet's answer带我走了正确的路。
即使用作曲家重新安装了symfony/phpunit-bridge
,我也无法通过bin/phpunit
运行测试。我不得不直接调用simple-phpunit
脚本。
最终,我在Dockerfile中为测试映像添加了一个构建步骤,即:
ENTRYPOINT ["./vendor/bin/simple-phpunit"]
CMD ["--coverage-text"]