LoginSignup
52
55

More than 5 years have passed since last update.

CI環境構築 ~ PHP編 ~

Last updated at Posted at 2014-04-05

PHPの継続的インテグレーション環境を無料で簡単にセットアップする方法

利用するサービス

  • Travis CI(無料のCI環境)
  • CoverAll(Travis CIで実行したコードのカバレッジを閲覧できるサービス)
  • Slack(Travis CIからの通知を受け取るチャットサービス)

事前準備

  1. GitHubアカウントの取得
  2. リポジトリ作成
  3. RubyGemのインストール(Travisコマンドインストールに使用)

Travis CIのアカウント取得とリポジトリ選択

  1. 右上のリンクからGitHubアカウントでサインイン
  2. テストしたいリポジトリをONに変更

テストコード

  • tests/HogeTest.php
  • tests/bootstrap.php
  • Fuga.php

tests/HogeTest.php

  1. 1 + 1 = 2のテスト結果を返す。
  2. Fugaクラスのindexメソッドを呼び出してTrueを確認する。
tests/HogeTest.php
class HogeTest extends PHPUnit_Framework_TestCase {

    function testHoge() {
        $this->assertEquals(2, 1 + 1);
    }

    function testFuga() {
        $fuga = new Fuga();

        $this->assertTrue($fuga->index());
    }
}

tests/bootstrap.php

  1. newされたクラスを呼び出すモジュール
  2. phpunit.xmlでパスを指定
tests/bootstrap.php
function loader($class)
{
    $file = $class . '.php';
    if (file_exists($file)) {
        require $file;
    }
}

spl_autoload_register('loader');

Fuga.php

  1. index():trueを返す。
Fuga.php
class Fuga {

    public function index() {
        return true;
    }
}

設定ファイル

  • tests/phpunit.xml
  • composer.json
  • .coveralls.yml
  • .travis.yml

phpunit.xml

  1. phpunit実行環境の設定ファイル
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
    colors="true"
    bootstrap="bootstrap.php">
    <testsuite name="Hoge">
        <directory>./</directory>
    </testsuite>
    <logging>
        <log type="coverage-clover" target="build/logs/clover.xml"/>
    </logging>
</phpunit>

composer.json

  1. coverallsの設定は php-coverallsを利用させて頂いた。
composer.json
{
    "require-dev": {
        "satooshi/php-coveralls": "dev-master"
    }
}

coveralls.yml

  1. coverallsの設定ファイル
.coveralls.yml
src_dir: ./
coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json

.travis.yml

  1. Travis CIの設定ファイル
  2. php: バージョン指定
  3. before_script: 事前にcomposerをインストール
  4. script: phpunitを実行
  5. after_script: coveralls用モジュールを起動
  6. notifications: slackへの通知設定
.travis.yml
language: php

php:
  - 5.3
  - 5.4
  - 5.5
  - hhvm

before_script:
  - curl -s http://getcomposer.org/installer | php
  - php composer.phar install --dev --prefer-source

script:
  - mkdir -p build/logs
  - phpunit --coverage-clover build/logs/clover.xml --configuration tests/phpunit.xml tests

after_script:
  - php vendor/bin/coveralls -v

notifications:
  slack:
    rooms:
      secure: your_token_key

GitHubにPush

Travis CI

Travis CI - Free Hosted Continuous Integration Platform for the Open Source Community 2014-04-05 11-50-01 2014-04-05 11-51-04 2014-04-05 11-52-21 2014-04-05 11-53-47.jpg
成功画面

  1. .travis.ci.ymlで設定した各バージョンごとにテスト実行
  2. テスト内容の詳細リンク
  3. build passバッジの画像生成リンク
  4. テスト再実行ボタン
  5. テスト結果通知はメールではうっとうしいのでSlackで確認(後述)

Travis-CI---Free-Hosted-Continuous-Integration-Platform-for-the-Open-Source-Community-2014-04-05-11-59-13-2014-04-05-12-00-37.jpg
テスト詳細画面

  1. コマンドラインでPHPUnit実行したときと同じ画面結果
  2. コードカバレッジは見えないので、CoverAllsを利用する

CoverAlls

masashi0127:php | Build #8 | Coveralls - Test Coverage History & Statistics 2014-04-05 12-07-36 2014-04-05 12-07-56.jpg
1. 各バージョンごとのコードカバレッジを閲覧できる

Slack

mamao Slack 2014-04-05 12-12-56 2014-04-05 12-13-26.jpg
1. Travic CIの実行結果へのリンク、GitHubコミット時のcompare画面へのリンク、テスト結果を通知

52
55
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
52
55