Skip to content

Zeroplex 生活隨筆

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

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

標籤: PHP

PHP 中 「…」(three dots) 也可以用在 argument provider

Posted on 2018 年 3 月 9 日2021 年 3 月 12 日 By 日落 在〈PHP 中 「…」(three dots) 也可以用在 argument provider〉中尚無留言

看了 PHPUnit 的 source code,發現有個很有趣的寫法:

function assertSame($expected, $actual, string $message = ''): void
{
    Assert::assertSame(...func_get_args());
}

最初以為「…」只能用在定義 variable-length function argument/param,重新看了文件才發現之前文件沒有看完,也可以拿來把陣列分別當作 argument/param 傳入:

function add($a, $b) {
    return $a + $b;
}

add(...[1, 2]);  // $a = 1, $b = 2

// or ...

$args = [1, 2];
add(...$args);
Tags:PHP

PHPUnit 中「backupGlobals」的作用

Posted on 2018 年 3 月 8 日2021 年 3 月 12 日 By 日落 在〈PHPUnit 中「backupGlobals」的作用〉中尚無留言

複習 PHPUnit 順手寫了小程式做實驗。

PHPUnit 為了讓各個不同的 test case 不會因為 super globals 變數而互相影響,設計了 「backupGlobals」這個功能。先來看範例程式:

class MyTest extends TestCase
{
    public function testOne()
    {
        $GLOBALS['test'] = 123;

        $this->assertArrayHasKey('test', $GLOBALS);
    }

    public function testTwo()
    {
        $this->assertArrayHasKey('test', $GLOBALS);
    }
}

範例中 testOne() 修改了 $GLOBALS 變數的內容,如果在不使用「backupGlobals」的情況下,在 testTwo() 中的 $GLOBALS 也會保留 testOne() 修改過的內容,使 testTow() 抓得到「test」這個 key:

$ phpunit MyTest.php 
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.

..                                                                  2 / 2 (100%)

Time: 40 ms, Memory: 4.00MB

OK (2 tests, 2 assertions)

也就表示 testOne() 影響到了 testTwo() 的測試結果,這在測試時是不樂見的,各個測試應該有獨立的測試環境,不應該互相影響。

使用「backupGlobals」功能,PHPUnit 會在 test case 執行之前,使用 serialize() 對 super globals 做備份,並在測試結束以後 unserialize() 復原,所以 testOne() 中做的修改就不會影響到 testTwo() 了:

$ phpunit --globals-backup MyTest.php 
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.

.F                                                                  2 / 2 (100%)

Time: 52 ms, Memory: 4.00MB

There was 1 failure:

1) MyTest::testTwo
Failed asserting that an array has the key 'test'.

/home/johnroyer/tmp/MyTest.php:16

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

重點來了,因為是使用 serialize() 和 unserialize(),所以只能針對 primitive type 進行備份和還原,如果使用了 object 或是 singleton 時,「backupGlobals」就派不上用場了。這個時候要使用殺手鐧「runTestsInSeparateProcesses」:

/**
 * @runTestsInSeparateProcesses
 */
class MyTest extends TestCase
{
    ....
}

使用這個 annotation 會讓 PHPUnit 為每個 test case 分別 fork 出新的 proccess 進行測試,因此不同 test case 就可以在完全獨立的環境下執行不受影響:

$ phpunit MyTest.php 
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.

.F                                                                  2 / 2 (100%)

Time: 303 ms, Memory: 4.00MB

There was 1 failure:

1) MyTest::testTwo
Failed asserting that an array has the key 'test'.

/home/johnroyer/tmp/MyTest.php:19

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

但使用這個殺手鐧也是要付出一點代價:時間。最前二次執行 PHPUnit 時,跑完二個 test case 只花費了 50ms 左右,而開 proccess 來進行測試則花了 300ms。

Tags:PHP, PHPUnit

PHP 7.x 與 HHVM 的效能比較

Posted on 2018 年 3 月 2 日2021 年 3 月 12 日 By 日落 在〈PHP 7.x 與 HHVM 的效能比較〉中尚無留言

前幾天看到國外有人使用 PHP 7.x 和 HHVM 做了效能測試「The Definitive PHP 5.6, 7.0, 7.1, 7.2 & HHVM Benchmarks (2018)」,可以從比較上看到 PHP 7 以後速度有大幅的躍進,到了 7.1 與 7.2 已經和 HHVM 不相上下了。

新版的 Druple 還不支援 PHP 7.2,但 7.1 的效能也很接近 HHVM 了

目前用過跑起來最慢的 project 應該是 Gallery 和 Druple (古早的版本),由上圖可以看到 PHP 7.x 幾乎快了 5.x 一倍,另外在 7.1 的效能也逼近 HHVM 了。

相較於 HHVM 在佈署之前還要經過編譯步驟,我想應該很多人會回來使用 PHP 7.x 吧。

Tags:PHP

Silex EOL in June 2018

Posted on 2018 年 1 月 15 日2021 年 3 月 12 日 By 日落 在〈Silex EOL in June 2018〉中尚無留言

Symfony 團隊認為新版的 Symfony 4 的修改,已經能讓使用者有和 Silex 相同的使用者體驗,於是決定不再繼續維護 Silex:

http://symfony.com/blog/the-end-of-silex

目前 Silex 將在 6 月結束維護 (EOL, End Of Life):
So, we’ve decided to not support Symfony 4 in Silex, or at least not add the new features added in 3.4. The current stable version of Silex is still maintained for bugs and security issues. But its end of life is set to June 2018.

Tags:PHP

PHP 7.2 下使用 xdebug

Posted on 2018 年 1 月 14 日2021 年 3 月 12 日 By 日落 在〈PHP 7.2 下使用 xdebug〉中尚無留言

我目前使用的 PHP APT 是「http://ppa.launchpad.net/ondrej/php/ubuntu」,不過看起來在 PHP 7.2 還沒有 xdebug 可以使用。

如果硬要使用 aptitude 安裝,只能裝到 PHP 7.1 的版本。

所以後來用 phpbrew 在硬幹,發現不能安裝是有原因的:目前 xdebug stable 版本只支援到 PHP 7.1,若要在 PHP 7.2 使用 xdebug,只能先安裝 dev 版本。

所以開發機可能考慮先降版本回到 7.1 吧。

Tags:PHP

文章分頁

上一頁 1 ... 12 13 14 ... 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 國際 授權條款授權.