16
16

More than 5 years have passed since last update.

php.iniの設定値を自動テストするコマンドツール

Posted at

php.ini の設定を確認するために、PHPUnitを使って自動テストできるコマンドツールを作ってみた。これで手順書+目視確認っていうアナログから脱出する。要件としては、phpunitが入っていること。

以下のファイルを php-ini-check という名前で ~/bin などに保存して chmod +x する。

php-ini-check
#!/usr/bin/env phpunit --colors --no-configuration
<?php

class PhpIniTest extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        echo sprintf('PHP Version: %s', PHP_VERSION), PHP_EOL;
    }

    /**
     * @dataProvider dataForTestIni
     */
    public function testIniDevelop($name, $expect, $message)
    {
        $actual = ini_get($name);

        if ( $actual === false ) {
            $this->markTestSkipped(sprintf("The configuration option doesn't exist: %s", $name));
        }

        $this->assertSame($expect, $actual, $message);
    }

    public static function dataForTestIni()
    {
        return array(
            array('expose_php', '', "Offにする。HTTPヘッダにPHPのバージョンを出す必要がない。"),
            array('display_errors', '1', "Onにする。(開発環境)"),
            array('log_errors', '1', "Onにすること。"),
            array('log_errors_max_len', '4096', "4096 = 目安"),
            array('error_log', '/var/log/php_error.log', "ログの場所を指定する。"),
            array('register_globals', '0', "Offにする。予期せぬセキュリティ的な問題や不具合の原因になる。"),
            array('register_long_arrays', '0', "Offにする。\$HTTP_GET_VARSなどは使わない。"),
            array('magic_quotes_gpc', '0', "Offにする。"),
            array('allow_url_include', '', "デフォルトでOff。必要に応じてプログラムでOnにする。"),
            array('default_charset', 'UTF-8', "UTF-8にする。"),
            array('mbstring.internal_encoding', 'UTF-8', "UTF-8にする。"),
            array('mbstring.language', 'Japanese', 'Japaneseにする。'),
            array('mbstring.substitute_character', 'none', "\"none\"にする。クォーテーションを忘れずに。"),
            array('date.timezone', 'Asia/Tokyo', "Asia/Tokyoをセットする。"),
            array('phar.readonly', '', "Offにする。(開発環境)"),
        );
    }
}

ところで、シェバンって #!/usr/bin/env phpunit --colors --no-configuration みたいな書き方できるのね。今まで #!/usr/bin/env php ぐらいしかやったことなかった。

インストールしたばっかりだとでるわでるわ

$ php-ini-check 
#!/usr/bin/env phpunit --colors --no-configuration
PHPUnit 3.6.10 by Sebastian Bergmann.

PHP Version: 5.3.10
FF.FFFFF.FFFF.F

Time: 0 seconds, Memory: 5.50Mb

There were 12 failures:

1) PhpIniTest::testIniDevelop with data set #0 ('expose_php', '', 'Offにする。HTTPヘッダにPHPのバージョンを出す必要がない。')
Offにする。HTTPヘッダにPHPのバージョンを出す必要がない。
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-
+1

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

2) PhpIniTest::testIniDevelop with data set #1 ('display_errors', '1', 'Onにする。(開発環境)')
Onにする。(開発環境)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-1
+

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

3) PhpIniTest::testIniDevelop with data set #3 ('log_errors_max_len', '4096', '4096 = 目安')
4096 = 目安
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-4096
+1024

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

4) PhpIniTest::testIniDevelop with data set #4 ('error_log', '/var/log/php_error.log', 'ログの場所を指定する。')
ログの場所を指定する。
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-/var/log/php_error.log
+

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

5) PhpIniTest::testIniDevelop with data set #5 ('register_globals', '0', 'Offにする。予期せぬセキュリティ的な問題や不具合の原因になる。')
Offにする。予期せぬセキュリティ的な問題や不具合の原因になる。
Failed asserting that two strings are identical.

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

6) PhpIniTest::testIniDevelop with data set #6 ('register_long_arrays', '0', 'Offにする。$HTTP_GET_VARSなどは使わない。')
Offにする。$HTTP_GET_VARSなどは使わない。
Failed asserting that two strings are identical.

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

7) PhpIniTest::testIniDevelop with data set #7 ('magic_quotes_gpc', '0', 'Offにする。')
Offにする。
Failed asserting that two strings are identical.

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

8) PhpIniTest::testIniDevelop with data set #9 ('default_charset', 'UTF-8', 'UTF-8にする。')
UTF-8にする。
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-UTF-8
+

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

9) PhpIniTest::testIniDevelop with data set #10 ('mbstring.internal_encoding', 'UTF-8', 'UTF-8にする。')
UTF-8にする。
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-UTF-8
+

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

10) PhpIniTest::testIniDevelop with data set #11 ('mbstring.language', 'Japanese', 'Japaneseにする。')
Japaneseにする。
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-Japanese
+neutral

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

11) PhpIniTest::testIniDevelop with data set #12 ('mbstring.substitute_character', 'none', '"none"にする。クォーテーションを忘れずに。')
"none"にする。クォーテーションを忘れずに。
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-none
+

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

12) PhpIniTest::testIniDevelop with data set #14 ('phar.readonly', '', 'Offにする。(開発環境)')
Offにする。(開発環境)
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-
+1

/Users/suin/Dropbox/Suin/Projects/Private/cli-tools/php-ini-check.php:22
/Users/suin/local/php/versions/5.3.10/bin/phpunit:46

FAILURES!
Tests: 15, Assertions: 15, Failures: 12.
16
16
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
16
16