LoginSignup
6

More than 5 years have passed since last update.

posted at

updated at

Laravel5.4 へのアップデート手順と生じた変更点4つ

はじめに

Laravel を 5.3 から 5.4 にアップデートしたら、思いのほか大きく改修することになった。
忘れないように手順と変更点をメモしておく。

アップデート手順

composer.json でバージョンをあげる

composer.json
"require": {
    "laravel/framework": "5.4.*"
}

アップデート

  • composer update

バージョン確認

  • php artisan -V

キャッシュクリア

  • php artisan view:clear
  • php artisan route:clear

ide_helper を再作成(ついでに)

  • php artisan ide-helper:generate

※ide_helper とは
https://github.com/barryvdh/laravel-ide-helper

変更された処理と対応

1. PHPUnit のバージョンを上げる

composer.json
"require-dev": {
    "phpunit/phpunit": "5.7.*",
}

ひとまず PHPUnit のバージョンを上げよう。
上げないと大量にエラーを吐く。

2. Assert メソッドが変わった

この変更が原因でテストが失敗しまくっていた。
各 Assert メソッドを置換して対応した。

Assert メソッド新旧対応表

5.3 以前 5.4
seeInDatabase assertDatabaseHas
dontSeeInDatabase assertDatabaseMissing
seeJson assertJson
dontSeeJson ?

dontSeeJson の代わりがないので作成した

AbstractTestCase.php

private function formatToExpectedJson($key, $value)
{
    $expected = json_encode([$key => $value]);

    if (Str::startsWith($expected, '{')) {
        $expected = substr($expected, 1);
    }

    if (Str::endsWith($expected, '}')) {
        $expected = substr($expected, 0, -1);
    }

    return trim($expected);
}

protected function assertDontJson(TestResponse $testResponse, array $assertData)
{

    $testResponseContent = json_encode(
        (array)$testResponse->original
    );

    foreach ($assertData as $key => $value) {
        $expected = $this->formatToExpectedJson($key, $value);
        PHPUnit::assertFalse(
            Str::contains($testResponseContent, $expected),
            "[{$expected}]".PHP_EOL.'within'.PHP_EOL."[{$testResponseContent}]."
        );
    }

    return $testResponse;
}

テスト関連リファレンス

https://laravel.com/docs/5.4/testing
https://laravel.com/docs/5.4/http-tests
https://laravel.com/docs/5.4/database-testing

3. make() メソッドのパラメータに第 2 引数を指定できなくなった

make Method Parameters

The container's make method no longer accepts a second array of parameters. This feature typically indicates a code smell. Typically, you can always construct the object in another way that is more intuitive.

4. レスポンスの中身へのアクセス方法が変わった

5.3 以前 5.4
$response->response->content() $response->content()

5.4 の方はトレイトで取得している
/vendor/laravel/framework/src/Illuminate/Http/ResponseTrait.php

あとがき

他にも気づいたら追記して行こうと思う。

公式のアップグレードガイド

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
What you can do with signing up
6