Skip to content

Zeroplex 生活隨筆

軟體開發、伺服器和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

標籤: Laravel

Laravel Horizon 僅支援 queue in Redis

Posted on 2023 年 4 月 2 日2023 年 4 月 2 日 By 日落 在〈Laravel Horizon 僅支援 queue in Redis〉中尚無留言

透過 Laravel Horizon 可以快速監看 queued jobs 的運作狀況,不過要注意的是僅支援資料放在 Redis 中的 queue:

Laravel Horizon requires that you use Redis to power your queue. Therefore, you should ensure that your queue connection is set to redis in your application’s config/queue.php configuration file.

所以要先檢查幾個項目:

  • 記得 composer reuqire predis/predis
  • 在 .env 中的 QUEUE_CONNECTION 數值必須是 redis
  • config/queue.php 中的 default 建議直接改成 redis
Tags:Laravel, PHP, Redis

Lavavel 各版本的 End Of Life (EOL)

Posted on 2023 年 2 月 20 日 By 日落 在〈Lavavel 各版本的 End Of Life (EOL)〉中尚無留言

前幾天 Laravel 推出 v10,另外舊版也不再提供安全更新。現況是:

  • Laravel 10:支援到 2024 年 9 月
  • Laravel 9 LTS:支援到 2024 年 9 月
  • 其他版本均不再提供功能、安全修正

若專案需要 migration 到新版本的 Laravel,可以考慮使用 Laravel Shift 服務。

另外還有個要注意的:

  • Laravel 9 可支援 PHP >= 8.0
  • Laravel 10 僅支援 PHP >= 8.1,不再支援 8.0
Tags:Laravel, PHP

Laravel Migration 出現 foreign key constraint in complete 注意事項

Posted on 2022 年 4 月 16 日2022 年 4 月 16 日 By 日落 在〈Laravel Migration 出現 foreign key constraint in complete 注意事項〉中尚無留言

今天遇到錯誤訊息耽誤了很久:

... foreign key constraint 'xxx_id_foreign' are incompatible.

錯誤訊息沒有詳細列出可能的錯誤,追了很久才知道有一些地方要注意。

More “Laravel Migration 出現 foreign key constraint in complete 注意事項” »

Tags:Database, Laravel, PHP

整合 Laravel / Lumen 和 AspectMock 的設定

Posted on 2022 年 3 月 4 日2022 年 3 月 4 日 By 日落 在〈整合 Laravel / Lumen 和 AspectMock 的設定〉中尚無留言

Laravel 和 Lumen 都有自己整理好的一套 unit test framework,因此若要加入 AspectMock 會需要對預設的 boostrap file 做一些調整。

整合時需要注意的事情:

  • 需要改寫 /bootstrap/app.php
  • phpunit.xml 中的 bootstrap config,要改成自己新建立的 bootstrap file
  • 注意 require 順序,不要讓 Laravel App 的資料被覆蓋掉 (另外也要注意 require 和 require_once 的差異)

建立新的 bootstrap 設定檔 /tests/bootstrap.php:

<?php

// composer autoload
require_once __DIR__.'/../vendor/autoload.php';

// load Laravel bootstrap
$app = require_once __DIR__ . '/../bootstrap/app.php';


// create temporary folder for AspectMock
$tmpPath = '/tmp/aspectMock/';
if (!file_exists($tmpPath)) {
    mkdir($tmpPath, 0777);
}

$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
    'appDir' => __DIR__ . '/..',
    'debug' => true,
    'includePaths' => [
        __DIR__ . '/../app',
        __DIR__ . '/../vendor/laravel',  // 如果需要 mock Model 則加上這行
    ],
    'excludePaths' => __DIR__, // "/tests" 目錄不需要處理
    'cacheDir' => $tmpPath,
]);

return $app;  // 把 App 回傳給 createApplication()

接下來要調整原有的 phpunit.xml 設定檔,需要參考 AspectMock 的要求,來調整:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="tests/bootstrap.php"
         backupGlobals="false">

注意最後面二行,bootstrap 要改成剛剛新建立的檔案,backupGlobals 則要關閉。

最後,去調整 BaseTest.php,在 tearDown() 加上 AspectMock 清理設定的指令:

protected function tearDown(): void
{
    test::clean();
}

另外在 createApplication() 這邊,要改用上面建立的 bootstrap file:

public function createApplication()
{
    return require __DIR__ . '/bootstrap.php';
}

以上全部改好的話,執行 vendor/bin/phpunit 應該可以正常運作。


Error: Call to a member function make() on boolean

注意 require 和 require_once 的差異,Laravel 會在每次開始 unit test 之前,重新建立 $app,所以用 require_once 的話,就的設定清不掉

-$app = require_once __DIR__ . '/../bootstrap/app.php'; // Laravel bootstrap
+$app = require __DIR__ . '/../bootstrap/app.php';
Tags:Laravel, PHP, PHPUnit

Customize HTTP header in Unit Test on Lumen

Posted on 2022 年 2 月 23 日2022 年 2 月 23 日 By 日落 在〈Customize HTTP header in Unit Test on Lumen〉中有 1 則留言

按照 Lumen 文件,在 unit test 中建立 HTTP request 要呼叫 $this->call(),但沒辦法自訂 header 內容。

細看實作 Laravel\Lumen\Testing\Concerns\MakesHttpRequests::call():

/**
 * Call the given URI and return the Response.
 *
 * @param  string  $method
 * @param  string  $uri
 * @param  array  $parameters
 * @param  array  $cookies
 * @param  array  $files
 * @param  array  $server
 * @param  string  $content
 * @return \Illuminate\Http\Response
 */
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
    $this->currentUri = $this->prepareUrlForRequest($uri);

    $symfonyRequest = SymfonyRequest::create(
        $this->currentUri, $method, $parameters,
        $cookies, $files, $server, $content
    );

    $this->app['request'] = LumenRequest::createFromBase($symfonyRequest);

    return $this->response = TestResponse::fromBaseResponse(
        $this->app->prepareResponse($this->app->handle($this->app['request']))
    );
}

從 function parameter 和 return,大概就知道使用這個方法沒辦法塞 header 值。

但是若透過 SymfonyRequest 的話,則可以設定 header 內容:

$request = SymfonyRequest::create();
$request->headers->set('acccess_token', 'ooxx');

因此,若需要自訂 header,則必須 override 原本的 call() 方法。以下是自己改寫的 trait:

use Illuminate\Testing\TestResponse;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

trait ApiTokenRequestTrait
{
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null, $headers = [])
    {
        $symfonyRequest = SymfonyRequest::create(
            $uri,
            $method,
            $parameters,
            $cookies,
            $files,
            $server,
            $content
        );

        foreach ($headers as $key => $value) {
            $symfonyRequest->headers->set($key, $value);
        }

        $this->app['request'] = \Laravel\Lumen\Http\Request::createFromBase($symfonyRequest);

        return TestResponse::fromBaseResponse(
            $this->app->prepareResponse($this->app->handle($this->app['request']))
        );
    }
}
Tags:Laravel, Lumen, PHP

文章分頁

上一頁 1 2 3 4 5 下一頁

其他

關於我  (About me)

小額贊助

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Ubuntu Unix Vim Web Windows WordPress XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.