LoginSignup
2
4

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リポジトリ

ディレクトリ構成

├── 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フォルダ

以下は接続テスト用、phpunitの動作チェック用のファイルです

index.php

<?php
phpinfo();

sample.php

<?php
declare(strict_types=1);
namespace App;

class Sample
{
    public function hello() {
        return "Hello World";
    }
}

tests/sampleTest.php

<?php

use PHPUnit\Framework\TestCase;
use App\Sample;

class SampleTest extends TestCase
{
    public function testReturnHello()
    {
        $sample = new Sample();
        $result = $sample->hello();

        $this->assertEquals("Hello World", $result);
    }
}

コンテナを起動する

$ docker-compose up -d

ブラウザで localhost に接続

スクリーンショット 2020-03-12 12.53.29.png

ブラウザで localhost:8080 に接続

スクリーンショット 2020-03-12 13.07.28.png

PHPUnit をインストールする

$ docker-compose exec php composer update

テストを実行する

$ docker-compose exec php composer test -- html/tests

スクリーンショット 2020-03-12 13.03.43.png

参考

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