概要
HumHubにはCodeceptionによるテストコードが実装されている。
このテストコードを実行するための環境構築の手順。
関連
前提
- HumHUbが、/var/www/html/appname/に設置されていること
- Protected、その他dotfileを公開ディレクトリより上に設置されていること
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
これでテストが実行されるはずです。
以上