LoginSignup
1
0

More than 3 years have passed since last update.

CircleCI2.0+BitbucketでLaravel MySQLでテスト設定の備忘録

Posted at

CircleCI2.0+BitbucketでLaravel MySQLでテスト設定の備忘録

まえがき

phpunitでのテストは行っていたがテストの自動実行が出来ていなかったのでCircleCIを使って自動実行するようにした。

前提条件

  • リポジトリはbitbucketを利用
  • Laravel 6.4.x
  • テスト用のデータベース設定は migrateとseedで事前に設定できる様にしてある
  • tests以下にユニットテストを記述し、開発機上ですべてのテストが成功する状態

config.yml

# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
  build:
    shell: /bin/bash

    docker:
      # Specify the version you desire here
      - image: circleci/php:7.2-node-browsers

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # Using the RAM variation mitigates I/O contention
      # for database intensive operations.
      - image: circleci/mysql:5.7-ram
      #- image: circleci/mysql:5.7
      - image: redis:2.8.19

    environment:
      - APP_DEBUG: true
      - APP_ENV: testing
      - APP_KEY: base64:moozahMeihbeebahpeYeGuivireipoVeesahNgah=   # ← Your app key here.
      - DB_CONNECTION: circle_test
      - MYSQL_ALLOW_EMPTY_PASSWORD: true

    #working_directory: ~/repo

    steps:
      - checkout

      - run: sudo apt update # PHP CircleCI 2.0 Configuration File# PHP CircleCI 2.0 Configuration File sudo apt install zlib1g-dev libsqlite3-dev
      - run: sudo docker-php-ext-install zip
      - run: sudo apt-get install libpng-dev

      - run:
          name: Install PHP Extensions
          command: sudo docker-php-ext-install pdo_mysql mbstring gd

      # Download and cache dependencies
      - restore_cache:
          keys:
            # "composer.lock" can be used if it is committed to the repo
            - v1-dependencies-{{ checksum "composer.json" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run: composer install -n --prefer-dist

      - run:
          name: PDF保存フォルダの作成
          command: |
            sudo mkdir -p /var/myapp/pdf /var/myapp/pdf/sengen
            sudo chmod 0777 -R /var/myapp
            sudo chmod 0777 -R /var/myapp/pdf

      - save_cache:
          key: v1-dependencies-{{ checksum "composer.json" }}
          paths:
            - ./vendor

      - run:
          name: Wait for db
          command: dockerize -wait tcp://localhost:3306 -timeout 1m

      - restore_cache:
          keys:
            - node-v1-{{ checksum "package.json" }}
            - node-v1-
      - run: yarn install
      - save_cache:
          key: node-v1-{{ checksum "package.json" }}
          paths:
            - node_modules

      - run:
          name: Migration & Seeding
          # working_directory: laravel
          command: php artisan migrate --seed

      - run: php -dxdebug.coverage_enable=1 ./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml ./tests --teamcity



メモ

  • #working_directory: ~/repo
    build直下のworking_directoryを有効にしておくと他のworking_directoryに影響してうまく動かなかった。

  • run: sudo apt-get install libpng-dev
    QR生成モジュールを使っているのでapt-getでlibpng-devをインストールした

  • 作業フォルダの作成

  • - run:
        name: PDF保存フォルダの作成
        command: |
          sudo mkdir -p /var/myapp/pdf /var/myapp/pdf/sengen
          sudo chmod 0777 -R /var/myapp
          sudo chmod 0777 -R /var/myapp/pdf
    

    テスト実行で作成する作業フォルダをmkdirしてchmod 0777 した

  • command: php artisan migrate --seed

を実行してmigrateとseedを行いテスト用データベースとテストデータを設定

  • run: php -dxdebug.coverage_enable=1 ./vendor/phpunit/phpunit/phpunit --configuration ./phpunit.xml ./tests --teamcity ユニットテストを追加。これで良いのかまだ分からない。とりあえずビルドに失敗するとCircleCIでエラーが発生する様にはなった。

config/database.php


return [
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [
        'circle_test' => [
            'driver' => 'mysql',
            'host' => '127.0.0.1',
            'port' => '3306',
            'database' => 'circle_test',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
      ...以下略

テスト用のMySQLコネクションをdatabase.php に追記した。

結果

bitbucketにpushすると自動でテストを実行して失敗したら通知が届く様になった。

参考

以下の記事を参考にさせて頂きました

1
0
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
1
0