1. Qiita
  2. 投稿
  3. cakephp3

Gitlab CI を使用して CakePHP3のテストを実行してみた。

  • 3
    いいね
  • 0
    コメント
に投稿

はじめに

Gitlabは使っていましたがCI/CD機能は使ったことなかったので、実際初めてです。

前提

  • Gitlab gitlab.com
  • php 5.5.9
  • cakephp/cakephp 3.3.10
  • cloud9 はオンラインIDEです。

CakePHP3 で簡単なアプリを作成

cloud9 ramp構成でワークスペースを作成

プロジェクトのひな型を作成

 composer create-project --prefer-dist cakephp/app cakephp3gitlabci
 Set Folder Permissions ? (Default to Y) [Y,n]? Y

DocumentRootの設定

/etc/apache2/sites-enabled/001-cloud9.conf
DocumentRoot /home/ubuntu/workspace/cakephp3gitlabci

Git Init

git init
git add -A
git commit -m "first"

インストール状況の確認

Run Project Button Click
Preview Button Click
スクリーンショット

Database setting

cp config/app.php config/app.c9.php

cloud9用のconfig/app.phpを作成してコミットしておきます。

config/app.php
    'Datasources' => [
        'default' => [
            'host' => getenv('IP'),
            'username' =>  getenv('C9_USER') ,
            'password' => '',
            'database' => 'c9',

スクリーンショット

$ cp config/app.php config/app.c9.php
$ git add config/app.c9.php

create migrations

Migrationを作成して実行

$ bin/cake bake migration CreateProducts name:string description:text created modified
$ bin/cake migrations migrate

modelを作成

Bakeコマンドでモデル作成
$ bin/cake bake model Products

ひな形が作成されます
src/Model/Entity/Product.php
src/Model/Table/ProductsTable.php
tests/Fixture/ProductsFixture.php
tests/TestCase/Model/Table/ProductsTableTest.php

model test

簡単なテストを一つ書きます。

tests/TestCase/Model/Table/ProductsTableTest.php
    public function testFind()
    {
        /** @var App\Model\Entity\Product */
        $product = $this->Products->find()->where(['id' => 1])->first();
        $this->assertEquals("Lorem ipsum dolor sit amet",$product->name);
    }

composerでphpunitをインストールして実行

$ composer require --dev phpunit/phpunit
$ vender/bin/phpunit

ここまでで、ローカル環境でテストが実行できる環境になる

GitLab CI のセットアップ

.gitlab-ci.ymlの作成

DockerHubを検索していたら、そのまま使えそうなものがあったので、ここから、コピペします。
https://hub.docker.com/r/kaffineaddict/gitlabcakephp3/

GitLabにpush

$ git remote add origin https://gitlab.com/zonoise/cakephp3_2.git                                                                
$ git push origin master 

最初は失敗します。
https://gyazo.com/168377d9db706c7eef079e60265c5110

configの設定

GitLab用にconfig/app.phpを分けます。

$ cp config/app.default.php config/app.gitlab.php

gitlab-ci.ymlのデータベース情報をもとに接続情報を編集します。(Defaultとtest両方)

config/app.gitlab.php
            'host' => 'mysql',
            'username' => 'root',
            'password' => 'mysql',
            'database' => 'cake_test',

configを分割したので、テストが走る前にcpする処理を追加

gitlab-ci.yml
  - cp config/app.gitlab.php config/app.php
  - bin/cake migrations migrate

終わり

テストが通ったので今回はここまでになります。
スクリーンショット

今回作業したプロジェクトは下記になります。
https://gitlab.com/zonoise/cakephp3_2

この投稿は GitLab Advent Calendar 201618日目の記事です。
Comments Loading...