LoginSignup
1
0

More than 5 years have passed since last update.

HumHubでCodeceptionを実行する

Last updated at Posted at 2018-02-14

概要

HumHubにはCodeceptionによるテストコードが実装されている。
このテストコードを実行するための環境構築の手順。

関連

前提

PhantomJSのインストール

Codeceptionを実行するためには、ヘッドレスブラウザが必要になります。
PhantomJSは開発が終わってますが、使い慣れているのでインストールします。

composer.jsonに以下を追記します。
developmentのみにインストールされるようにします。

    "require-dev": {
        "codeception/codeception": "~2.3",
        "yiisoft/yii2-debug": "~2.0.0",
        "yiisoft/yii2-gii": "~2.0.0",
        "yiisoft/yii2-faker": "~2.0.0",
-       "yiisoft/yii2-apidoc": "~2.0.0"
+       "yiisoft/yii2-apidoc": "~2.0.0",
+       "jonnyw/php-phantomjs" : "4.*"
    },

    "scripts": {
        "post-create-project-cmd": [
            "yii\\composer\\Installer::postCreateProject"
-       ]
+       ],
+       "post-install-cmd": [
+           "PhantomInstaller\\Installer::installPhantomJS"
+       ],
+       "post-update-cmd": [
+           "PhantomInstaller\\Installer::installPhantomJS"
+       ]
    },

インストールします。

$ composer update jonnyw/php-phantomjs

PATHを通します。
これで、phantomjsだけでなく、codeceptコマンドへのパスも通ります。

$ echo 'export PATH="/var/www/html/appname/protected/vendor/bin/:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

テスト用DBの作成

テスト用のDBを作成します。

mysql> CREATE DATABASE `humhub_db_test` CHARACTER SET utf8 COLLATE utf8_general_ci;

テスト用DBの権限をユーザーに付与します。

mysql> GRANT ALL ON `humhub_db`.* TO `humhub`@localhost;
mysql> FLUSH PRIVILEGES;

テスト用DBへの接続情報設定

protected/humhub/tests/config/common.php

<?php

return [
    'components' => [
        'db' => [
-           'dsn' => 'mysql:host=localhost;dbname=humhub_test',
-           'username' => 'travis',
-           'password' => '',
+           'dsn' => sprintf('mysql:host=%s;dbname=%s_test', getenv('DB_HOST'), getenv('DB_NAME')),
+           'username' => getenv('DB_USERNAME'),
+           'password' => getenv('DB_PASSWORD'),
            'charset' => 'utf8',
            'attributes' => [
                PDO::ATTR_PERSISTENT => true
            ]
        ],
        'view' => [
            'theme' =>
            [
                'name' => 'HumHub',
                'basePath' => '@webroot/themes/HumHub',
            ],
        ],
        'queue' => [
            'class' => 'humhub\components\queue\driver\Instant',
        ],
    ],
    'params' => [
        'installed' => true,
-       'moduleAutoloadPaths' => ['/home/travis/build/humhub'],
+       'moduleAutoloadPaths' => ['/var/www/html/appname'],
    ]
];

環境変数を追加

DB_HOST=
DB_USERNAME=
DB_PASSWORD=
DB_NAME=

初期設定

tests/config/test.phpの設定

The HUMHUB_PATH is used by your test environment to determine the humhub root path. This is only required for non core module tests and can also be set in your modules test configuration /tests/config/test.php:

この設定は、non core moduleのテストの時のみ必要になるらしい。

  • core module
    • protected/humhub/modules
  • none core module
    • protected/modules

ってことかな?

protected/humhub/tests/config/test.php

<?php

return [
    'fixtures' => [
        'default'
    ],
+   'humhub_root' => '/path/to/my/humhub/root',
];

codeceptionディレクトリの下にあるyiiコマンドに実行権限を付与

$ cd protected/humhub/tests/codeception/bin
$ chmod u+x yii

migrationを実行

$ ./yii migrate/up --includeModuleMigrations=1 --interactive=0
Migrated up successfully.

yii installer/autoを実行

まず、runtime/searchdbディレクトリに書き込み権限を付与

$ sudo chmod 777 /var/www/html/appname/protected/runtime/searchdb/

以下のコマンド実行します。
userテーブルにテスト用ユーザーが作成されたりします。

$ ./yii installer/auto

これでCodeceptionを実行できる環境が整います。

テスト実行

今回は、userモジュールのテストを実行してみます。

まずは、PhantomJSを起動します。
以下のコマンドを実行し、起動したままにします。

$ phantomjs --webdriver=4444

次に、codeception.ymlのある場所でcodeceptコマンドを実行する必要があるので、該当の場所へ移動してから実行します。

$ cd protected/humhub/modules/user/tests
$ codecept run

これでテストが実行されるはずです。

以上

参考

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