Skip to content

Zeroplex 生活隨筆

軟體開發和生活瑣事

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

標籤: PHP

PHPUnit 10 改變 expected exception 的寫法

Posted on 2023 年 4 月 22 日2023 年 4 月 22 日 By 日落 在〈PHPUnit 10 改變 expected exception 的寫法〉中尚無留言

PHPUnit 9 以及之前測試是否有 throw exception 的方法,是在 test case 前加上 annotation:

/**
 * @expectedException \Exception
 */
public function testFailed() { }

在 PHPUnit 10 則直接在 test case 中呼叫 assertion 即可:

public function testFailed()
{
    $this->expectException(\Exception::class);
}

但要注意,必須在測試開始之前。若 exception 比 expectedException() 還要早出現仍會視為 test failed。

Tags:PHP, PHPUnit

安裝 Codeception 撰寫 unit test

Posted on 2023 年 4 月 20 日 By 日落 在〈安裝 Codeception 撰寫 unit test〉中尚無留言

Codeception 是一個 testing framework,除了 unit test 以外,還包含了 functional test、behavior test,並提供各種 module 供整合。

到專案目錄使用 composer 安裝 Codeception:

composer require codeception/codeception --dev

初始化 unit test 的設定:

php vendor/bin/codecept init Unit

 This will install Codeception for unit testing only

? Where tests will be stored? (tests) 

Codeception provides additional features for integration tests
Like accessing frameworks, ORM, Database.
? Do you wish to enable them? (y/n) n
 Adding codeception/module-asserts for Asserts to composer.json
1 new packages added to require-dev
? composer.json updated. Do you want to run "composer update"? (y/n) y
 Running composer update

建立新的 unit test 可透過 generate:test 指令來建立測試範例:

php vendor/bin/codecept generate:test unit MyTest

產生的檔案會放在 tests/MyTest.php。

樣板檔大概長這樣:

<?php

namespace Tests\Unit;

// 有使用才加,用不到可以刪除
use Tests\Support\ .... ;

class ArrayQueueTest extends \Codeception\Test\Unit
{

    protected function _before()
    {
    }

    // tests
    public function testSomeFeature()
    {
    }
}

其中:

  • _before():相當於 PHPUnit 的 setup()。_after() 則是 tearDown()
  • testSomeFeature():只要是 public function 且是 test 開頭,則自動視為 test case

透過 run 可以執行測試:

php vendor/bin/codecept run
Codeception PHP Testing Framework v5.0.10 https://helpukrainewin.org
[Seed] 2070615206

Tests.unit Tests (1) -----------------------------------------------------------------------------------------------------------------------------------------
✔ ArrayQueueTest: Some feature(0.00s)
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Time: 00:00.045, Memory: 10.00 MB
Tags:Codeception, PHP

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

PHP PDO 連線至 PostgreSQL

Posted on 2023 年 3 月 3 日2023 年 3 月 3 日 By 日落 在〈PHP PDO 連線至 PostgreSQL〉中尚無留言

先確認有安裝 php-pgsql 套件:

sudo apt install php-pgsql

安裝完套件,就可以使用 PDO 來建立連線:

$connection = new PDO(
    'pgsql:host=127.0.0.1;port=5432;dbname=postgres',
    'postgres',
    'password'
);
Tags:PHP, PostgreSQL

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

文章分頁

上一頁 1 ... 3 4 5 ... 27 下一頁

其他

關於我  (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 Raspberry Pi Ubuntu Unix Vim Web Windows XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


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