使用phpunit進行單元測試
本教程假定您使用 PHP 8.1 或 PHP 8.2。您將學習如何編寫簡單的單元測試以及如何下載和運行 PHPUnit.
PHPUnit 10 的文檔 在這。
下載:可以用以下2種方法之一:
1.PHP 存檔 (PHAR)
我們分發了一個 PHP存檔(PHAR),其中包含使用PHPUnit 10所需的一切 。只需從這里 下載 并使其可執行:
wget -O phpunit https://phar.phpunit.de/phpunit-10.phar
? chmod +x phpunit
? ./phpunit --version
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
2.Composer
您可以使用 Composer 將 PHPUnit 作為本地、每個項目、開發時依賴項添加到您的項目中:
? composer require --dev phpunit/phpunit ^10
? ./vendor/bin/phpunit --version
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
上面顯示的示例假定composer在您的$PATH上。
您的 composer.json 應該看起來像這樣:
{"autoload": {"classmap": ["src/"]},"require-dev": {"phpunit/phpunit": "^10"}
}
代碼
src/Email.php
<?php
declare(strict_types=1);
final class Email
{private string $email;private function __construct(string $email){$this->ensureIsValidEmail($email);$this->email = $email;}public static function fromString(string $email): self{return new self($email);}public function asString(): string{return $this->email;}private function ensureIsValidEmail(string $email): void{if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {throw new InvalidArgumentException(sprintf('"%s" is not a valid email address',$email));}}
}
測試代碼
tests/EmailTest.php
<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
final class EmailTest extends TestCase
{public function testCanBeCreatedFromValidEmail(): void{$string = 'user@example.com';$email = Email::fromString($string);$this->assertSame($string, $email->asString());}public function testCannotBeCreatedFromInvalidEmail(): void{$this->expectException(InvalidArgumentException::class);Email::fromString('invalid');}
}
測試執行:以下2種方法都可以:
1.PHP 存檔 (PHAR)
? ./phpunit --bootstrap src/autoload.php tests
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 70 ms, Memory: 10.00MB
OK (2 tests, 2 assertions)
上面假設你已經下載了phpunit.phar并將其作為phpunit放入你的$PATH,并且src/autoload.php 是一個為要測試的類設置自動加載 的腳本。這樣的腳本通常使用 phpab 等工具生成。
–bootstrap src/autoload.php指示 PHPUnit 命令行測試運行程序在運行測試之前包含src/autoload.php.
tests 指示 PHPUnit 命令行測試運行程序執行在 tests 目錄的 *Test.php 源代碼文件中聲明的所有測試.
2.Composer
? ./vendor/bin/phpunit tests
PHPUnit 10.0.0 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 70 ms, Memory: 10.00MB
OK (2 tests, 2 assertions)
上面假設 vendor/autoload.php(由 Composer 管理的自動加載器腳本)存在,并且能夠加載 Email 類的代碼。根據設置自動加載的方式,您可能需要立即運行composer dump-autoload。
tests 指示 PHPUnit 命令行測試運行程序執行在 tests 目錄的 Test.php 源代碼文件中聲明的所有測試.
一些測試組件推薦:
https://packagist.org/packages/mockery/mockery
phpunit/phpunit
fakerphp/faker
https://github.com/phpstan/phpstan
vimeo/psalm
mikey179/vfsstream
rector/rector
引用
declare和strict_types
ps:declare(strict_types=1);
嚴格類型
默認情況下,如果能做到的話,PHP將會強迫錯誤類型的值轉為函數期望的標量類型。例如,一個函數的一個參數期望是string,但傳入的是integer,最終函數得到的將會是一個string類型的值。
可以基于每一個文件開啟嚴格模式。在嚴格模式中,只有一個與類型聲明完全相符的變量才會被接受,否則將會拋出一個TypeError。 唯一的一個例外是可以將integer傳給一個期望float的函數。
使用 declare 語句和strict_types 聲明來啟用嚴格模式
https://blog.csdn.net/joshua317/article/details/121252625
assertsame
使用運算符檢查身份
報告由 if 標識的錯誤,如果兩個變量的類型和值不同 或者 兩個變量不引用同一對象 報錯
https://docs.phpunit.de/en/10.1/assertions.html#assertsame