Skip to content

Zeroplex 生活隨筆

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

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

標籤: PHP

PHP catch Exceptions and Errors

Posted on 2023 年 6 月 3 日 By 日落 在〈PHP catch Exceptions and Errors〉中有 2 則留言

早期 PHP 僅有 Exception 可以被 try ... catch,若遇到資料型態錯誤或是運算錯誤,則顯示 PHP Error 並且中斷程式執行;若是 Warning 或 Notice,則程式會繼續執行。

在 PHP v7.x 後,除了原有的 Exception 以外,另外增加了 Error 型態。PHP 在遇到錯誤時,改拋 Error 而非中斷實行,程式設計師也可以透過 try ... catch 來決定 Error 要如何處理。

try {
    count(100);
} catch (\Error $e) {
    // TypeError
}

或是

try {
    10 / 0;
} catch (\Error $e) {
   // DivisionByZeroError
}

這個 feature 讓 PHP 的 error handling 能力大幅增加,再加上防禦性程式設計,不太需要擔心沒留意 undefined behaviour 導致程式中斷、問題沒被記錄。

Tags:PHP

PHP build-in server 會將 dot 符號誤判為檔案

Posted on 2023 年 4 月 27 日2023 年 4 月 27 日 By 日落 在〈PHP build-in server 會將 dot 符號誤判為檔案〉中尚無留言

在 Laravel 中想要實作 route:

Route::get('domain/{name}')
    ->where('name', '[\w\d\.]+');

但在 local 的環境上一直顯示 404 NOT FOUND。

後來注意到 PHP build-in server (php -S) 的 log 中有顯示:

[404]: GET /whois/zeroplex.tw - No such file or directory

表示 PHP 把 zeroplex.tw 視為一個檔案,嘗試尋找並執行,當然會變成 404。

若將環境改為 Nginx + php-fpm 就不會遇到這個錯誤了。


參考資料:php – Route with dot (IP address) not found, returns 404 – Stack Overflow

Tags:Laravel, 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

文章分頁

上一頁 1 2 3 4 ... 26 下一頁

其他

關於我  (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 國際 授權條款授權.