EC-CUBE / EC-CUBE/ec-cube

EC-CUBE4.2.0 以降で、プラグインの PHPUnit をローカル環境で実行するとデータベース接続に失敗する

Open
#5,954 1 comment 0 reactions 0 assignees View on GitHub
bug bug:Low
Dominant language
PHP
Stars
788
Forks
719
Avg merge
4d 4h
Merged PRs (30d)
39

Description

### 概要(Overview)

EC-CUBE4.2.0 以降で、プラグインの PHPUnit をローカル環境で実行するとデータベース接続に失敗してエラーになってしまう。
SQLite3 を使用した場合は問題ない

### 再現手順(Procedure)

```
$ bin/console eccube:composer:require ec-cube/productreview42
$ bin/console eccube:plugin:enable --code=ProductReview42
$ vendor/bin/phpunit app/Plugin/ProductReview42/Tests/Web/ProductReviewConfigControllerTest.php

PHPUnit 9.5.27 by Sebastian Bergmann and contributors.

Warning: Your XML configuration validates against a deprecated schema.
Suggestion: Migrate your XML configuration using "--migrate-configuration"!

Testing Plugin\ProductReview42\Tests\Web\ProductReviewConfigControllerTest
EEEE 4 / 4 (100%)

Time: 00:00.452, Memory: 52.50 MB

There were 4 errors:

1) Plugin\ProductReview42\Tests\Web\ProductReviewConfigControllerTest::testRouting
Symfony\Component\Routing\Exception\RouteNotFoundException: Unable to generate a URL for the named route "product_review42_admin_config" as such route does not exist.
```

環境変数 `DATABASE_URL` が取得できず、プラグインがロードできないためルーティングが見つからない模様。

原因は以下のコード
https://github.com/EC-CUBE/ec-cube/blob/4.2/tests/bootstrap.php#L20

この箇所のみ symfony/dotenv を使用しているが、 Symfony5 より .env の環境変数が [putenvへ反映されなくなった](https://github.com/symfony/dotenv/blob/fcedd6d382b3afc3e1e786aa4e4fc4cf06f564cf/Dotenv.php#L41-L43)ため。

以下のように修正するか、
```diff
diff --git a/src/tests/bootstrap.php b/src/tests/bootstrap.php
index ba106da6..084ebca3 100644
--- a/src/tests/bootstrap.php
+++ b/src/tests/bootstrap.php
@@ -17,5 +17,7 @@ require dirname(__DIR__).'/vendor/autoload.php';
if (file_exists(dirname(__DIR__).'/config/bootstrap.php')) {
require dirname(__DIR__).'/config/bootstrap.php';
} elseif (method_exists(Dotenv::class, 'bootEnv')) {
- (new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
+ (new Dotenv())
+ ->usePutenv(true)
+ ->bootEnv(dirname(__DIR__).'/.env');
}
```

`getenv()` 及び `env()` の使用をやめる必要がある。
Symfonyでは `$_ENV` または `$_SERVER` の使用が推奨されている

### 環境(Environment)

EC-CUBE4.2.0
PHP8.1
DB:
- MySQL8.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.