6
4

More than 5 years have passed since last update.

LaravelのIntegrated Test機能を普通のPHPUnitで使う

Last updated at Posted at 2015-11-23

Laravel5.1から追加されたIntegrated Testを普通のPHPUnit環境で使う方法です。元々はlaracast/integratedで提供されていたものが5.1から統合されたものです。

なので素のPHPUnitでも使えます。
素のPHPUnitでWebAPIとか通信系のテストをするのはかなりウザイのでLaraveler以外の人にも有用です。

Laravel内で外部サイトのテストもしたかったのですが、うまく動かなかったので普通の方法も。

基本的には、ここの通りやればいいのですが、Databaseに関する機能を利用する場合、symfony/var-dumperを追加する必要がありました。

制限事項

元々Laravelで構築したアプリ用なのでJavaScriptを使ったページのテストは苦手です。
Selenium連携版もありますが、現時点(2015年11月時点)では完成度はいまいちで、素直にSeleniumの環境でテストを書いたほうがいいでしょう。

前提環境

  • composerをインストールしておきましょう。

準備

PHPUnitのインストール

mkdir test1
cd test1
composer require phpunit/phpunit --dev

laracast/integratedのインストール

composer require laracasts/integrated --dev

suggestsされるものを入れる

laracasts/integratedを入れると、

symfony/browser-kit suggests installing symfony/process ()
illuminate/support suggests installing jeremeamia/superclosure (Required to be able to serialize closures (~2.0).)
illuminate/support suggests installing paragonie/random_compat (Provides a compatible interface like PHP7's random_bytes() in PHP 5 projects (^1.0.6).)
illuminate/support suggests installing symfony/var-dumper (Required to use the dd function (2.7.*).)

と言われます。このうち、symfony/var-dumperはdatabase系の機能を利用するのに必要なのでインストールします。

composer require symfony/var-dumper:2.7.* --dev

あとは、必要に応じてインストールすることにします。

laracast/integratedを使う

基本記述

test1/testsディレクトリを作成し、その中にsampleTest.phpをします。そして、下記の記述を行ないます。

<?php

    //autoload.phpファイルの読み込み
    require_once './vendor/autoload.php';

    //長いので短い名前つけます。
    use Laracasts\Integrated\Extensions\Goutte as IntegrationTest;

    //テストクラス
    class ExampleTest extends IntegrationTest
    {
        //標準ではhttp://localhost:8888になっている
        protected $baseUrl = "http://www.yahoo.co.jp";

        public function testYahoo()
        {
            $this->visit('/')->see('Yahoo');
        }
    }

テスト実行

./vendor/bin/phpunit --colors tests/

OK (1 test, 2 assertions)

となり、これでOKです。

baseUrlの変更

Laravelではこれが出来なかった。baseUrlを変更してみます。

<?php

    require_once './vendor/autoload.php';

    use Laracasts\Integrated\Extensions\Goutte as IntegrationTest;


    class ExampleTest extends IntegrationTest
    {
        //test for yahoo
        protected $baseUrl = "http://www.yahoo.co.jp";

        public function testYahoo()
        {
            $this->visit('/')->see('Yahoo');
        }


        //test for google
        public function testGoole()
        {
            //baseUrlの変更
            $this->baseUrl = "http://www.google.jp";
            $this->visit('/')->see('Google');
        }

    }

データベースをテストする

DBへの接続情報をintegrated.jsonに記述する

Larave内では、.evnの情報を引き継ぐので何もしなくて良いが、素の状態では、アプリケーションルート(ここでは、test1直下)にintegrated.jsonにDBの接続情報を記述します。

なお、githubのmarkdownファイルのサンプル記述は、connectionの最後に,が無く、動きません。

{
    "pdo": {
        "connection" : "mysql:host=localhost;dbname=testdb",
        "username" : "username",
        "password" : "password"
    }
}

テストを書く(抜粋)

非常に簡単です。

public function testDb()
{
    $this->seeInDatabase('users',['name'=>'hoge2']);
}

$this->VerifyInDatabase('users',['name'=>'hoge2']);でもOKです。

その他の使い方は、ここを参考にいろいろ試して下さい。Laravelでの使いかたやForm、APIでの記述の仕方はこちらと同じです。

その他

LaravelのQueryBuilderやEloquentを使う

UIやAPIのテストを実行した結果、DBに正常に値が書き込まれているかをチェックしたくなります。もちろん普通にPDOでコードを書いてもいいですが、Laravelに慣れてる場合、QueryBuilderやEloquentが使いたくなります。その場合は、こちらを参考にしてください。

6
4
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
6
4