iT邦幫忙

0

phpunit 想看 coverage 卻出現重複宣告問題

  • 分享至 

  • xImage

大家好,我遇到一個問題,我想看我測試寫的覆蓋率,但會出現錯誤

➜  ironman git:(master) ✗ php -dxdebug.mode=coverage vendor/bin/phpunit  --coverage-html build/coverage
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.

..........                                                        10 / 10 (100%)

Time: 00:01.871, Memory: 44.00 MB

OK (10 tests, 21 assertions)

Generating code coverage report in HTML format ... PHP Fatal error:  Cannot declare class App\Http\Controllers\api\UserController, because the name is already in use in /Users/jimchien/work/ironman/app/Http/Controllers/api/UserController.php on line 11

Fatal error: Cannot declare class App\Http\Controllers\api\UserController, because the name is already in use in /Users/jimchien/work/ironman/app/Http/Controllers/api/UserController.php on line 11
PHP Fatal error:  Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /Users/jimchien/work/ironman/vendor/laravel/framework/src/Illuminate/Container/Container.php:1089

是說我的UserController重複宣告了,後來我在測試中自己先new UserController後,錯誤訊息變了

public function test1()
    {
        new UserController();
        assertTrue(true);
    }

錯誤訊息變成

➜  ironman git:(master) ✗ php -dxdebug.mode=coverage vendor/bin/phpunit  --coverage-html build/coverage
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.

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

Time: 00:02.102, Memory: 44.00 MB

OK (11 tests, 22 assertions)

Generating code coverage report in HTML format ... PHP Fatal error:  Cannot declare class App\Providers\EventServiceProvider, because the name is already in use in /Users/jimchien/work/ironman/app/Providers/QueryLogServiceProvider.php on line 8

Fatal error: Cannot declare class App\Providers\EventServiceProvider, because the name is already in use in /Users/jimchien/work/ironman/app/Providers/QueryLogServiceProvider.php on line 8
PHP Fatal error:  Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /Users/jimchien/work/ironman/vendor/laravel/framework/src/Illuminate/Container/Container.php:1089

於是我改了phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app/Http</directory>
        </include>
    </coverage>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <!-- <server name="DB_CONNECTION" value="sqlite"/> -->
        <!-- <server name="DB_DATABASE" value=":memory:"/> -->
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

原本include directory 是 ./app 後來改成 ./app/Http

就可以了..., 想問問是發生什麼問題,導致重複宣告

chan15 iT邦新手 2 級 ‧ 2021-09-30 10:22:06 檢舉
你先撇除 coverage,直接跑測試應該就會出問題了吧
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
Terry L.
iT邦研究生 3 級 ‧ 2022-05-24 17:20:25

phpunit.xml 加上屬性 processIsolation="true" 就可以了
不過 tests 會變的跑超慢,因為變成一個 test 是一個獨立的 process 在跑

我要發表回答

立即登入回答