LoginSignup
5
2

More than 3 years have passed since last update.

【PHPUnit】phpunit.xmlを使ってグローバル変数を設定したい

Posted at

PHPUnitを使ってテストコードを書くとき、
phpunit.xml を使って\$_POST, \$_GET, $_ENV (その他いろいろ)などが設定できる。

ディレクトリ構成
 App/
   src/
      Environment.php
   tests/
      EnvironmentTest.php
   vendor/
   composer.json
   phpunit.xml
phpunit.xml
<phpunit>
    <php>
        <env name="ENVIRONMENT" value="localhost" force="true"/>
    </php>
</phpunit>
Environment.php

<?php

class Environment
{
    public static function getEnvironment()
    {
        return \getenv('ENVIRONMENT');
    }
}

EnvironmentTest.php
<?php
require_once __DIR__.'/../src/Environment.php';

use PHPUnit\Framework\TestCase;

class EnvironmentTest extends TestCase
{
    /**
     * @test
     */
    public function xmlで環境変数が設定できている(): void
    {
        $this->assertSame('localhost', Environment::getEnvironment());       
    }
}

phpunit.xmlはカレントディレクトリにないとデフォルトでは読み込まれない

カレントディレクトリにphpunit.xmlがある場合

C:\App> vendor/bin/phpunit tests\EnvironmentTest.php
PHPUnit 8.0.6 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 813 ms, Memory: 4.00 MB

OK (1 test, 1 assertion)

testsディレクトリに移動して実行

C:\App> cd tests
C:\App\tests> ../vendor/bin/phpunit EnvironmentTest.php
PHPUnit 8.0.6 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 1.66 seconds, Memory: 4.00 MB

There was 1 failure:

1) EnvironmentTest::xmlで環境変数が設定できている
Failed asserting that false is identical to 'localhost'.

C:\App\tests\EnvironmentTest.php:13

カレントディレクトリにないXML設定ファイルを読み込む

--configuration, -c

設定を XML ファイルから読み込みます。 詳細は XML 設定ファイル を参照ください。

phpunit.xml あるいは phpunit.xml.dist (この順番で使用します) が現在の作業ディレクト>リに存在しており、かつ --configuration が使われていない場合、設定が自動的にそのファイルから読み込まれます。

指定されているのがディレクトリで、 phpunit.xml あるいは phpunit.xml.dist (この順番で使用します) がそのディレクトリ内に存在する場合、設定が自動的にそのファイルから読み込まれます。

C:\App\tests> ../vendor/bin/phpunit --configuration ../phpunit.xml EnvironmentTest.php
PHPUnit 8.0.6 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 796 ms, Memory: 4.00 MB

OK (1 test, 1 assertion)

XML設定ファイルを読み込まない

--no-configuration

現在の作業ディレクトリにある phpunit.xml および phpunit.xml.dist を無視します。

C:\App\tests> cd ..
C:\App\> vendor/bin/phpunit --no-configuration tests/EnvironmentTest.php
PHPUnit 8.0.6 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 597 ms, Memory: 4.00 MB

There was 1 failure:

1) EnvironmentTest::xmlで環境変数が設定できている
Failed asserting that false is identical to 'localhost'.

C:\App\tests\EnvironmentTest.php:13

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
5
2
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
5
2