Help us understand the problem. What is going on with this article?

Laravelパッケージ開発ハンズオン〜テスト導入編〜

前回の Laravelパッケージ開発ハンズオン〜環境編〜 続きの記事です。

Laravelテスト導入

  • sebastianbergmann/phpunit
    • PHPの単体テストフレームワーク
  • orchestral/testbench
    • Laravelのパッケージ開発用のユニットテストヘルパー
    • パッケージのテストでLaravelのコアクラスを利用できる

作業場所

$ cd docker
$ docker-compose exec app ash
$ cd /packages/laravel-hello-world

作成するファイル・ディレクトリ

$ touch .gitignore
$ touch phpunit.xml
$ mkdir tests
$ touch tests/TestCase.php
$ touch tests/ExampleTest.php

空のファイル・ディレクトリを作成しておきます。

.gitignore を編集

/vendor
/composer.lock
/.phpunit.result.cache

テストライブラリの導入

$ composer require phpunit/phpunit --dev
$ composer require orchestra/testbench --dev

phpunit のバージョン確認

$ ./vendor/bin/phpunit --version
PHPUnit 8.3.4 by Sebastian Bergmann and contributors.

composer.json を編集

    "autoload-dev": {
        "psr-4": {
            "UcanLab\\LaravelHelloWorld\\Test\\": "tests/"
        }
    }

※カンマ(,)に注意

オートローダ生成

$ composer dump-autoload

phpunit.xml を編集

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
         backupGlobals="false"
         backupStaticAttributes="false"
         colors="true"
         verbose="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="MyPackage Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">src/</directory>
        </whitelist>
    </filter>
</phpunit>

テストの作成

tests/TestCase.php

<?php

namespace UcanLab\LaravelHelloWorld\Test;

use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
}

tests/ExampleTest.php

<?php

namespace UcanLab\LaravelHelloWorld\Test;

class ExampleTest extends TestCase
{
    /**
     * @return void
     */
    public function testResolve()
    {
        $this->assertTrue(true);
    }
}

テストの実行

$ ./vendor/bin/phpunit
PHPUnit 8.3.4 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.3.8 with Xdebug 2.7.2
Configuration: /packages/laravel-hello-world/phpunit.xml

.                                                                   1 / 1 (100%)

Time: 1.18 seconds, Memory: 12.00 MB

OK (1 test, 1 assertion)

参考

ucan-lab
Backend Developer at ROLO. I love PHP and I'm focusing on Laravel, Docker, GraphQL.
https://u-can.pro
miraito-inc
システムデザインを中心に置いた開発により高品質で使いやすいシステムを提供いたします。業務システム構築、アプリ開発、コンサルティングまで幅広く手がけています。
https://miraito-inc.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした