0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHPUnit導入・使用方法

0
Last updated at Posted at 2025-12-20

1. 導入

さくっとPHPUnitを試したかったのでXAMPPを使いテストした。
・XAMPP
https://www.apachefriends.org/jp/index.html
・PHP8.2.4
・Mac

% /Applications/XAMPP/xamppfiles/bin/php -v
PHP 8.2.4 (cli) (built: Apr  6 2023 04:12:41) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
   with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies

2. コンポーザーインストール

% /Applications/XAMPP/xamppfiles/bin/php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
% /Applications/XAMPP/xamppfiles/bin/ php -r "if (hash_file('sha384', 'composer-setup.php') === 'c8b085408188070d5f52bcfe4ecfbee5f727afa458b2573b8eaaf77b3419b0bf2768dc67c86944da1544f06fa544fd47') { echo 'Installer verified'.PHP_EOL; } else { echo 'Installer corrupt'.PHP_EOL; unlink('composer-setup.php'); exit(1); }"
% /Applications/XAMPP/xamppfiles/bin/php  php composer-setup.php
% /Applications/XAMPP/xamppfiles/bin/php  php -r "unlink('composer-setup.php');"php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
% /Applications/XAMPP/xamppfiles/bin/php -r "if (hash_file('sha384', 'composer-setup.php') === 'c8b085408188070d5f52bcfe4ecfbee5f727afa458b2573b8eaaf77b3419b0bf2768dc67c86944da1544f06fa544fd47') { echo 'Installer verified'.PHP_EOL; } else { echo 'Installer corrupt'.PHP_EOL; unlink('composer-setup.php'); exit(1); }"
% /Applications/XAMPP/xamppfiles/bin/php composer-setup.php
%/ Applications/XAMPP/xamppfiles/bin/php -r "unlink('composer-setup.php');"

インストール確認

% /Applications/XAMPP/xamppfiles/htdocs/composer.phar -V
Composer version 2.9.2 2025-11-19 21:57:25
PHP version 8.2.4 (/Applications/XAMPP/xamppfiles/bin/php-8.2.4)
Run the "diagnose" command to get more detailed diagnostics output.

3. PHPUnitインストール

/htdocs/composer.json
{
  "require": {},
  "require-dev": {
    "phpunit/phpunit": "^11.0"
  }
}

① PHPUnitインストール

% /Applications/XAMPP/xamppfiles/bin/php  composer.phar install

② PHPUnitインストール確認

% /Applications/XAMPP/xamppfiles/bin/php /Applications/XAMPP/xamppfiles/htdocs/vendor/bin/phpunit --version
PHPUnit 11.5.46 by Sebastian Bergmann and contributors.

4. PSR-4(クラスをオートローディング)有効

htdocs/composer.json
{
  "require": {},
  "require-dev": {
    "phpunit/phpunit": "^11.0"
  },
  "autoload": {
    "psr-4": {
      "App\\": "../htdocs"
    }
  }
}

以下を実行

% /Applications/XAMPP/xamppfiles/bin/php composer.phar dump-autoload
Generating autoload files
Generated autoload files

5. PHPUnit 実行

今回は、自作のアプリケーションのMessageController.phpのMessageControllerクラスのテストを行う。
注意:MessageController.phpは記事に記載していません。

/htdocs/tests/MessageControllerTest.php
<?php
use PHPUnit\Framework\TestCase;
use App\Controllers\MessageController;

class MessageControllerTest extends TestCase
{
    protected $controller;

    protected function setUp(): void
    {
        $this->controller = new MessageController();
    }

    public function testInstanceCreated()
    {
        // $this->controllerがMessageControllerのインスタンスでない場合にエラー
        $this->assertInstanceOf(MessageController::class, $this->controller);
    }
}
?>

/htdocs/phpunit.xmlから実行も可能。

/htdocs/phpunit.xml
<phpunit
    bootstrap="vendor/autoload.php"
    colors="true"
>
    <testsuites>
        <testsuite name="Example Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

/htdocs/testsフォルダの中を実行したい時

% /Applications/XAMPP/xamppfiles/bin/php /Applications/XAMPP/xamppfiles/htdocs/vendor/bin/phpunit tests
PHPUnit 11.5.46 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.4
Configuration: /Applications/XAMPP/xamppfiles/htdocs/phpunit.xml

D                                                                   1 / 1 (100%)

Time: 00:00.033, Memory: 6.00 MB

OK, but there were issues!
Tests: 1, Assertions: 1, Deprecations: 2.

/htdocs/phpunit.xmlから実行したい時

% /Applications/XAMPP/xamppfiles/bin/php /Applications/XAMPP/xamppfiles/htdocs/vendor/bin/phpunit
PHPUnit 11.5.46 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.4
Configuration: /Applications/XAMPP/xamppfiles/htdocs/phpunit.xml

D                                                                   1 / 1 (100%)

Time: 00:00.033, Memory: 6.00 MB

OK, but there were issues!
Tests: 1, Assertions: 1, Deprecations: 2.

古い書き方があるみたいだが、今回はPHPUnitを実行することがゴールなのでOK。

/htdocs/test/◯◯◯◯Test.phpが実行される。
私は
・「◯◯◯◯Test.php」の形式で書かなかった時、実行できなかった。
・また「◯◯◯◯Test.php」は、「クラス名Test.php」でなければ実行できなかった。

7. 参考

ありがとうございました。
https://rakuraku-engineer.com/posts/phpunit/#%E4%BA%8B%E5%89%8D%E3%81%AB%E5%BF%85%E8%A6%81%E3%81%AA%E3%82%82%E3%81%AE

8 .感想

①. assert◯◯◯◯メソッドは調べれば他のパターンもあるので、他のパターンのテストも可能。
②. まとめて単体テストが可能。
という観点で考えると、画面をキャプチャしながらテストするという方法をしなくても済むのではないかと感じた。
ここまでできれば、業務で初めて使う場合でもキャッチアップしながら使えると思う。
是非、業務で使う機会があれば使ってみたい。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?