1
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?

More than 5 years have passed since last update.

PHPUnit8系と7系で違うところメモ【with laravel 6 & laravel 5.8】

1
Last updated at Posted at 2019-12-25

概要

同時に使っていたら少しずつ違って混乱したので、メモ

環境

  • laravel 6.7.0 / PHPUnit 8.5.0
  • laravel 5.8 / PHPUnit 7.5

をそれぞれ使い比べた

特定の環境下のみでテストは実行したい

setUpBeforeClassに定義する。
実行順からして、setUpBeforeClass内では一手間(後述)加えないとlaravelのヘルパーは使えない。

との話でしたが、 laravel 6.7 / PHPUnit 8.5 では問題ありませんでした。
laravel6、またはPHPUnit8から仕様が変わっているのではと思います。

    public static function setUpBeforeClass(): void
    {
        parent::setUpBeforeClass();
        if (config('app.env') === 'local') {
			// ごにょごにょ
        }
    }

dataProviderでEloquentなどlaravelの機能使いたい

dataProviderはsetUpBeforeClassよりも先に実行されるためlaravelのクラス等が使用出来ないので一手間使う。

こちらは laravel 6.7 / PHPUnit 8.5 でもエラーが発生した。

laravelの初期化を行うメソッドを実行するか、

public function addProvider() 
{
	$this->refreshApplication();
}

クロージャ内ならば実行出来る。

public function addProvider() 
{
    return [
        'test1' => function() {
			return Article::all();
		},
        'test2' => function() {
			return Article::all();
		}
    ]
}
1
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
1
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?