2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

docker-composeでPHP+MySQLの環境構築、composerでphpunitインストール

Posted at

はじめに

dockerでphp + apache + mariadb の環境を構築して、 phpmyadmin、xdebugおよびcomposerを使ってphpunitをインストールします

[下記のgithubリポジトリ](https://github.com/jyef/project) ## ディレクトリ構成 ``` ├── composer.json ├── docker-compose.yml ├── db └── my.cnf ├── html │   ├── index.php │   ├── sample.php │   ├──tests │     └── sampleTest.php ├── php    ├── Dockerfile └── php.ini ``` ## docker-compose.yml ``` version: "3"

services:
php:
build: ./php/
tty: true
stdin_open: true
volumes:
- ./php/php.ini:/usr/local/etc/php/php.ini
- ./:/var/www/
ports:
- 80:80
db:
image: mariadb:latest
environment:
MYSQL_ROOT_PASSWORD: root
TZ: 'Asia/Tokyo'
volumes:
- ./db/data:/var/lib/mysql
- ./db/my.cnf:/etc/mysql/conf.d/my.cnf
ports:
- 3306:3306
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- PMA_ARBITRARY=1
- PMA_HOST=db
- PMA_USER=root
- PMA_PASSWORD=root
depends_on:
- db
ports:
- 8080:80
volumes:
- "./phpmyadmin/sessions:/sessions"

## composer.json

{
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"scripts": {
"test": [
"phpunit"
]
},
"autoload": {
"psr-4": {
"App\": "html/"
}
}
}

## phpフォルダ
### Dockerfile

FROM php:7.4.3-apache

RUN apt-get update && apt-get install -y
mariadb-client vim zip
&& pecl install xdebug
&& docker-php-ext-enable xdebug
&& docker-php-ext-install pdo_mysql
&& apt-get clean

install composer

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

WORKDIR /var/www

### php.ini

[Date]
date.timezone = "Asia/Tokyo"
[mbstring]
mbstring.internal_encoding = "UTF-8"
mbstring.language = "Japanese"
[xdebug]
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_host = "host.docker.internal"
xdebug.remote_port=9000
xdebug.remote_log = /var/log/xdebug.log

## dbフォルダ
### my.cnf

[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
default_authentication_plugin=mysql_native_password

[client]
default-character-set=utf8mb4


## htmlフォルダ
<p>以下は接続テスト用、phpunitの動作チェック用のファイルです</p>
### index.php
hello(); $this->assertEquals("Hello World", $result); } } ``` ## コンテナを起動する ``` $ docker-compose up -d ``` ### ブラウザで localhost に接続 ![スクリーンショット 2020-03-12 12.53.29.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/552987/a8e84414-7e5f-4835-93c9-1acf1b4551a5.png) ### ブラウザで localhost:8080 に接続 ![スクリーンショット 2020-03-12 13.07.28.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/552987/9f13a8de-5a2d-770f-beb1-ed87bf3af51e.png) ## PHPUnit をインストールする ``` $ docker-compose exec php composer update ``` ### テストを実行する ``` $ docker-compose exec php composer test -- html/tests ``` ![スクリーンショット 2020-03-12 13.03.43.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/552987/8e4a744a-d564-57d2-4276-dc56e6f92c7c.png) ## 参考 - [PHP で遊ぶための環境を構築してみた](https://yudy1152.hatenablog.com/entry/2019/04/13/111709) - [PHPUnitでテストコードを書いて実行するまで](https://nakawork.net/posts/try-phpunit) - [docker-composeでMySQL8.0とphpMyAdmin環境構築](https://qiita.com/hirokiseiya/items/04ad429713be7042c9d1)
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?