0

I'm trying to get coverage with pcov & PHPUnit for my Laravel project on docker.

I get an error PHP Fatal error: Uncaught Error: Call to undefined method Illuminate\Container\Container::basePath() when I add --coverage-text. No error occurs without --coverage-text.

What should I do to fix this?

  • Case with --coverage-text
# php -d pcov.enabled=1 ./vendor/bin/phpunit --coverage-text tests/Unit/path/to/my/file
PHPUnit 8.5.9 by Sebastian Bergmann and contributors.

PHP Fatal error:  Uncaught Error: Call to undefined method Illuminate\Container\Container::basePath() in /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:192
Stack trace:
#0 /var/www/html/laravel/app/Some/MyClass.php(5): base_path('vendor/autoload...')
#1 /var/www/html/laravel/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(955): include_once('/var/www/html/...')
#2 /var/www/html/laravel/vendor/phpunit/php-code-coverage/src/CodeCoverage.php(239): SebastianBergmann\CodeCoverage\CodeCoverage->initializeData()
#3 /var/www/html/laravel/vendor/phpunit/phpunit/src/Framework/TestResult.php(646): SebastianBergmann\CodeCoverage\CodeCoverage->start(Object(Tests\Unit\Path\To\My\File))
#4 /var/www/html/laravel/vendor/phpunit/phpunit/src/Framework/TestCase.php(763): PHPUnit\Framework\TestResult->run(Object(Tests\Unit\Path\To\My\File))
#5 /var/www/html/laravel/vendor/php in /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 192
  • Case without --coverage-text
# php -d pcov.enabled=1 ./vendor/bin/phpunit tests/Unit/path/to/my/file
PHPUnit 8.5.9 by Sebastian Bergmann and contributors.

...........                                                       11 / 11 (100%)

Time: 45.6 seconds, Memory: 64.50 MB

OK (11 tests, 291 assertions)

Versions:

  • PHP: 7.4.29
  • Laravel: 6.20.3
  • pcov: 1.0.11
  • Xdebug: 3.1.4

I installed pcov with pecl in Dockerfile.

RUN yes | pecl install pcov && docker-php-ext-enable pcov
Yusuke
  • 429
  • 4
  • 4
  • 1
    Check `app/Some/MyClass.php` you may be trying to use `base_path` at a point where you're not sure whether or not the framework has been booted properly – apokryfos May 30 '22 at 14:05
  • Isn't that same for the case without `--coverage-text`? – Yusuke May 31 '22 at 01:42
  • 1
    coverage will load all files pre-emptively so some things that normally don't pop up as errors might pop up if you didn't design them with the intention of being loaded out of your expected oreder. – apokryfos May 31 '22 at 05:41
  • I use `__DIR__` instead of `base_path()` and the error is now cleared. Thank you! – Yusuke May 31 '22 at 10:57

0 Answers0