symfony/http-kernelをphpstorm&dockerでphpunit&xdebug

[1.ソースのダウンロード](#1-ソースのダウンロード)
[2.dockerの設定](#2-dockerの設定)
[3.phpstormの設定](#3-phpstormの設定)

#1. ソースのダウンロード
```
git clone https://github.com/symfony/http-kernel.git
```
#2. dockerの設定
```
cd ./http-kernel
mkdir docker
touch docker/Dockerfile
touch docker/php.ini
touch docker-compose.yml
```
```Dockerfile:Dockerfile
FROM php:7.4

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y git
```

```ini:php.ini
; timezone
date.timezone = Asia/Tokyo

; error reporing
log_errors = On
error_log = /dev/stderr

; xdebug
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
```

```yml:docker-compose.yml
version: "3"
services:
  php-cli:
    build: ./docker
    volumes:
      - ./:/var/www/html
      - ./docker/php.ini:/usr/local/etc/php/php.ini
    working_dir: /var/www/html
  composer:
    image: composer
    volumes:
      - ./:/app
      - ./docker/php.ini:/usr/local/etc/php/php.ini
    working_dir: /app
```

#3. Composer Install
```
docker-compose run composer install
docker-compose run composer require --dev symfony/phpunit-bridge
```
ちなみにphpunit/phpunitは使いません。[^1]
>The PHPUnit bridge is designed to work with all maintained versions of Symfony components, even across different major versions of them. You should always use its very latest stable major version to get the most accurate deprecation report.

使うとエラーが出ます。

```shell
docker-compose run php-cli ./vendor/bin/phpunit Tests/
[25-Oct-2020 07:28:39 Asia/Tokyo] PHP Fatal error:  Trait 'Symfony\Bridge\PhpUnit\ExpectDeprecationTrait' not found in /var/www/html/Tests/Controller/ContainerControllerResolverTest.php on line 21
```

## 注意
```
docker-compose run composer ./vendor/bin/simple-phpunit
```
を一度実行させておくことで、symfony/phpunit-bridgeからphpunit/phpunitをインストールさせておく


#3. phpstormの設定

## File > Settings > Languages & Frameworks > PHP

phpstormがdockerのphpを実行するように設定します

CLI interpreterの右端にある...をクリック
![Screenshot from 2020-10-25 07-33-45.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/320131/aee3f78e-787e-e6da-1363-d1974e7938fd.jpeg)

追加の+ボタンクリックして、From Docker,...を選択
![Screenshot from 2020-10-25 07-34-02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/320131/96411270-e47b-7217-5127-2c646fd519e4.png)

Docker-composeにチェックを入れ、dockerから実行させるphpのservice(この場合php-cli)を選択
![Screenshot from 2020-10-25 07-34-26.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/320131/ec143324-523a-4de4-3d01-80a39afaf7a4.png)

OK押して、phpが読み込まれてversionなどが表示されてれば問題ありません
![Screenshot from 2020-10-25 07-34-43.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/320131/862db34d-c76d-6a57-49b8-d444eee9b1db.png)

# Phpunit
## File > Settings > Languages & Frameworks > PHP > Test Frameworks

phpstormがdocker内のphpunitを実行するように設定します

追加の+ボタンから PHPUnit by Remote Interpreterを選択
![Screenshot from 2020-10-25 07-37-21.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/320131/26e45719-f669-1e94-ba46-4c2a122f66ed.png)

php-cliを選択
![Screenshot from 2020-10-25 07-37-40.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/320131/ae9ded1c-bd52-584c-a5b3-e969a9f502bd.png)

1. PHPUnit library の Path to phpunit.phar にチェック入れる
2. Path to phpunit.pharに ```/var/www/html/vendor/bin/simple-phpunit```
3. Test Runner の Default configulation file にチェック入れ、```/var/www/html/phpunit.xml.dist```

![Screenshot from 2020-10-25 10-00-25.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/320131/96ad2b7f-308a-a217-c30a-68c162fe3002.png)

# いざ実行
editor右上の虫のアイコン左のセレクタでedit configurationを選択
画面が開いたら追加の+ボタンからPhpunitを選択し、下記のように設定
![Screenshot from 2020-10-25 09-56-18.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/320131/fa03f32e-a5f1-e98b-921d-163a77146de5.png)


breakpointを貼って、simple-phpunitが選択されている状態で虫アイコンをクリック

![Screenshot from 2020-10-25 08-30-20.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/320131/ecbedccd-adc7-0aad-2744-4afe8aabb298.png)




[^1]: https://symfony.com/doc/current/components/phpunit_bridge.html#installation