LoginSignup
1
0

More than 3 years have passed since last update.

Codeceptionでセンシティブな情報を.envから取得する

Last updated at Posted at 2020-08-13

テストコードにパスワードを直書きしたくない

AdminLoginCept.php
$I = new AcceptanceTester($scenario);
$I->wantToTest('管理画面にログイン');

$I->amOnPage('/admin/');
$I->seeCurrentURLEquals('/admin/login');
$I->fillField('login_id', 'hideki');
$I->fillField('password', 'hidekisuteki'); // 👎
$I->click('ログイン');
$I->seeCurrentURLEquals('/admin/');
  • 直書きせずに、環境変数ファイルから取得する方法があるはずと思って調べたのですが、思ったより面倒だったので書き残しておきます。

Codeception Config Module をインストール

Loads params from Codeception config and pass them to test scenarios.

composer require-dev justblackbird/codeception-config-module
composer.json
"require-dev": {
    "codeception/codeception": "^4.1",
    "justblackbird/codeception-config-module": "^1.0",
}

codeception.ymlにパラメーター設定を追加

.env
ADMIN_PASSWORD=hidekisuteki
codeception.yml
paths:
    tests: tests
    output: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
actor_suffix: Tester
extensions:
    enabled:
        - Codeception\Extension\RunFailed
params:
    - .env # 👈
  • これで、.envファイル内の環境変数を設定ファイルの中で使えるようになります(テストコード内ではまだです)。

acceptance.suite.ymlにモジュール設定を追加

acceptance.suite.yml
actor: AcceptanceTester
modules:
    enabled:
        - PhpBrowser:
            url: http://localhost/
        - \Helper\Acceptance
        - Config:
            password: "%ADMIN_PASSWORD%" # 👈
  • .env内の使いたいパラメーターの名前を、%で囲って記述します。

テストコード内でモジュールを使用

AdminLoginCept.php
$I = new AcceptanceTester($scenario);
$I->wantToTest('管理画面にログイン');

$I->amOnPage('/admin/');
$I->seeCurrentURLEquals('/admin/login');
$I->fillField('login_id', 'hideki');
$I->fillField('password', $I->grabFromConfig('password')); // 👍
$I->click('ログイン');
$I->seeCurrentURLEquals('/admin/');
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